Download presentation
Presentation is loading. Please wait.
Published byErick Bailey Modified over 9 years ago
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 Class Classname : 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 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; } }; 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; }
5
Example with two classes 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; } }; int main() { clrscr(); base b_obj; b_obj.getdata(); b_obj.show(); derived d_obj; d_obj.getdata(); d_obj.show(); return 0; } 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; } };
6
Types Of Inheritance Single Inheritance Multiple Inheritance Multi-level Inheritance Hierarchical Inheritance Hybrid Inheritance
7
Example Single Inheritance(public) class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } }; int main() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; cout<<d_obj.z; return 0; } 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; } };
8
8 The type of inheritance defines the access level for the members of derived class that are inherited from the base class Access Rights of Derived Classes privateprotectedpublic private--- protectedprivateprotected publicprivateprotectedpublic Type of Inheritance Access Controlfor Members
9
Example Single Inheritance(protected) class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } }; int main() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; //error cout <<d,obj.z; return 0; } 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; } };
10
Example Single Inheritance(private) class base { class base { private: int a; protected: int b; public: int c; void getdata() {cin>>a;} void show() { cout<<endl<<a; } }; int main() { derived d_obj; d_obj.getdata(); d_obj.show(); cout<<d_obj.c; //error cout <<d,obj.z; return 0; } 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; } };
11
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 C private: int a; public: void Set_a() 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
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. 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() void Set_a(),Class E, Class D, and Class F can access the private data member, a which belongs to Class C.
13
Access Control If a member is public it can be used everywhere without restrictions. Class C public: int a; public: void Set_a() int a and void Set_a can be accessed everywhere. t a and void Set_a can 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 Class A Class B Class A { }; Class B : public A { }; Single Inheritance
16
Multi-level Inheritance Class A Class B Class C Public/private
17
Example for Access control in multi-level inheritance 1.A B C public public X Y Z A B C M N P X Y Z A B, Y C,Z M N, B,Y P, C, Z
18
Example for Access control in multi-level inheritance 1.A B C public private X Y Z A B C M N P X Y Z A B, Y C,Z M,B,Y,C,Z N P
19
Example for Access control in multi-level inheritance 1.A B C private public X Y Z A B C M N P X Y Z A,Y,Z B C M N,B P,C
20
Example for Access control in multi-level inheritance 1.A B C private private X Y Z A B C M N P X Y Z A,Y,Z B C M,B,C N P
21
Multiple Inheritance Class A Class B 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 X Y Z A B C M N P X Y Z A B C M N, B,Y P, C, Z private Protected public
23
Example for Access control in multiple inheritance A B private C public X Y Z A B C M N P X Y Z A B C M,B,C N,Y P, Z
24
Hierarchical Inheritance Class A Class B Class CClass D Class GClass F Class E
25
Hybrid Inheritance Class A Class B Class CClass D Class G Class F Class E
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 A Class B Class C Class A { }; Class B { }; Class C:public A, public B { };
28
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”;} } }; Class C:public B, private A { public: C() {cout<<“C’s Constructor”;} ~C() { Cout<<“C’s Destructor”;} } }; int main() { C obj; return 0; } Output B’s Constructor A’s Constructor C’s Constructor C’s Destructor A’s Destructor B’s Destructor
29
Hybrid Inheritance Class A Class B Class C Class D
30
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”;} } }; 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”;} } }; 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 int main() { D obj; return 0; }
31
Explicit call to base class Constructor through derived class 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; } }; 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; }
32
Explicit call to base class Constructor through derived class 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; } }; 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; }
33
Explicit call to base class Constructor through derived class 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; } }; 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; }
34
Explicit call to base class Constructor through derived class 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; } }; 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; }
35
Explicit call to base class Constructor through derived class 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; } }; 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; }
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: int add() { a= a+10;} int add(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 : int da; 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 Class Base { int b; public: Base() {b=0;} Base(int x) { b=x;} void show() {cout<<b;} }; 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; }
39
Derived class Pointer can point base class data member n function 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;} }; 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; }
40
Derived class Pointer can not point to base class object 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;} }; 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; }
41
Base class Pointer can point to derived class object 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;} }; 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; }
42
Base class Pointer can point to derived class data by type casting 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;} }; 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; }
43
IF Derived class does not have function definition Class base { public: void show() {cout<<“\n Base class show”;} }; Class derived:public base { int a; public: derived() { a = 10;} }; int main() { base b,*bptr; derived d; bptr = &b; bptr->show(); // base bptr = &d; bptr->show(); // base return 0; }
44
Virtual function 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”;} }; 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; }
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 Arts Sciencecommerce Shape Draw() Circle Draw() Rectangle Draw() Triangle 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 D Class C
49
Example for virtual class Class A { -------- --------- public: int da; void show(){cout<<da;} }; Class B:public A { -------- --------- public: int db; }; Class C:public A { -------- --------- public: int dc; }; Class D:public B,public C { public: int dd; void show() {cout<<dd,<<dc<<db<<da;}//ambiguity error }; int main() { D obj; return 0; } da db, A::da dc,A::da dd, A::da,A::da
50
Example for virtual class Class A { -------- --------- public: int da; void show(){cout<<da;} }; Class B:virtual public A { -------- --------- public: int db; }; Class C:virtual public A { -------- --------- public: int dc; }; Class D:public B,public C { public: int dd; void show() {cout<<dd,<<dc<<db<<da;} }; int main() { D obj; return 0; } da db, A::da dc,A::da dd, A::da
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.