The mechanism of deriving a new class from an old class is
called inheritance( or derivation). The old class is referred to as the base class or super class or parent class. The new class is called the derived class or subclass or child class. NEED FOR INHERITANCE The capability to express the inheritance relationship which ensures the closeness with the real world model. Idea of reusability, i.e., the new class can use some of the features of old class. The advantages of reusability are : faster development time, easier maintenance and easy to extend Transitive nature of inheritance, i.e., it can be passed on further. If a class B inherits properties of another class A, then all subclasses of B will automatically inherit the properties of A. This property is called transitive. DIFFERENT FORMS OF INHERITANCE Single inheritance A derived class with only one base class is called single inheritance. Multiple inheritance A derived class with several base classes is called multiple inheritance. DIFFERENT FORMS OF INHERITANCE Multilevel inheritance The mechanism of deriving a class from another derived class is called multilevel inheritance. ( Transitive nature of inheritance is reflected by this form) Hierarchical inheritance When many sub classes inherits from a single base class, it is known as hierarchical inheritance. DIFFERENT FORMS OF INHERITANCE Hybrid inheritance It is a combination of hierarchical and multiple inheritance. DEFINING DERIVED CLASS A derived class is defined by specifying its relationship with the base class using visibility mode. The general form of defining a derived class is: class derived_class : visibility_mode base_class { ____________ ____________ // members of derived class. }; The colon indicates that the derived_class is derived (inherits some property) from base_class. DEFINING DERIVED CLASS Following are some examples of derived class definitions: 1. class Marksheet : public student // public derivation { // members of derived class }; 2. class Marksheet : private student // private derivation { // members of derived class }; 3. class Marksheet : protected student // protected derivation { // members of protected class }; DEFINING DERIVED CLASS If no visibility mode is specified, then by default the visibility mode is considered as private. Actually a derived class inherits all the members of a base class; however, the derived class has access privilege only to the non private members of the base class. The private members of the base class cannot be accessed directly, yet the objects of derived class are able to access them through the non-private inherited members. MULTIPLE INHERITANCE Subclass inheriting from multiple base classes is known as Multiple inheritance. The Syntax of defining a derived class is given below : class derived_classname : mode baseclass1, mode baseclass2 { // members of derived class }; Example 1 class marks : public semester1, private semester2 { // members }; VISIBILITY MODES It can be public, private or protected. If inheritance is done in public mode, public members of the base class become the public members of derived class and protected members of base class become the protected members of derived class. If inheritances is done in a private mode, public and protected members of base class become the private members of derived class. If inheritance is done in a protected mode, public and protected members of base class become the protected members of derived class. VISIBILITY MODES PRIVATE PUBLIC VISIBILTY MODE class student { private : int x; void getdata ( ); public: int y; void putdata ( ); protected: int z; void check ( ); }; class marks : public student { private : int a ; void readdata ( ); public : int b; void writedata( ); protected : int c; void checkvalue( ); }; PUBLIC VISIBILTY MODE The public derivation does not change the access specifiers for inherited members in the derived class. The private data of base class student cannot be inherited. PRIVATE VISIBILITY MODE class student { private : int x; void getdata ( ); public: int y; void putdata ( ); protected: int z; void check ( ); }; class marks : private student { private : int a ; void readdata ( ); public : int b; void writedata( ); protected : int c; void checkvalue( ); }; PRIVATE VISIBILITY MODE The data present in public and protected section of base class become the private members of derived class. The data in private section of base class cannot be inherited. Making a Private Member Inheritable Private members of the base class are not inherited by derived class ( i.e., the derived class does not get access privilege for them) Methods to make private member inheritable:- 1. By making visibility mode of the private member as public 2. By making the visibility mode of the private member as protected. By modifying the visibility mode of private member to public will make them accessible to all the other functions(non member ) of the program, thus taking the advantage of data hiding Making a Private Member Inheritable Introduction to third visibility modifier, protected. This modifier retains the data hiding of the private members and at the same time makes it inheritable. A member declared as protected is accessible by the member functions within its class and any class immediately derived from it. PROTECTED VISIBILITY MODE class student { private : int x; void getdata ( ); public: int y; void putdata ( ); protected: int z; void check ( ); }; class marks:protected student { private : int a ; void readdata ( ); public : int b; void writedata( ); protected : int c; void checkvalue( ); }; PROTECTED VISIBILITY MODE The data present in private section of base class cannot be inherited. The difference between private and protected section is that data present in protected section can be inherited. Otherwise both the section cannot be accessed by the object of the class. INHERITANCE AND CONSTRUCTORS AND DESTRUCTORS When an object of derived class is created, first the base class constructor is invoked, followed by the derived class constructors. When an object of derived class expires, first the derived class destructor is invoked, followed by the base class destructor. In case of multiple inheritance, the base classes are constructed in the order in which they appear in the declaration of the derived class. In a multilevel inheritance, the constructors will be executed in the order of inheritance. 22 Constructor Rules for Derived Classes class A { public: A ( ) {cout<< A:default<<endl;} }; class B : public A { public: B () {cout<<B<<endl;} }; Object of class B test; A:default B output: CONSTRUCTORS RULES As long as no base class constructor takes any arguments, the derived class need not have a constructor function. If any base 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 class constructor. CONSTRUCTORS RULES Derived class constructor accepts arguments for itself as well as for its base constructor. The general form of defining a derived constructor is:- The header line of derived constructor function contains two parts separated by a colon(:). First part provides the declaration of the arguments that are passed to derived constructor. The second part lists the function calls to the base constructors. DerivedClassCon ( ArgumentsBaseDerived ) : BaseClassCon ( baseClass args ) { DerivedClass constructor body } CONSTRUCTORS RULES 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 the derived constructor does not need any argument for itself, yet it accepts parameters for is base constructor and then invokes base constructor with these parameter. CONSTRUCTORS RULES 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, float j , int p , float q ): base(p, q) { x=i; y=j; } }; Here the derived constructor is accepting parameters for itself (i,j) as well as for its base constructor (p,q). Then it invokes its base constructor with appropriate parameters and uses rest of the parameters for initializing its own member . Execution of base class constructor Method of inheritance Order of execution class B:public A { }; A(); base constructor B(); derived constructor class A : public B, public C { }; B(); base(first) C(); base(second) A(); derived class A: public B, virtual public C { }; C(); virtual base C(); ordinary base A(); derived class alpha { int x; public: alpha(int i) { x=i cout<<alpha intialized\n; } void show_x(void) { cout<<x =<<x<<\n;} ~alpha() {cout<<\n alpha destructing;} }; class beta { float y; public: beta (float j) { y=j; cout<<beta intialized\n; } void show_y(void) { cout<<y =<<y<<\n;} ~beta() {cout<<\n alpha destructing;} }; class gamma: public beta, public alpha { int m,n; public: gamma(int a, float b , int c, int d): alpha(a),beta(b) { m=c; n=d; cout<<gamma intialized\n; } void show_mn(void) { cout<<m =<<m<<\n; cout<<n =<<n<<\n; } ~gamma() {cout<<\n gamma destructing;} }; int main() { gamma g (5, 10.75, 20, 30); cout<<\n; g.show_x(); g.show_y(); g.show_mn(); return 0; } beta initialized alpha initialized gamma initialized x=5 y=10.75 m=20 n=30 gamma destructing alpha destructing beta destructing Virtual Base Class When a class inherits from multiple base classes, it can lead to an ambiguous situation. This is especially when the base classes themselves are derived from a common base class As shown in the figure, A is a base class from which two classes are derived, B and C. These further acts as base classes for D. Virtual Base Class D wants to access a function which is inherited from base class A, into B and C. Obviously, since D is inheriting from both B and C, it receives two copies of the members they inherited from their base class A. When this member is accessed, compiler faces an ambiguous situation, which can be resolved in two ways. Using Scope resolution operator B :: member or C :: member The other way is to make the common base class (here, A) as virtual class Virtual Base Class class A { . public : int a; }; class B : virtual public A { . public : int b; }; class C : virtual public A { . public : int c; }; class D : public B, public C { . public : int c; }; Now, because of the keyword virtual, classes B and C will share a single common base class member a. Thus there will be no ambiguity when accessing it. Q What is the difference between base class and normal class? Abstract Class A class that serves only as a base class from which other classes are derived , but no object of this base class type exists, is known as Abstract Class Containership or Aggregation When a class contains objects of other class types as its member, it is referred to as Containership or Containment or Aggregation. class X { }; class Y {.}; class Z { X ob1; //object of X class Y ob1; //object of Y class }; All the objects of Z class type will be containing objects of ob1 and ob2 of X and Y types respectively. This type of relationship is called containership or nesting Containership or Aggregation Invocation of Constructor in containership. With reference to previous example, when we create an object of class Z, first constructor of class X followed by Y ( order in which they are declared) and then Z is invoked Constructor X Constructor Y Constructor Z Containership or Aggregation If constructor requires arguments for the object creation then this is accomplished by using an initialization list in the constructor of the enclosing class:- class Z { X ob1; //object of X class Y ob1; //object of Y class public: Z(arg-list):ob1(arg-list), ob2(arg-list) { // Z constructor body } }; Relationship between Classes When a class inherits from another class, the derived class has IS-A relationship with its base class. When a class contains object of another class- type, then containing/enclosing class HAS-A relationship with contain/enclosed class