For example, Classes B, C and D all contain the variables x, y and z
class A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A
// 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
Example 1: C++ public Inheritance
#include <iostream>
using namespace std;
class Base
{ private: int pvt = 1;
protected: int prot = 2;
public: int pub = 3;
// function to access private member
int getPVT()
{ return pvt; }};
class PublicDerived : public Base
{ public:
// function to access protected member from Base
int getProt()
{ return prot;
}
};
int main()
{
PublicDerived object1;
cout << "Private = " << object1.getPVT() <<
endl;
cout << "Protected = " << object1.getProt() <<
endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
Output
Private = 1
Protected = 2
Public = 3
// C++ program to demonstrate the working of protected inheritance
#include<iostream>
using namespace std;
class Base
{ private: int pvt = 1;
protected: int prot = 2;
public: int pub = 3;
// function to access private member
int getPVT() { return pvt; } };
class ProtectedDerived : protected Base
{ public:
// function to access protected member from
Base
int getProt()
{ return prot; }
// function to access public member from Base
int getPub()
{ return pub;
}};
int main()
{
ProtectedDerived object1;
cout << "Private cannot be accessed." <<
endl;
cout << "Protected = " <<
object1.getProt() << endl;
cout << "Public = " << object1.getPub() <<
endl;
return 0;
}
Output
Private cannot be accessed.
Protected = 2
Public = 3
// C++ program to demonstrate the working of private inheritance
#include <iostream>
using namespace std;
class Base
{ private: int pvt = 1;
protected: int prot = 2;
public: int pub = 3;
// function to access private member
int getPVT()
{ return pvt; }};
class PrivateDerived : private Base
{ public:
// function to access protected member from Base
int getProt() { return prot; }
// function to access private member
int getPub()
{ return pub; }};
int main()
{
PrivateDerived object1;
cout << "cannot be accessed." << endl;
cout << "Protected = " <<
object1.getProt() << endl;
cout << "Public = " << object1.getPub()
<< endl;
return 0;}
Output
Private cannot be accessed.
Protected = 2
Public = 3
Single inheritance Example 1
#include <iostream>
// Base class
class Shape
{ public:
void setWidth(int w)
{ width = w; }
void setHeight(int h)
{ height = h; }
protected: int width; int height;
};
// Derived class
class Rectangle: public Shape
{ public:
int getArea()
{ return (width * height); }
};
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() <<
endl;
return 0;
}
When the above code is compiled and
executed, it produces the following result −
Total area: 35
Example 2
# include <iostream.h>
class Value
{
protected:
int val;
public:
void set_values (int a)
{ val=a;}
};
class Cube: public Value
{
public:
int cube()
{ return (val*val*val); }
};
int main ()
{
Cube cub;
cub.set_values (5);
cout << "The Cube of 5 is::" << cub.cube() << endl;
return 0;
}
Result:
The Cube of 5 is:: 125
In the above example the derived class "Cube" has only one base
class "Value". This is the single inheritance OOP's concept.
C++ program for Single inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// sub class derived from two base classes
class Car: public Vehicle
{
-------
};
// main function
int main()
{
/* creating object of sub class
will invoke the constructor of
base classes */
Car obj;
return 0;
}
Output:
This is a vehicle
program to explain multiple inheritance
#include <iostream.h>
// first base class
class Vehicle {
public:
Vehicle()
{ cout << "This is a Vehicle" << endl; }
};
// second base class
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" <<
endl;
} };
// sub class derived from two base classes
class Car: public Vehicle, public
FourWheeler {
};
int main()
{
/* creating object of sub class will invoke
the constructor of base classes */
Car obj;
return 0;
}
Output:
This is a Vehicle
This is a 4 wheeler Vehicle
program to explain multiple inheritance
#include <iostream>
using namespace std;
// Base class Shape
class Shape
{ public:
void setWidth(int w)
{ width = w; }
void setHeight(int h)
{ height = h; }
protected: int width; int height; };
// Base class PaintCost
class PaintCost
{ public:
int getCost(int area)
{ return area * 70; } };
// Derived class
class Rectangle: public Shape, public PaintCost
{ public: int getArea()
{ return (width * height);
}
};
int main(void)
{ Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
// Print the total cost of painting
cout << "Total paint cost: $" << Rect.getCost(area) << endl;
return 0; }
When the above code is compiled and executed, it produces the
following result −
Total area: 35
Total paint cost: $2450
Example:
#include <iostream.h>
using namespace std;
class Square
{
protected:
int l;
public:
void set_values (int x)
{ l=x;}
};
class CShow
{
public:
void show(int i);
};
void CShow::show (int i)
{
cout << "The area of the square is::" << i << endl;
}
class Area: public Square, public CShow
{
public:
int area()
{ return (l *l); }
};
int main ()
{
Area r;
r.set_values (5);
r.show(r.area());
return 0;
}
Result:
The area of the square is:: 25
program to explain multlevel inheritance
#include <iostream.h>
// base class
class Vehicle
{
public:
Vehicle()
{ cout << "This is a Vehicle" << endl; }
};
// sub class derived from vehicle base class
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"Objects with 4 wheels are
vehicles"<<endl;
} };
// sub class derived from two base classes
class Car: public fourWheeler{
public:
car()
{ cout<<"Car has 4 Wheels"<<endl; }
};
int main()
{
//creating object of sub class will invoke
the constructor of base classes
Car obj;
return 0;
}
output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
#include <iostream.h>
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{ rollno = a; }
void put_num()
{ cout << "Roll Number Is:n"<< rollno << "n"; }
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{ sub1 = x;
sub2 = y; }
void put_marks(void)
{ cout << "Subject 1:" << sub1 << "n";
cout << "Subject 2:" << sub2 << "n"; }
};
class res : public marks
{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2;
put_num();
put_marks();
cout << "Total:"<< tot; }
};
int main()
{
res std1;
std1.get_num(5);
std1.get_marks(10,20);
std1.disp();
return 0;
}
Result:
Roll Number Is: 5
Subject 1: 10
Subject 2: 20
Total: 30
In the above example, the derived function "res" uses the function
"put_num()" from another derived class "marks", which just a level above.
This is the multilevel inheritance OOP's concept in C++.
Hierarchical Inheritance
In this type of inheritance, more than one sub
class is inherited from a single base class. i.e.
more than one derived class is created from a
single base class.
// C++ program to implement Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// first sub class
class Car: public Vehicle
{
----------
};
// second sub class
class Bus: public Vehicle
{
};
int main()
{
// creating object of sub class will
invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}
Output:
This is a Vehicle
This is a Vehicle
#include <iostream.h>
class Side
{
protected:
int l;
public:
void set_values (int x)
{ l=x;}
};
class Square: public Side
{
public:
int sq()
{ return (l *l); }
};
class Cube: public Side
{
public: int cub()
{ return (l*l*l); }
};
Example:
int main ()
{
Square s;
s.set_values (10);
cout << "The square value is::"
<< s.sq() << endl;
Cube c;
c.set_values (20);
cout << "The cube value is::"
<< c.cub() << endl; return 0; }
Result:
The square value is:: 100
The cube value is::8000
Get square and cube of a number by using Hierarchical Inheritance
Class Number
{ private: int num;
public: void getNumber(void)
{ cout << "Enter integer number: "; cin >>
num; }
int returnNumber(void)
{ return num; } };
class Square:public Number
{ public: int getSquare(void)
{ int num,sqr; num=returnNumber();
sqr=num*num; return sqr; } };
class Cube:public Number { private:
public: int getCube(void) { int
num,cube; num=returnNumber();
cube=num*num*num;
return cube; } };
int main()
{
Square objS; Cube objC;
int sqr,cube; objS.getNumber();
sqr =objS.getSquare();
cout << "Square of "<<
objS.returnNumber() << " is: " << sqr <<
endl; objC.getNumber();
cube=objC.getCube();
cout << "Cube of "<< objS.returnNumber()
<< " is: " << cube << endl;
return 0; }
Output
Enter an integer number: 10 Square of 10
is: 100
Enter an integer number: 20
Cube of 10 is: 8000
// C++ program to implement Hierarchical Inheritance
#include <iostream.h>
#include <string.h>
class member
{ char gender[10]; int age;
public: void get() { cout << "Age: ";
cin >> age; cout << "Gender: ";
cin >> gender; }
void disp()
{ cout << "Age: " << age << endl;
cout << "Gender: " << gender ; cout<< endl;
} };
class stud : public member
{ char level[20];
public: void getdata()
{ member::get();
cout << "Class: "; cin >> level; }
void disp2()
{ member::disp();
cout << "Level: " << level << endl; } };
class staff : public member
{ float salary;
public: void getdata()
{ member::get();
cout << "Salary: Rs."; cin >> salary; }
void disp3()
{ member::disp();
cout << "Salary: Rs." << salary << endl; }};
int main()
{ member M;
staff S; stud s;
cout << "Student" << endl; cout << "Enter data" << endl;
s.getdata();
cout << endl << "Displaying data" << endl;
s.disp();
cout << endl << "Staff Data" << endl;
cout << "Enter data" << endl;
S.getdata();
cout << endl << "Displaying data" << endl;
S.disp();
}
Output
// C++ program to implement Hybrid Inheritance
#include <iostream.h>
// base class
class Vehicle
{
public:
Vehicle()
{ cout << "This is a Vehicle" << endl;}
};
//base class
class Fare
{ public:
Fare()
{ cout<<"Fare of Vehiclen"; }
};
// first sub class
class Car: public Vehicle
{
};
// second sub class
class Bus: public Vehicle, public Fare
{
};
int main()
{
Bus obj2;
return 0;
}
Output:
This is a Vehicle
Fare of Vehicle
EXAMPLE
#include <iostream.h>
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{ rollno = a; }
void put_num()
{ cout << "Roll Number Is:"<< rollno << "n"; }
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{ sub1 = x;
sub2 = y; }
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "n";
cout << "Subject 2:" << sub2 << "n";
}
};
class extra
{
protected:
float e;
public:
void get_extra(float s)
{e=s;}
void put_extra(void)
{ cout << "Extra Score::" << e << "n";}
};
class res : public marks, public extra{
protected:
float tot;
public:
void disp(void)
{ tot = sub1+sub2+e;
put_num();
put_marks();
put_extra();
cout << "Total:"<< tot; }
};
int main()
{
res std1;
std1.get_num(10);
std1.get_marks(10,20);
std1.get_extra(33.12);
std1.disp();
return 0;
}
Result:
Roll Number Is: 10
Subject 1: 10
Subject 2: 20
Extra score:33.12
Total: 63.12
• In the above example the derived class "res" uses the
function "put_num()". Here the "put_num()" function is
derived first to class "marks". Then it is derived and used
in class "res". This is an example of "multilevel
inheritance-OOP's concept". But the class "extra" is
inherited a single time in the class "res", an example for
"Single Inheritance". Since this code uses both
"multilevel" and "single" inheritance it is an example of
"Hybrid Inheritance".
Example
#include<iostream.h>
class Person
{ int id; char name[200];
public: void accept_person_details()
{ cout<<"n -------------------------------------------------- n";
cout<<"n Enter Id : “ ; cin>>id;
cout<<"n Enter Name : "; cin>>name;
}
void display_person_details()
{ cout<<"n -------------------------------------------------- n";
cout<<"n Id : "<<id;
cout<<"n Name : "<<name;
} };
class Teaching : public Person
{ char subject_name[100]; teacher_name[200];
public:
void accept_teacher_details()
{ accept_person_details();
cout<<"n Enter Subject Name : "; cin>>subject_name;
cout<<"n Enter Teacher Name : "; cin>>teacher_name;
}
void display_teacher_details()
{
display_person_details();
cout<<"n Subject Name : "<<subject_name;
cout<<"n Teacher Name : "<<teacher_name;
} };
class NonTeaching : public Person
{ char dept_name[200];
public:
void accept_nonteaching_details()
{ cout<<"n Enter Department Name : "; cin>>dept_name; }
void display_nonteaching_details()
{ cout<<"n Department Name : "<<dept_name; } };
class Instructor : public NonTeaching, public Teaching
{
public:
void accept_instructor_details()
{ accept_teacher_details(); accept_nonteaching_details(); }
void display_instructor_details()
{ display_teacher_details(); display_nonteaching_details(); } };
int main()
{
Instructor *inst;
int cnt, i;
cout<<"n Enter No. of Instructor Details You Want? : ";
cin>>cnt;
inst=new Instructor[cnt];
for(i=0; i<cnt; i++)
{ inst[i].accept_instructor_details(); }
for(i=0; i<cnt; i++)
{ inst[i].display_instructor_details(); }
return 0; }
Query
#include<iostream>
using namespace std;
class student //base class derivation
{ protected: int r_no;
public: void getRollno()
{ cout << "Enter the roll number of student : "; cin >> r_no; }
void putRollno() { cout << "nRoll Number -: " << r_no << "n"; } };
class test : public student //intermediate base class
{ protected: int part1, part2;
public: void getMarks() { cout << "Enter the marks of student in SA 1 : ";
cin >> part1; cout << "Enter the marks of student in SA 2 : ";
cin >> part2; }
void putMarks()
{ cout << "Marks Obtained : " << "n"; cout << " Part 1 -: " << part1;
cout << "n Part 2 -: " << part2 << "n"; } };
class sports
{ protected: int score;
public: void getSportsMarks()
{ cout << "Enter the marks in Physical Eduction : ";
cin >> score; }
void putSportsMarks()
{ cout << "Additional Marks : " << score << "n n"; } };
class result : public test, public sports //derived from test and sports
{ int total; public: void display () { total = part1 + part2 + score;
putRollno(); putMarks();
putSportsMarks(); cout << "Total Score : " << total ; } };
int main ()
{ result s1; s1.getRollno();
s1.getMarks(); s1.getSportsMarks();
s1.display();
return 0; }

