Derived Class & Base Class Class Hierarchies. - Public & Private Inheritance. - Level of Inheritance, Multiple Inheritance
The document discusses inheritance in C++ including:
1) Different types of inheritance like single, multiple, hierarchical, and multilevel inheritance.
2) Public and private inheritance and how they control accessibility of members of the base class.
3) An example demonstrating public and private inheritance with classes A, B, and C where B publicly inherits from A and C privately inherits from A.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
47 views
Derived Class & Base Class Class Hierarchies. - Public & Private Inheritance. - Level of Inheritance, Multiple Inheritance
The document discusses inheritance in C++ including:
1) Different types of inheritance like single, multiple, hierarchical, and multilevel inheritance.
2) Public and private inheritance and how they control accessibility of members of the base class.
3) An example demonstrating public and private inheritance with classes A, B, and C where B publicly inherits from A and C privately inherits from A.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10
1
- Derived class & base class
- Class Hierarchies. - Public & private inheritance. - Level of Inheritance, multiple inheritance
Inheritance 2 Single Inheritance Class inherits from one base class Multiple Inheritance Class inherits from multiple base classes
Inheritance 3 Multiple Inheritance Hierarchical Inheritance Multilevel Inheritance Hybrid(Virtual) Inheritance 4 Public: Derived objects are accessible by the base class objects (focus of this chapter)
Private: Derived objects are inaccessible by the base class
Protected: Derived classes and friends can access protected members of the base class Types of Inheritance 5 6 7 Class Hierarchies. 8 #include<iostream> const int LEN =80;
class employee { private : char name[LEN}; unsigned long number; public: void getdata() { cout << \n Enter last name ; cin>>name cout< Enter number : cin >>number ; } void putdata() const { cout <<\n Name : <<name; cout << \n Number : << number; } };
class manager : public employee { private : char title[LEN]; double dues; public : void getdata() { employee::getdata(); cout<< Enter title : ; cin>>title; cout<< Enter golf club dues:; cin>>dues; } void putdata() const { employee::putdata(); cout<< \n Title : <<title; cout<< \n Golf club dues : <<dues; } };
9 class scientist : public employee { private: int pubs; public : void getdata() { employee::getdata(); cout<< Enter number of pubs: ; cin>>pubs; } void putdata() const { employee::putdata(); cout<< \n Number of publications ; cout<<pubs; } };
class laborer : public employee { }; int main() { manager m1,m2; scientist s1; laborer l1; cout<<endl; cout<<\n Enter datad for manager 1; m1.getdata(); cout<<\n Enter datad for manager 2; m2.getdata(); cout<< \n enter the data for scientet 1; s1.getdata(); cout<<\n enter data for laborer 1; l1.getdata(); cout<<\nData for manager 1; m1.putdata(); cout<<\nData for manager 1; m2.putdata(); cout<<\nData for Scientist 1; s1.putdata(); cout<<\nData on laborer 1; l1.putdata(); return 0: } 10 Public and Private Inheritance class A { private : int privdataA; protected : int protdataA; public : int pudataA; };
class B : public A { public: void funct() { int a; a= privdataA; // error : not accessible; a= protdataA; // ok a= pubdataA; // ok } }; class C : private A { public: void funct() { int a; a= privdataA; // error : not accessible; a= protdataA; // ok a= pubdataA; // ok } }; int mai() { int a; B objB; a= objB.privdataA; // error: not accessible a= objB. protdataA ;// error: not accessible a= objB. pubdataA ; //ok C objC; a= objC.privdataA; // error: not accessible a= objC. protdataA ; // error: not accessible a= objC.pubdataA; // error: not accessible return 0; }