inheritance: base and derived classes

50
Inheritance: Base and Derived Classes

Upload: kalei

Post on 12-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

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.

• 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.

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

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;}

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; }

}; 

Types Of Inheritance

• Single Inheritance

• Multiple Inheritance

• Multi-level Inheritance

• Hierarchical Inheritance

• Hybrid Inheritance

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

• 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

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; }

};

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; }

};

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.

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.

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.

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.

Types Of Inheritance

Class A

Class B

Class A{};

Class B : public A{

};

Single Inheritance

Multi-level Inheritance

Class A

Class B

Class C

Public/private

Public/private

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

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

Example for Access control in multi-level inheritance

1. A B C private public

XYZ

ABC

MNP

XYZ

A,Y,ZBC

MN,BP,C

Example for Access control in multi-level inheritance

1. A B C private private

XYZ

ABC

MNP

XYZ

A,Y,ZBC

M,B,CNP

Multiple Inheritance

Class A Class B

Class C

Class A{};

Class B{};

Class C:public A , public B {};

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

Example for Access control in multiple inheritance

A B private C public

XYZ

ABC

MNP

XYZ

ABC

M,B,CN,YP, Z

Hierarchical Inheritance

Class A

Class B Class C Class D

Class GClass FClass E

Hybrid Inheritance

Class A

Class B Class C Class D

Class G

Class FClass E

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.

Multiple Inheritance

Class A Class B

Class C

Class A{};

Class B{};

Class C:public A , public B {};

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

Hybrid Inheritance

Class A Class B

Class C

Class D

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;}

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;}

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;}

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;}

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;}

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;}

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;

}

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;}};

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;}

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;}

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;}

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;}

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;}

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;}

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;}

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()

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;

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.

Virtual Class

Hybrid Inheritance

Class A

Class B

Class D

Class C

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

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