labwork practice on inhetitance-1.pptx

  • 1.
    For example, ClassesB, C and D all contain the variables x, y and z class A { public: int x; protected: int y; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected A { // x is protected // y is protected // z is not accessible from C }; class D : private A // 'private' is default for classes { // x is private // y is private // z is not accessible from D };
  • 2.
    Example 1: C++public Inheritance #include <iostream> using namespace std; class Base { private: int pvt = 1; protected: int prot = 2; public: int pub = 3; // function to access private member int getPVT() { return pvt; }}; class PublicDerived : public Base { public: // function to access protected member from Base int getProt() { return prot; } }; int main() { PublicDerived object1; cout << "Private = " << object1.getPVT() << endl; cout << "Protected = " << object1.getProt() << endl; cout << "Public = " << object1.pub << endl; return 0; } Output Private = 1 Protected = 2 Public = 3
  • 3.
    // C++ programto demonstrate the working of protected inheritance #include<iostream> using namespace std; class Base { private: int pvt = 1; protected: int prot = 2; public: int pub = 3; // function to access private member int getPVT() { return pvt; } }; class ProtectedDerived : protected Base { public: // function to access protected member from Base int getProt() { return prot; } // function to access public member from Base int getPub() { return pub; }}; int main() { ProtectedDerived object1; cout << "Private cannot be accessed." << endl; cout << "Protected = " << object1.getProt() << endl; cout << "Public = " << object1.getPub() << endl; return 0; } Output Private cannot be accessed. Protected = 2 Public = 3
  • 4.
    // C++ programto demonstrate the working of private inheritance #include <iostream> using namespace std; class Base { private: int pvt = 1; protected: int prot = 2; public: int pub = 3; // function to access private member int getPVT() { return pvt; }}; class PrivateDerived : private Base { public: // function to access protected member from Base int getProt() { return prot; } // function to access private member int getPub() { return pub; }}; int main() { PrivateDerived object1; cout << "cannot be accessed." << endl; cout << "Protected = " << object1.getProt() << endl; cout << "Public = " << object1.getPub() << endl; return 0;} Output Private cannot be accessed. Protected = 2 Public = 3
  • 5.
    Single inheritance Example1 #include <iostream> // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; } When the above code is compiled and executed, it produces the following result − Total area: 35
  • 6.
    Example 2 # include<iostream.h> class Value { protected: int val; public: void set_values (int a) { val=a;} }; class Cube: public Value { public: int cube() { return (val*val*val); } }; int main () { Cube cub; cub.set_values (5); cout << "The Cube of 5 is::" << cub.cube() << endl; return 0; } Result: The Cube of 5 is:: 125 In the above example the derived class "Cube" has only one base class "Value". This is the single inheritance OOP's concept.
  • 7.
    C++ program forSingle inheritance #include <iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; // sub class derived from two base classes class Car: public Vehicle { ------- }; // main function int main() { /* creating object of sub class will invoke the constructor of base classes */ Car obj; return 0; } Output: This is a vehicle
  • 8.
    program to explainmultiple inheritance #include <iostream.h> // first base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; // second base class class FourWheeler { public: FourWheeler() { cout << "This is a 4 wheeler Vehicle" << endl; } }; // sub class derived from two base classes class Car: public Vehicle, public FourWheeler { }; int main() { /* creating object of sub class will invoke the constructor of base classes */ Car obj; return 0; } Output: This is a Vehicle This is a 4 wheeler Vehicle
  • 9.
    program to explainmultiple inheritance #include <iostream> using namespace std; // Base class Shape class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // Base class PaintCost class PaintCost { public: int getCost(int area) { return area * 70; } }; // Derived class class Rectangle: public Shape, public PaintCost { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; } When the above code is compiled and executed, it produces the following result − Total area: 35 Total paint cost: $2450
  • 10.
    Example: #include <iostream.h> using namespacestd; class Square { protected: int l; public: void set_values (int x) { l=x;} }; class CShow { public: void show(int i); }; void CShow::show (int i) { cout << "The area of the square is::" << i << endl; } class Area: public Square, public CShow { public: int area() { return (l *l); } }; int main () { Area r; r.set_values (5); r.show(r.area()); return 0; } Result: The area of the square is:: 25
  • 11.
    program to explainmultlevel inheritance #include <iostream.h> // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; // sub class derived from vehicle base class class fourWheeler: public Vehicle { public: fourWheeler() { cout<<"Objects with 4 wheels are vehicles"<<endl; } }; // sub class derived from two base classes class Car: public fourWheeler{ public: car() { cout<<"Car has 4 Wheels"<<endl; } }; int main() { //creating object of sub class will invoke the constructor of base classes Car obj; return 0; } output: This is a Vehicle Objects with 4 wheels are vehicles Car has 4 Wheels
  • 17.
    #include <iostream.h> class mm { protected: introllno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:n"<< rollno << "n"; } }; class marks : public mm { protected: int sub1; int sub2; public: void get_marks(int x,int y) { sub1 = x; sub2 = y; }
  • 18.
    void put_marks(void) { cout<< "Subject 1:" << sub1 << "n"; cout << "Subject 2:" << sub2 << "n"; } }; class res : public marks { protected: float tot; public: void disp(void) { tot = sub1+sub2; put_num(); put_marks(); cout << "Total:"<< tot; } }; int main() { res std1; std1.get_num(5); std1.get_marks(10,20); std1.disp(); return 0; } Result: Roll Number Is: 5 Subject 1: 10 Subject 2: 20 Total: 30 In the above example, the derived function "res" uses the function "put_num()" from another derived class "marks", which just a level above. This is the multilevel inheritance OOP's concept in C++.
  • 22.
    Hierarchical Inheritance In thistype of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.
  • 23.
    // C++ programto implement Hierarchical Inheritance #include <iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; // first sub class class Car: public Vehicle { ---------- }; // second sub class class Bus: public Vehicle { }; int main() { // creating object of sub class will invoke the constructor of base class Car obj1; Bus obj2; return 0; } Output: This is a Vehicle This is a Vehicle
  • 24.
    #include <iostream.h> class Side { protected: intl; public: void set_values (int x) { l=x;} }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube: public Side { public: int cub() { return (l*l*l); } }; Example: int main () { Square s; s.set_values (10); cout << "The square value is::" << s.sq() << endl; Cube c; c.set_values (20); cout << "The cube value is::" << c.cub() << endl; return 0; } Result: The square value is:: 100 The cube value is::8000
  • 25.
    Get square andcube of a number by using Hierarchical Inheritance Class Number { private: int num; public: void getNumber(void) { cout << "Enter integer number: "; cin >> num; } int returnNumber(void) { return num; } }; class Square:public Number { public: int getSquare(void) { int num,sqr; num=returnNumber(); sqr=num*num; return sqr; } }; class Cube:public Number { private: public: int getCube(void) { int num,cube; num=returnNumber(); cube=num*num*num; return cube; } }; int main() { Square objS; Cube objC; int sqr,cube; objS.getNumber(); sqr =objS.getSquare(); cout << "Square of "<< objS.returnNumber() << " is: " << sqr << endl; objC.getNumber(); cube=objC.getCube(); cout << "Cube of "<< objS.returnNumber() << " is: " << cube << endl; return 0; } Output Enter an integer number: 10 Square of 10 is: 100 Enter an integer number: 20 Cube of 10 is: 8000
  • 26.
    // C++ programto implement Hierarchical Inheritance #include <iostream.h> #include <string.h> class member { char gender[10]; int age; public: void get() { cout << "Age: "; cin >> age; cout << "Gender: "; cin >> gender; } void disp() { cout << "Age: " << age << endl; cout << "Gender: " << gender ; cout<< endl; } }; class stud : public member { char level[20]; public: void getdata() { member::get(); cout << "Class: "; cin >> level; } void disp2() { member::disp(); cout << "Level: " << level << endl; } }; class staff : public member { float salary; public: void getdata() { member::get(); cout << "Salary: Rs."; cin >> salary; } void disp3() { member::disp(); cout << "Salary: Rs." << salary << endl; }}; int main() { member M; staff S; stud s; cout << "Student" << endl; cout << "Enter data" << endl; s.getdata(); cout << endl << "Displaying data" << endl; s.disp(); cout << endl << "Staff Data" << endl; cout << "Enter data" << endl; S.getdata(); cout << endl << "Displaying data" << endl; S.disp(); }
  • 27.
  • 28.
    // C++ programto implement Hybrid Inheritance #include <iostream.h> // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl;} }; //base class class Fare { public: Fare() { cout<<"Fare of Vehiclen"; } }; // first sub class class Car: public Vehicle { }; // second sub class class Bus: public Vehicle, public Fare { }; int main() { Bus obj2; return 0; } Output: This is a Vehicle Fare of Vehicle
  • 29.
    EXAMPLE #include <iostream.h> class mm { protected: introllno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:"<< rollno << "n"; } }; class marks : public mm { protected: int sub1; int sub2; public: void get_marks(int x,int y) { sub1 = x; sub2 = y; } void put_marks(void) { cout << "Subject 1:" << sub1 << "n"; cout << "Subject 2:" << sub2 << "n"; } };
  • 30.
    class extra { protected: float e; public: voidget_extra(float s) {e=s;} void put_extra(void) { cout << "Extra Score::" << e << "n";} }; class res : public marks, public extra{ protected: float tot; public: void disp(void) { tot = sub1+sub2+e; put_num(); put_marks(); put_extra(); cout << "Total:"<< tot; } }; int main() { res std1; std1.get_num(10); std1.get_marks(10,20); std1.get_extra(33.12); std1.disp(); return 0; } Result: Roll Number Is: 10 Subject 1: 10 Subject 2: 20 Extra score:33.12 Total: 63.12
  • 31.
    • In theabove example the derived class "res" uses the function "put_num()". Here the "put_num()" function is derived first to class "marks". Then it is derived and used in class "res". This is an example of "multilevel inheritance-OOP's concept". But the class "extra" is inherited a single time in the class "res", an example for "Single Inheritance". Since this code uses both "multilevel" and "single" inheritance it is an example of "Hybrid Inheritance".
  • 32.
  • 33.
    #include<iostream.h> class Person { intid; char name[200]; public: void accept_person_details() { cout<<"n -------------------------------------------------- n"; cout<<"n Enter Id : “ ; cin>>id; cout<<"n Enter Name : "; cin>>name; } void display_person_details() { cout<<"n -------------------------------------------------- n"; cout<<"n Id : "<<id; cout<<"n Name : "<<name; } }; class Teaching : public Person { char subject_name[100]; teacher_name[200]; public: void accept_teacher_details() { accept_person_details(); cout<<"n Enter Subject Name : "; cin>>subject_name; cout<<"n Enter Teacher Name : "; cin>>teacher_name; }
  • 34.
    void display_teacher_details() { display_person_details(); cout<<"n SubjectName : "<<subject_name; cout<<"n Teacher Name : "<<teacher_name; } }; class NonTeaching : public Person { char dept_name[200]; public: void accept_nonteaching_details() { cout<<"n Enter Department Name : "; cin>>dept_name; } void display_nonteaching_details() { cout<<"n Department Name : "<<dept_name; } }; class Instructor : public NonTeaching, public Teaching { public: void accept_instructor_details() { accept_teacher_details(); accept_nonteaching_details(); } void display_instructor_details() { display_teacher_details(); display_nonteaching_details(); } };
  • 35.
    int main() { Instructor *inst; intcnt, i; cout<<"n Enter No. of Instructor Details You Want? : "; cin>>cnt; inst=new Instructor[cnt]; for(i=0; i<cnt; i++) { inst[i].accept_instructor_details(); } for(i=0; i<cnt; i++) { inst[i].display_instructor_details(); } return 0; }
  • 36.
  • 37.
    #include<iostream> using namespace std; classstudent //base class derivation { protected: int r_no; public: void getRollno() { cout << "Enter the roll number of student : "; cin >> r_no; } void putRollno() { cout << "nRoll Number -: " << r_no << "n"; } }; class test : public student //intermediate base class { protected: int part1, part2; public: void getMarks() { cout << "Enter the marks of student in SA 1 : "; cin >> part1; cout << "Enter the marks of student in SA 2 : "; cin >> part2; } void putMarks() { cout << "Marks Obtained : " << "n"; cout << " Part 1 -: " << part1; cout << "n Part 2 -: " << part2 << "n"; } };
  • 38.
    class sports { protected:int score; public: void getSportsMarks() { cout << "Enter the marks in Physical Eduction : "; cin >> score; } void putSportsMarks() { cout << "Additional Marks : " << score << "n n"; } }; class result : public test, public sports //derived from test and sports { int total; public: void display () { total = part1 + part2 + score; putRollno(); putMarks(); putSportsMarks(); cout << "Total Score : " << total ; } }; int main () { result s1; s1.getRollno(); s1.getMarks(); s1.getSportsMarks(); s1.display(); return 0; }