inheritance: base and derived classes. introduction in dictionary inheritance is defined as the...
TRANSCRIPT
![Page 1: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/1.jpg)
Inheritance: Base and Derived Classes
![Page 2: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/2.jpg)
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.
![Page 3: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/3.jpg)
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
![Page 4: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/4.jpg)
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;}
![Page 5: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/5.jpg)
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; }
};
![Page 6: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/6.jpg)
Types Of Inheritance
• Single Inheritance
• Multiple Inheritance
• Multi-level Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
![Page 7: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/7.jpg)
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; }
};
![Page 8: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/8.jpg)
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
private protected public
private - - -
protected private protected protected
public private protected public
Type of Inheritance
Access C
ontrolfor M
embers
![Page 9: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/9.jpg)
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; }
};
![Page 10: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/10.jpg)
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; }
};
![Page 11: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/11.jpg)
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 Cprivate: int a;public: void Set_a()
Class E: friend Class Cprivate: int num;public: void Set_num()
• void Set_a() and Class E can access the private data member, a which belongs to Class C.
![Page 12: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/12.jpg)
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 Cprotected: int a;public: void Set_a()
Class E: friend Class Cprivate: int num;public: void Set_num()
Class F: friend Class Dprivate: int numF;public: void Set_numF()
Class Dprivate: 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.
![Page 13: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/13.jpg)
Access Control
• If a member is public it can be used everywhere without restrictions.
Class Cpublic: int a;public: void Set_a()
int a and void Set_a can be accessed everywhere.• int a and void Set_a can be accessed everywhere.
t a and void Set_a can be accessed everywhere.
![Page 14: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/14.jpg)
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.
![Page 15: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/15.jpg)
Types Of Inheritance
Class A
Class B
Class A{};
Class B : public A{
};
Single Inheritance
![Page 16: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/16.jpg)
Multi-level Inheritance
Class A
Class B
Class C
Public/private
Public/private
![Page 17: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/17.jpg)
Example for Access control in multi-level inheritance
1. A B C public public
XYZ
ABC
MNP
XYZ
AB, YC,Z
MN, B,YP, C, Z
![Page 18: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/18.jpg)
Example for Access control in multi-level inheritance
1. A B C public private
XYZ
ABC
MNP
XYZ
AB, YC,Z
M,B,Y,C,ZNP
![Page 19: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/19.jpg)
Example for Access control in multi-level inheritance
1. A B C private public
XYZ
ABC
MNP
XYZ
A,Y,ZBC
MN,BP,C
![Page 20: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/20.jpg)
Example for Access control in multi-level inheritance
1. A B C private private
XYZ
ABC
MNP
XYZ
A,Y,ZBC
M,B,CNP
![Page 21: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/21.jpg)
Multiple Inheritance
Class A Class B
Class C
Class A{};
Class B{};
Class C:public A , public B {};
![Page 22: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/22.jpg)
Example for Access control in multiple inheritance
A B public C Indicates public
XYZ
ABC
MNP
XYZ
ABC
MN, B,YP, C, Z
privateProtected public
![Page 23: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/23.jpg)
Example for Access control in multiple inheritance
A B private C public
XYZ
ABC
MNP
XYZ
ABC
M,B,CN,YP, Z
![Page 24: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/24.jpg)
Hierarchical Inheritance
Class A
Class B Class C Class D
Class GClass FClass E
![Page 25: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/25.jpg)
Hybrid Inheritance
Class A
Class B Class C Class D
Class G
Class FClass E
![Page 26: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/26.jpg)
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.
![Page 27: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/27.jpg)
Multiple Inheritance
Class A Class B
Class C
Class A{};
Class B{};
Class C:public A , public B {};
![Page 28: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/28.jpg)
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;}
OutputB’s ConstructorA’s ConstructorC’s ConstructorC’s DestructorA’s DestructorB’s Destructor
![Page 29: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/29.jpg)
Hybrid Inheritance
Class A Class B
Class C
Class D
![Page 30: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/30.jpg)
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”;}}};
OutputB’s ConstructorA’s ConstructorC’s ConstructorD’s ConstructorD’s DEstructorC’s DestructorA’s DestructorB’s Destructor
int main(){ D obj; return 0;}
![Page 31: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/31.jpg)
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;}
![Page 32: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/32.jpg)
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;}
![Page 33: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/33.jpg)
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;}
![Page 34: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/34.jpg)
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;}
![Page 35: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/35.jpg)
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;}
![Page 36: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/36.jpg)
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;
}
![Page 37: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/37.jpg)
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;}};
![Page 38: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/38.jpg)
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 baseobj.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;}
![Page 39: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/39.jpg)
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;}
![Page 40: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/40.jpg)
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;}
![Page 41: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/41.jpg)
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;}
![Page 42: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/42.jpg)
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;}
![Page 43: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/43.jpg)
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;}
![Page 44: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/44.jpg)
Virtual functionClass 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;}
![Page 45: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/45.jpg)
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 Science commerce
ShapeDraw()
CircleDraw()
RectangleDraw()
TriangleDraw()
![Page 46: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/46.jpg)
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
• Declarationvirtual draw() = 0;
![Page 47: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/47.jpg)
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.
![Page 48: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/48.jpg)
Virtual Class
Hybrid Inheritance
Class A
Class B
Class D
Class C
![Page 49: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/49.jpg)
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::d
a
![Page 50: Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf761a28abf838c80c46/html5/thumbnails/50.jpg)
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