Inheritance

37
INHERITANCE

Transcript of Inheritance

Page 1: Inheritance

INHERITANCE

Page 2: Inheritance

DEFINATION

Inheritance is a mechanism of creating new classes called derived class from existing ones i.e. base classes.

Inheritance is a most powerful feature of object oriented programming.

Page 3: Inheritance

Types of Inheritance

Single Inheritance Multiple Inheritance Multilevel Inheritance Hierarchical Inheritance Hybrid Inheritance

Page 4: Inheritance

SINGLE INHERITANCE

When a derived class inherits only from one base class ,it is Known as single Inheritance.

Base Class

Derived Class

A

B

Page 5: Inheritance

When a derived class inherit from multiple base classes it is known as multiple Inheritance.

A B

C

MULTIPLE INHERITANCE

Base Class

Derived Class

Page 6: Inheritance

MULTILEVEL INHERITANCE

When a derived class inherit from a class that itself inherits from other class , it is known as multilevel Inheritance.

Base Class

Derived Class of x Base Class of z

Derived Class

A

B

C

Page 7: Inheritance

HYBRID INHERITANCE

Hybrid Inheritance combines two or more forms of Inheritance.

w

x y

z

Page 8: Inheritance

HIERARCHICAL INHERITANCE When many derived classes inherit from a

single base class , it is known as hierarchical Inheritance.

Base Class

Derived classes

a

b c d

Page 9: Inheritance

A general form to defining a derived class is:

Class derived class name: visibility mode base class name{ members of derived class} ;

Class ABC: public XYZ{ members of ABC};

DEFINING DERIVED CLASS

Page 10: Inheritance

Example of derived class definition is:

Class Sub : public Super \\ public derivation{ ……… \\ members of sub};

Class Sub: private Super \\ private derivation{……... \\ members of sub};

Class Sub: protected Super \\ protected derivation{……... \\ members of sub};

Page 11: Inheritance

MULTIPLE INHERITANCEExample of derived class definition is:

Class derived_class : vis_mode base1, vis_mode base 2{…………. \\ members of derived class};

Class Sub : public SuperA, private SuperB{ ……… \\ members of sub};

Page 12: Inheritance

Visibility Modes

DEFINATION:- Visibility Mode specifies whether the features

of the base class are privately derived or publicly derived or protected derived.

Public Visibility Mode- It means that the derived class can access the public and protected members of the base class. The public members of the base class become the public member of the derived class, and the protected member of the base class become the protected member of the derived class.

Page 13: Inheritance

Private Visibility Mode- The public and protected members of the base class become private members of the derived class. The inherited members can only be accessed only through the member function of derived class.

Protected Visibility Mode- The public and protected members of base class become protected members of the derived class.

Page 14: Inheritance

Visibility of Inherited base class members in Derived Class.

Visibility Visibility ModeMode

Public Public members members of base of base class class becomesbecomes

Protected Protected members members of base of base class class becomesbecomes

Private Private members members of the of the base class base class is not is not accessible accessible to the to the derived derived class.class.

PublicPublic PublicPublic ProtectedProtected

ProtectedProtected ProtectedProtected ProtectedProtected

PrivatePrivate PrivatePrivate PrivatePrivate

Page 15: Inheritance

Accessibility of Base class members Access Specifier

Accessible from own class

Accessible from derived class

Accessible from objects outside class

Public Yes Yes Yes

Protected Yes Yes No

Private Yes No No

Page 16: Inheritance

Inheritance and Constructors and Destructors

When an object of a derived class is created, the program first calls the constructor for base class.

When an object expires, the program first calls the derived destructor and then base destructor

Page 17: Inheritance

Class super

{

…….

};

Class Sub : public Super

{ …..

};

int main()

{

sub ob1;

…….

}

Page 18: Inheritance

Base class ConstructorsBase class constructor require arguments to construct their objects.

It is the responsibility of derived class constructor to pass those arguments to base class.

Syntax :

Derived ::Derived (type1 x, type2 y ….) : base (x , y)

{

…..

}

Page 19: Inheritance

Class Base

{ int a;

float b;

public :

Base(int I, float j)

{ a = i;

b = j;

}

};

Class Derived : public Base

{….

Public :

Derived ( int p, float q) : Base (p , q)

{ }

};

Even if derived const does not need a parameter, yet it accepts parameter for base const.

Page 20: Inheritance

Class Base

{ int a;

float b;

public :

Base( int i, float j)

{ a = i;

b = j;

}

};

Class Derived : public Base

{ int x; float y;

Public :

Derived ( int i, int j , int p, float q) : Base (p , q)

{ x = i ;

Y = j ;

}

};

Derived Const is accepting parameter for itself( i ,j) and ( p , q) for Base const

Page 21: Inheritance

Constructor in Derived Class

# include<iostream.h> class alpha{

protected: int x;

alpha(int i){ x=i;}}; class beta{

protected: float y;

Beta(float j){

y=j;}};

Page 22: Inheritance

Class gamma: public beta, public alpha{

int k;public:

gamma(int a,float b, int c): alpha(a) , beta(b){

k=c;}void show(){

cout<<“1”<<x<<“2”<<y<<“3”<<k;}};

