Вы находитесь на странице: 1из 36

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.

Types of Inheritance

Single Inheritance Multiple Inheritance Multilevel Inheritance Hierarchical Inheritance Hybrid Inheritance

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

Base Class
A

Derived Class

MULTIPLE INHERITANCE
When a derived class inherit from multiple base classes it is known as multiple Inheritance.
A
B Base Class

Derived Class

MULTILEVEL INHERITANCE
When a derived class inherit from a class that itself inherits from other class , it is known as multilevel Inheritance. Base Class A

Derived Class of x Base Class of z

Derived Class

HYBRID INHERITANCE
Hybrid Inheritance combines two or more forms of Inheritance.
w

HIERARCHICAL INHERITANCE
When many derived classes inherit from a single base class , it is known as hierarchical Inheritance. Base Class a

Derived classes

DEFINING DERIVED CLASS


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

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

MULTIPLE INHERITANCE
Example 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 };

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.

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.

Visibility of Inherited base class members in Derived Class. Visibility Mode Public members of base class becomes Public Protected Protected members of base class becomes Protected Protected Private members of the base class is not accessible to the derived class.

Public Protected

Private

Private

Private

Accessibility of Base class members


Access Specifier Accessible from own class
Yes Yes Yes

Public Protected Private

Accessible from derived class Yes


Yes No

Accessible from objects outside class


Yes No No

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

Class super
{ .

};
Class Sub : public Super { ..

};
int main() { sub ob1; .

Base class Constructors


Base 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) { .. }

Class Base { int a;

float b;
public : Base(int I, float j) { a = i; b = j; }

};
Class Derived : public Base {. Public :

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

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


{ }; }

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

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

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

Facts about inheritance


Private members of a base class are not directly accessible in the derived class.
Example int test; Class base { int test; public: void getit() { cin>> test; \\ Global variable \\ private members of base

}
}; Class derived : public base { public : void check ( ) { }; test++; } \\ not allowed

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

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 ;

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.

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

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

~ 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 Base2 Constructing Base1 Constructing Derived 1. 15 2. 14 Destructing Derived Destructing Base1 Destructing Base2

3. 16

Example 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 ambiguous Ob.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; }

Virtual Base class

Remedy
To Use Scope resolution operator to a

Void main ( ) { D3 ob; ob.D1 :: a = 25 \\ scope resolved Ob.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; }

To Use a keyword Virtual Example Class Base { public : int a ; }; Class D1 : virtual public Base { public : int b ; }; Class D2 : virtual public Base { public : int c ; }; Class D3 : public D1, public D2 base { public : int total; };

\\ D1 inherits Base as Virtual

\\ this time only one copy of

Void main ( ) { D3 ob; ob.a = 25 \\ now unambiguous Ob.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; }

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

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

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