Inheritance allows the creation of new classes from existing classes. There are different types of inheritance including single, multiple, multilevel, hierarchical and hybrid. Multilevel inheritance allows a derived class to inherit from another derived class. Constructors and destructors are called from base to derived classes. Multiple inheritance requires derived classes to pass arguments to multiple base class constructors.
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.
SINGLE INHERITANCE Whena derived class inherits only from one base class ,it is Known as single Inheritance. Base Class Derived Class A B
5.
When a derivedclass inherit from multiple base classes it is known as multiple Inheritance. A B C MULTIPLE INHERITANCE Base Class Derived Class
6.
MULTILEVEL INHERITANCE Whena 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
7.
HYBRID INHERITANCE Hybrid Inheritance combines two or more forms of Inheritance. w x y z
8.
HIERARCHICAL INHERITANCE Whenmany derived classes inherit from a single base class , it is known as hierarchical Inheritance. Base Class Derived classes a b c d
9.
A general formto 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
10.
Example of derivedclass 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 };
11.
MULTIPLE INHERITANCE Exampleof 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 };
12.
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.
13.
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.
14.
Visibility of Inheritedbase class members in Derived Class. Visibility Mode Public members of base class becomes Protected members of base class becomes Private members of the base class is not accessible to the derived class. Public Public Protected Protected Protected Protected Private Private Private
15.
Accessibility of Baseclass members No No Yes Private No Yes Yes Protected Yes Yes Yes Public Accessible from objects outside class Accessible from derived class Accessible from own class Access Specifier
16.
Inheritance and Constructorsand 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
17.
Class super { …… . }; Class Sub : public Super { ….. }; int main() { sub ob1; …… . }
18.
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) { … .. }
19.
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.
20.
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
21.
Constructor in DerivedClass # include<iostream.h> class alpha { protected: int x; alpha(int i) { x=i; } }; class beta { protected: float y; Beta(float j) { y=j; } };
22.
Class gamma: publicbeta, 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(); }
23.
Facts about inheritancePrivate 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 };
24.
When a classis 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 }
25.
When you derivea 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 ;
26.
Abstract class Aclass 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.
27.
Constructors in Multipleinheritance 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”; } };
28.
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”; }
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 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”; }
31.
To Use Scoperesolution 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”; } Remedy
32.
To Use akeyword 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; };