void main(){

gamma g(14,15.2,17);

g.show();

}

Page 23: Inheritance

Facts about inheritance Private members of a base class are not directly accessible in the derived class.

Example

int test; \\ Global variable

Class base { int test; \\ private members of base

public:

void getit()

{ cin>> test;

}

};

Class derived : public base

{ public :

void check ( )

{ test++; } \\ not allowed

};

Page 24: Inheritance

When a class is derived publicly, you cannot selectively deny access to base members by declaring them in derived private section of the derived class.

Example

Class base {

Public :

int x,y,z;

};

Class derived : public base

{

Public:

int a;

Private :

Base :: x; // Invalid

}

Page 25: Inheritance

When you derive a class privately, you can selectively allow access to some of the base members. To allow access to some of the inherited members, you can selectively declare some of the base class members in the public section of the privately derived class.

Example Class base {Public :int x,y,z;};Class derived : private base { Public:int a;Base :: x; \\Valid}

Or

Public :

Using Base :: x ;

Page 26: Inheritance

Abstract class

A class that serves only as a base class from which other classes can be derived, but no objects of this base type exist, is known as abstract class.

Page 27: Inheritance

Constructors in Multiple inheritance As long as no base class constructor takes any arguments, the derived class need not have a constructor function. However, if a base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base constructor.

In multiple inheritance , the base classes are constructed in the order in which they appear in the declaration of the derived class.

Example

Class Base1

{ protected :

int a;

public :

Base1(int x)

{ a = x; cout<<“Constructing Base 1”; }

~Base1( )

{ cout<<“Destructing Base1”; }

};

Page 28: Inheritance

Class base2

{ Protected :

int b;

public :

Base2(int y)

{ b = y; cout<<“Constructing Base2”; }

~Base2 ( )

{ cout<< Destructing Base2 “; }

};

Class derived : public Base2, public Base1

{ int c;

public :

derived (int I, int j, int k): Base2(i), Base1(j)

{ c = k; cout <<“Constructing Derived”; }

Page 29: Inheritance

~ Derived ( ){ cout << Destructing Derived “; }Void show( ){ cout <<“1.”<<a<<“\t 2.”<<b <<“\t 3.”<<c; }};Int main ( ){ Clrscr();Derived ob(14,15,16);Ob.show();}

OUTPUT :Constructing Base2Constructing Base1Constructing Derived1. 15 2. 14 3. 16Destructing Derived Destructing Base1Destructing Base2

Page 30: Inheritance

Virtual Base classExample Class Base { public : int a ;};Class D1 : public Base \\ D1 inherits Base{ public :

int b ;};Class D2 : public Base \\ D2 inherits Base{ public :

int c ;};Class D3 : public D1, public D2 \\ D3 inherits D1 and D2{ public :

int total;};Void main ( ){ D3 ob;ob.a = 25 \\ this is ambiguousOb.b = 50;Ob.c = 75;Ob.total = ob.a +ob.b + ob.c;Cout <<ob.a<<“\t”<< ob.b<<“\t”<< ob.c<<“\t”<< ob.total<<“\t”<<“\n”;}

Page 31: Inheritance

To Use Scope resolution operator to a

Void main ( ){ D3 ob;ob.D1 :: a = 25 \\ scope resolvedOb.b = 50;Ob.c = 75;Ob.total = ob.D1 :: a +ob.b + ob.c;Cout <<ob.D1 :: a<<“\t”<< ob.b<<“\t”<< ob.c<<“\t”<< ob.total<<“\t”<<“\n”;}

Remedy

Page 32: Inheritance

To Use a keyword Virtual

Example

Class Base { public : int a ;};Class D1 : virtual public Base \\ D1 inherits Base as Virtual { public :

int b ;};Class D2 : virtual public Base { public :

int c ;};Class D3 : public D1, public D2 \\ this time only one copy of base{ public :

int total;};

Page 33: Inheritance

Void main ( ){ D3 ob;ob.a = 25 \\ now unambiguousOb.b = 50;Ob.c = 75;Ob.total = ob.a +ob.b + ob.c;Cout <<ob.a<<“\t”<< ob.b<<“\t”<< ob.c<<“\t”<< ob.total<<“\t”<<“\n”;}

Page 34: Inheritance

Example of Multilevel Inheritance

#include<iostream.h> class student { protected: int roll_number; public: void get_number() { cout<<"Enter rollno"; cin>>roll_number; } void putnumber() { cout<<roll_number; } };

Page 35: Inheritance

class test: public student{

protected: float sub1; float sub2; public: void getmarks() { cout<<"enter the marks of sub1,sub2"; cin>>sub1>>sub2; } void putmarks() { cout<<"marks in sub1="<<sub1; cout<<"marks in sub2="<<sub2; }

};

Page 36: Inheritance

class result : public test{ float total; public: void display(void) { total=sub1+sub2; putnumber(); putmarks(); cout<<"Total"<<total; } }; void main() { result a; a.get_number(); a.getmarks(); a.display(); }

Page 37: Inheritance

ANY QUERY

THANK YOU