INHERITANCE PROGRAMS 1. Single Inheritance Public Derivation class base { int a; public: int b; void getdata(void); void putdata(void); int get_value(void); void put_value(void); }; class derived:public base //Public derivation { int c; public: void sum(void); void display(void); }; void base::getdata(void) { cout<<"Enter a and b"; cin>>a>>b; } void base::putdata(void) { cout<<"a="<
1
/* Data member 'int a;' is private in base class and can not be inherited, objects of derived class are able to access through an inherited member function of base class (get_value () function) */ 2. Single Inheritance Private Derivation class base { int a; public: int b; void getdata(void); void putdata(void); int get_value(void); void put_value(void); }; class derived:private base //Private derivation { int c; public: void sum(void); void display(void); }; void base::getdata(void) { cout<<"Enter a and b"; cin rel="nofollow">>a>>b; } void base::putdata(void) { cout<<"a="<
2
/* dobj1.b=20; gives error; data member 'b' has become private. dobj1.getdata();dobj1.putdata();dobj1.get_value(); dobj1.put_value();gives error. All member functions of base class become private and they cannot be accessed by Objects of derived class.*/ 3. Single Inheritance Same member function in base class derived class class base { int a; public: int b; void getdata(void); void putdata(void) { cout<<"base a="<
>a>>b; } int base::get_value(void) { return (a); } void base::put_value(void) { cout<<"Base Class Private Member a="<
3
}
/* When a base class and a derived class define a function of same name the derived class function supersedes the base class definition. the base class function will be called only if the derived class does not redefine the function*/ 4. Single Inheritance Same member function in base class derived class class base { int a; public: int b; void getdata(void); void putdata(void); int get_value(void); void put_value(void); }; class derived:public base //Public derivation { int c; public: void sum(void); void putdata(void);// same function name }; void base::getdata(void) { cout<<"Enter a and b"; cin rel="nofollow">>a>>b; } void base::putdata(void) { cout<<"base a="<
4
5.Single Inheritance Public Derivation class base { int a,b; public: int c; void getdata(void); void putdata(void); int get_sum(void); int geta() { return a; } int getb() { return b; } void put_value(void); }; class derived:public base //Public derivation { public: int d; void sum(void); void display(void); }; void base::getdata(void) { cout<<"Enter a b and c"; cin rel="nofollow">>a>>b>>c; } void base::putdata(void) { cout<<"a="<
5
6.Single Inheritance Private Derivation class base { int a,b; public: int c; void getdata(void); void putdata(void); int get_sum(void); int geta() { return a; } int getb() { return b; } void put_value(void); }; class derived:private base //Private derivation { public: int d; void sum(void); void display(void); }; void base::getdata(void) { cout<<"Enter a b and c"; cin rel="nofollow">>a>>b>>c; } void base::putdata(void) { cout<<"a="<
6
/* dobj1.getdata(); gives error. Derived class is inherited in private mode. data members and member functions of base class become private.*/ 7. Single Inheritance Public Derivation: Protected access specifier class base { int a; protected: int b; public: int c; void getdata(void); void putdata(void); int get_sum(void); int geta() { return a; } /* int getb() { return b; } */ void put_value(void); }; class derived:public base //Public derivation { public: int d; void sum(void); void display(void); }; void base::getdata(void) { cout<<"Enter a b and c"; cin rel="nofollow">>a>>b>>c; } void base::putdata(void) { cout<<"a="<
7
int main() { derived dobj1; dobj1.getdata(); dobj1.putdata(); dobj1.put_value(); dobj1.sum(); dobj1.display(); // dobj1.b=20; //not possible dobj1.c=15; //Public member of base class dobj1.sum(); dobj1.display(); return(0); } 8. Single Inheritance Private Derivation: Protected access specifier class base { int a; protected: int b; public: int c; void getdata(void); void putdata(void); int get_sum(void); int geta() { return a; } /* int getb() { return b; } */ void put_value(void); }; class derived:private base //Private derivation { public: int d; void sum(void); void display(void); }; void base::getdata(void) { cout<<"Enter a b and c"; cin rel="nofollow">>a>>b>>c; } void base::putdata(void) { cout<<"a="<
8
int base::get_sum(void) { return(a); } void base::put_value(void) { cout<<"Base Class Private Member a="<
>a; } void one::display_a(void){ cout<<"a="<
9
cin rel="nofollow">>b>>c;
}
void display_values(void) }; class three:public two { float d; public: void display(void) { d=c+b; display_a(); display_values(); cout<<"d="<
{ cout<<"b="<
10. Multilevel Inheritance class one { protected: int a; public: void get_a(void); void display_a(void); }; void one::get_a(void){ cout<<"Enter a"; cin>>a; } void one::display_a(void){ cout<<"a="<
>b>>c; }
10
}
void display_values(void) { display_a(); cout<<"b="<>a; } void one::display_a(void){ cout<<"a="<
11
class two : private one { protected: int b; float c; public: void get_values(void) { get_a(); cout<<"Enter b(int) and c(float) values"; cin rel="nofollow">>b>>c; } void display_values(void) { display_a(); cout<<"b="<
12
12. Multilevel Inheritance :derived classes in private mode class one { protected: int a; public: void get_a(void); void display_a(void); }; void one::get_a(void){ cout<<"Enter a"; cin>>a; } void one::display_a(void){ cout<<"a="<>b>>c; } void display_values(void) { display_a(); cout<<"b="<
13
// obj.get_a(); //obj.get_values(); obj.display(); return(0); } /* class two is derived in private mode. All the members (protected and public) of base class one become private members of class two. The members of class one cannot be accessed by class three directly. Class three is derived in private mode. All the members’ of class two become private. The members of class two cannot be accessed by class three directly.*/ 13. Multiple Inheritance: Public class one { protected: int a; public: void get_a(void); }; void one::get_a(void){ cout<<"Enter a"; cin>>a; } class two { protected: int b; public: void get_b(void) { cout<<"Enter b"; cin>>b; }; class three:public one,public two { public: void display(void) { cout<<"a="<
14
}
14. Multiple Inheritance: Public, Private class one { protected: int a; public: void get_a(void); }; void one::get_a(void){ cout<<"Enter a"; cin rel="nofollow">>a; } class two { protected: int b; public: void get_b(void) { cout<<"Enter b"; cin>>b; } }; class three:public one,private two { public: void display(void) { get_b(); cout<<"a="<
15
int main() { C obj1; obj1.disp();return(0);} /* Ambiguity occurs (error) display(); - rel="nofollow"> A::display(); or B::display(); */ 16.Ambiguity resolution in Single Inheritance class A { public: void display(void) { cout<<"Class A"<<endl; } }; class B: public A { public: void display(void) { cout<<"Class B"<<endl; } }; int main() { B obj1; obj1.display(); obj1.A::display(); obj1.B::display(); } /* Ambiguity occurs (error) display (); -> obj1.A:: display(); or obj1.B::display(); */
16