1 / 50

Inheritance: Base and Derived Classes

Inheritance: Base and Derived Classes. Introduction. In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive from a predecessor.

kalei
Download Presentation

Inheritance: Base and Derived Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance: Base and Derived Classes

  2. Introduction • In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive from a predecessor. • In programming language term, inheritance refers to objects inheriting properties(data members & member functions) of another class. • Advantage: code reusability. • Definition:Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say "A inherits from B". Objects of class A thus have access to attributes and methods of class B without having to redefine them. • Definition: If class A inherits from class B then B is called the superclass (or parent class) of A. A is called the subclass (or child class) of B.

  3. Syntax for deriving class ClassClassname: access specifier base1 class name , base2 class name { private : private data members; protected: protected data members; public: public data members; }; Access specifier can be - private,protected,public

  4. Example for protected int main() { base b_obj; b_obj.a= 10; // error not accessible b_obj.b= 20; error not accessible b_obj.c = 30; // will work since it is public data b_obj.getdata(); //will work -- public data b_obj.show(); // will work -- public data return 0; } class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a>>b>>c;} void show() { cout<<endl<<"Base Class show()"; cout<<endl<<a<<" "<<b<<" "<<c; } };

  5. Example with two classes class derived { private: int x; protected: int y; public: int z; void getdata() { cin>>x>>y>>z; } void show() { cout<<endl<<"derived class members"; cout<<endl<<x<<" "<<y<<" "<<z; } }; intmain() { clrscr(); base b_obj; b_obj.getdata(); b_obj.show(); derived d_obj; d_obj.getdata(); d_obj.show(); return 0; } class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a>>b>>c;} void show() { cout<<endl<<"Base Class show()"; cout<<endl<<a<<" "<<b<<" "<<c; } };

  6. Types Of Inheritance • Single Inheritance • Multiple Inheritance • Multi-level Inheritance • Hierarchical Inheritance • Hybrid Inheritance

  7. Example Single Inheritance(public) class derived : public base { private:int x; protected:int y; public:int z; void getdata() { cout<<"\n Enter data for base class"; base::getdata(); cin>>b>>c; cout<<"\n Enter data for derived class"; cin>>x>>y>>z; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; cout<<endl<<x<<" "<<y<<" "<<z; } }; intmain() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; cout<<d_obj.z; return 0; } class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } };

  8. Access Rights of Derived Classes Type of Inheritance • The type of inheritance defines the access level for the members of derived classthat are inherited from the base class Access Control for Members

  9. Example Single Inheritance(protected) class derived : protected base { private:int x; protected:int y; public:int z; void getdata() { cout<<"\n Enter data for base class"; base::getdata(); cin>>b>>c; cout<<"\n Enter data for derived class"; cin>>x>>y>>z; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; cout<<endl<<x<<" "<<y<<" "<<z; } }; intmain() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; //error cout <<d,obj.z; return 0; } class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } };

  10. Example Single Inheritance(private) class derived : private base { private:int x; protected:int y; public:int z; void getdata() { cout<<"\n Enter data for base class"; base::getdata(); cin>>b>>c; cout<<"\n Enter data for derived class"; cin>>x>>y>>z; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; cout<<endl<<x<<" "<<y<<" "<<z; } }; intmain() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; //error cout <<d,obj.z; return 0; } class base { class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } };

  11. Class C private: int a; public: void Set_a() Access Control • If a member is declared in a class C and is private, it can only be used by the member functions in C and by the friends of class C. Class E: friend Class C private: int num; public: void Set_num() • void Set_a() and Class E can access the private data member, a which belongs to Class C.

  12. Class C protected: int a; public: void Set_a() Class E: friend Class C private: int num; public: void Set_num() Class F: friend Class D private: int numF; public: void Set_numF() Class D private: int numD; public: void Set_numD() Access Control • If a member is declared in a class C and the member is protected, it can only be used by the member functions in C, friends of C and member functions and friends of classes derived from C. • void Set_a(),Class E, Class D, and Class F can access the private data member, a which belongs to Class C.

  13. Class C public: int a; public: void Set_a() Access Control • If a member is public it can be used everywhere without restrictions. • int a and void Set_acan be accessed everywhere. • int a and void Set_acan be accessed everywhere. • t a and void Set_acan be accessed everywhere.

  14. Access Control • A derived class cannot access directly the private members of its base class. • However, the derived class can access the public and protected member of its base class. • The derived class can only access private members of the base class only through access functions provided in the base class’s public and protected interfaces.

  15. Types Of Inheritance Single Inheritance Class A { }; Class B : public A { }; Class A Class B

  16. Multi-level Inheritance Class A Public/private Class B Public/private Class C

  17. Example for Access control in multi-level inheritance • A B C public public

  18. Example for Access control in multi-level inheritance • A B C public private

  19. Example for Access control in multi-level inheritance • A B C private public

  20. Example for Access control in multi-level inheritance • A B C private private

  21. Multiple Inheritance Class B Class A Class C Class A { }; Class B { }; Class C:public A , public B { };

  22. Example for Access control in multiple inheritance A B public C Indicates public

  23. Example for Access control in multiple inheritance A B private C public

  24. Hierarchical Inheritance Class A Class B Class C Class D Class E Class F Class G

  25. Hybrid Inheritance Class A Class B Class C Class D Class E Class F Class G

  26. Constructors and Destructors in derived classes • When derived class object is constructed ,then constructor of base class is called first and then constructor of derived class is called. • Destructor works in reverse way. i.e. derived class destructor is called first and then destructor of base class. • When class is derived from more than one base classes then calling constructor sequence depends on declaration of base classes. Constructors of base classes are called in left to right sequence whereas destructors are invoked in right to left sequence.

  27. Multiple Inheritance Class B Class A Class C Class A { }; Class B { }; Class C:public A , public B { };

  28. Class C:public B , private A { public: C() {cout<<“C’s Constructor”;} ~C() { Cout<<“C’s Destructor”;} } }; int main() { C obj; return 0; } Class A{ public: A() {cout<<“A’s Constructor”;} ~A() { Cout<<“A’s Destructor”;} } }; Class B{ public: B() {cout<<“B’s Constructor”;} ~B() { Cout<<“B’s Destructor”;} } }; Output B’s Constructor A’s Constructor C’s Constructor C’s Destructor A’s Destructor B’s Destructor

  29. Hybrid Inheritance Class B Class A Class C Class D

  30. Class C:public B , private A { public: C() {cout<<“C’s Constructor”;} ~C() { Cout<<“C’s Destructor”;} } }; Class D:public C{ public: D() {cout<<“D’s Constructor”;} ~D() { Cout<<“D’s Destructor”;} } }; int main() { D obj; return 0; } Class A{ public: A() {cout<<“A’s Constructor”;} ~A() { Cout<<“A’s Destructor”;} } }; Class B{ public: B() {cout<<“B’s Constructor”;} ~B() { Cout<<“B’s Destructor”;} } }; Output B’s Constructor A’s Constructor C’s Constructor D’s Constructor D’s DEstructor C’s Destructor A’s Destructor B’s Destructor

  31. Explicit call to base class Constructor through derived class class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj; d_obj.show(); return 0; } class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int n1,int n2,int n3) { a=n1; b=n2; c=n3; } void show() { cout<<endl<<a; } };

  32. Explicit call to base class Constructor through derived class class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj(10,20,30); d_obj.show(); return 0; } class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int n1,int n2,int n3) { a=n1; b=n2; c=n3; } void show() { cout<<endl<<a; } };

  33. Explicit call to base class Constructor through derived class class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3):base(n1,n2,n3) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj(10,20,30); d_obj.show(); return 0; } class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int n1,int n2,int n3) { a=n1; b=n2; c=n3; } void show() { cout<<endl<<a; } };

  34. Explicit call to base class Constructor through derived class class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3):base(5,6,7) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj(10,20,30); d_obj.show(); return 0; } class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int p1,int p2,int p3) { a=p1; b=p2; c=p3; } void show() { cout<<endl<<a; } };

  35. Explicit call to base class Constructor through derived class class derived : private base { private: int x; protected: int y; public: int z; derived() {x=y=z=0;} derived(int n1,int n2,int n3,int n4,int n5,int n6):base(n4,n5,n6) { x=n1; y=n2; z=n3; } void show() { cout<<endl<<"Base Class members"; base::show(); cout<<" "<<b<<" "<<c; cout<<endl<<"derived class members"; / cout<<endl<<x<<" "<<y<<" "<<z; } }; int main() { derived d_obj(10,20,30,40,50,60); d_obj.show(); return 0; } class base { private: int a; protected: int b; public: int c; base() { a=b=c=0;} base(int p1,int p2,int p3) { a=p1; b=p2; c=p3; } void show() { cout<<endl<<a; } };

  36. Concept Of Overloading and Over-riding • More than one Member functions with same name within a class is called as Overloading. e.g. Class A { private: int a; public: intadd() { a= a+10;} intadd(int x){ a = a + x;} float add(float x) { a = a+x;} }; int main() { A Obj; Obj.add(); Obj.add(45); Obj.add(24.8); return 0; }

  37. Cont…. • More than one member functions with same name across the class (inheritance) is called as Over-riding. Class A { private : intda; public: A() { da=0;} A(int x) { da=x;} void show() {cout<<da;} }; Class B :public A { private : int db; public: B() { db=0;} B(int x) { db=x;} void show() {cout<<db;} };

  38. Pointer to Object int main() { Base obj(30); //obj is object of class base obj.show(); //fn invoke thr’ obj with dot Base *pb; // pb is pointer to Base class pb=&obj; //pointing to object of base class pb->show(); // fn invoke thr’ pointer with arrow return 0; } Class Base { int b; public: Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} };

  39. Derived class Pointer can point base class data member n function int main() { Base obj(30),*pb; pb=&obj; pb->b = 50; pb->show(); //will call base class show() Derived dobj(20),*pd; pd = &dobj; pd->b = 50; pd->d=70; pd->show(); // will call derived class show() return 0; } Class Base { public: int a; Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; Class Derived :public Base { public: int d; Derived() {d=0;} Derived(int x) { d=x;} void show() {cout<<b<<d;} };

  40. Derived class Pointer can notpoint to base class object int main() { Base obj(30),*pb; pb=&obj; pb->b = 50; pb->show(); //will call base class show() Derived dobj(20),*pd; pd = &obj; // error return 0; } Class Base { public: int a; Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; Class Derived :public Base { public: int d; Derived() {d=0;} Derived(int x) { d=x;} void show() {cout<<b<<d;} };

  41. Base class Pointer can point to derived class object int main() { Base *pb; Derived obj(30); pb=&obj; pb->b = 50; pb->d = 40; //error pb->show(); //will call base class show() return 0; } Class Base { public: int a; Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; Class Derived :public Base { public: int d; Derived() {d=0;} Derived(int x) { d=x;} void show() {cout<<b<<d;} };

  42. Base class Pointer can point to derived class data by type casting int main() { Base *pb; Derived obj(30); pb=&obj; pb->b = 50; ((derived *)pb)->d = 40; ((derived *) pb)->show(); //will call derived class //show() return 0; } Class Base { public: int a; Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; Class Derived :public Base { public: int d; Derived() {d=0;} Derived(int x) { d=x;} void show() {cout<<b<<d;} };

  43. IF Derived class does not have functiondefinition int main() { base b,*bptr; derived d; bptr = &b; bptr->show(); // base bptr = &d; bptr->show(); // base return 0; } Class base { public: void show() {cout<<“\n Base class show”;} }; Class derived:public base { int a; public: derived() { a = 10;} };

  44. Virtual function int main() { base b,*bptr; derived d; bptr = &b; bptr->display(); //base bptr->show(); // base bptr = &d; bptr->display(); //base // checks data type of caller bptr->show(); //derived //checks type of object at runtime return 0; } Class base { public: void display() {cout<<“\n Base class display”;} virtual void show() {cout<<“\n Base class show”;} }; Class derived:public base { public: void display() {cout<<“\n derived class display”;} void show() {cout<<“\n derived class show”;} };

  45. Abstract Class • An abstract class is one which is not use to create objects but its is designed to act as base class for other classes. e.g. Student Science commerce Arts Shape Draw() Triangle Draw() Circle Draw() Rectangle Draw()

  46. Pure Virtual function • It is normal practice to declare a function as virtual in base class and redefine it in derived classes. • The function inside the base class serves only as place holder and such functions are called do nothing functions • Declaration virtual draw() = 0;

  47. Cont….. • A Pure virtual function is a function declared in base class that has no definition(code) relative to the base class. • Class containing pure virtual function can not be used to declare any objects of its own. e.g. Abstract Class • The main objective of an abstract class and pure virtual function is to achieve runtime polymorphism.

  48. Virtual Class Hybrid Inheritance Class A Class B Class C Class D

  49. Example for virtual class Class C:public A { -------- --------- public: int dc; }; Class D:public B,public C { public: intdd; void show() {cout<<dd,<<dc<<db<<da;}//ambiguity error }; int main() { D obj; return 0; } dc,A::da da Class A { -------- --------- public: intda; void show(){cout<<da;} }; Class B:public A { -------- --------- public: int db; }; dd, A::da ,A::da db, A::da

  50. Example for virtual class Class C:virtual public A { -------- --------- public: int dc; }; Class D:public B,public C { public: intdd; void show() {cout<<dd,<<dc<<db<<da;} }; int main() { D obj; return 0; } dc,A::da da Class A { -------- --------- public: intda; void show(){cout<<da;} }; Class B:virtual public A { -------- --------- public: int db; }; dd, A::da db, A::da

More Related