C++ Inheritance

INHERITANCE

The mechanism of deriving a new class from an old one is called inheritance(or derivation). The old class is referred to as the base class and the new one is called the derived class. The derived class with only one base class is called single inheritance and one with several base classes is called multiple inheritance. On the other hand, the traits of one class may be inherited by more than one class. This process is known as hierarchical inheritance. The mechanism of deriving a class from another ‘derived class’ is known as multilevel inheritance. The following figure shows various forms of inheritance that could be used for writing extensible programs. The direction of arrow indicates the direction of inheritance.


DEFINITION:

The mechanism of deriving the new class from an old one is called inheritance. The old class is called as base class and the new class is called as derived class.

Types of Inheritance:

1. Single inheritance

2. Multiple inheritance

3. Multilevel inheritance

4. Hierarchical inheritance(refer Assignment)

5. Hybrid inheritance




Defining derived classes:-

A derived class is defined by specifying its relationship with the base class in addition to its own details.

The general form:-

class derived-class-name: visibly-mode base-class-name

{

------------//

------------//members of derived class

------------

};

The colon indicates that the derived-class-name is derived from the base-class-name. The visibility mode is optional and, if present, may be either private or public. The default visibility-mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.

Examples:-

class ABC:private XYZ//private derivation

{
members of ABC

};

class ABC:public XYZ//public derivation

{

members of ABC

};

class ABC:XYZ//private derivation by default

{

members of ABC

};

When base class is privately inherited by a derived class, ’public members’ of the base class become ‘private members’ of the derived class and therefore the public members of the base class can only be accessed by the member functions of the derived class. They are inaccessible to the objects of the derived class. Remember, a public member of a class be accessed by its own objects using the dot operator. The result is that no member of the base class is accessible to the objects of the derived class.


When the base class is publicly inherited, ‘public members’ of the base class become ‘public members’ of the derived class and therefore they are accessible to the objects of the derived class. In both the cases, the private members are not inherited and therefore, the private members of the base class will never become the members of its derived class.



1.SINGLE INHERITANCE:-


The new class can be derived from only one base class is called single inheritance.



Let us consider a simple example to illustrate inheritance. The following program shows the base class B and a derived class D. The class B contains one private data

member, one public data member, and three public member functions. The class D contains one private data member and two public member functions.




#include<iostream.h> class B

{

int a; //private; not inheritable public:

int b; //public; ready for inheritance void get_ab()

{

a=5;

b=10;

}

int get_a(void)

{

return a;

}

void show_a(void)

{

cout<<”a=”<<a<<endl;

}

};

class D:public B //public derivation

{

int c; public:

void mul(void)

{

c=b*get_ab();
}

void display()

{

cout<<”a=”<<get_ab()<<endl; cout<<”b=”<<b<<endl; cout<<”c=”<<c<<endl;

}

};

void main()

{

D d; d.get_ab(); d.mul(); d.show_a(); d.display(); d.b=20; d.mul(); d.display();

}

Output:-

a=5

a=5

b=10

c=50


a=5

b=20

c=100


2.Multilevel Inheritance:-

The class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C. The chain ABC is known as inheritance path.


 




A derived class with multilevel inheritance is declared as follows. class A(….) ;

class B:public A(…..); class C:public B(…..);
The multilevel inheritance is defined as, the new class is derived from another derived class.


Here the class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C. The derived class is defined as,

Class A

{

//body of base class A

};

Class B : public A

{

//body of intermediate base class B

};


Class C : public B

{

//body of derived class

};



3.MULTIPLE INHERITANCE:-


A class can inherit the new class can be derived from Here the class A , class B and attributes or properties of two or more classes that is a more than one base class is called multiple inheritance. class C serves as a base class for the derived class D

The derived class definition syntax is: class d: visibility base-1, visibility base 2,…

{

body of class D;

};

Example:

#include<iostream.h> class A

{

protected:

int rollno;

public:

void getroll(int x)

{
rollno=x;

}

}; class B

{

protected:

int sub1,sub2;

public:

void getmark(int y,int z)\

{

sub1=y;

sub2=z;

}

};

class C : public A, public B

{

int total; public:

void display()

{

total=sub1+sub2;

cout<<”roll no:”<<rollno<<”sub1:”<<sub1<<”sub2:”<<sub2; cout<<”total”<<total;

}

};

void main()

{

C s;

s. getroll(435); s.getmark(100,90); s.display();
}

Run:

Roll no : 435

Sub1: 100

Sub2: 90


4.HIERARCHICAL INHERITANCE:-

The hierarchical inheritance structure is given below .This type is helpful when we have class hierarchies. In this scheme the base class will include all the features that are common to the subclasses.


#include<iostream.h>

#include<string.h>
 class A

{

protected: int x, y;
public:
void get ( )

{

cout<<”Enter two values”<<endl; cin>> x>>y;

}

};

class B : public A

{

private: int m; public:

void add( )

{

m= x + y;

cout<<”The Sum is “<<m;
}

};

class C : public A

{

private: int n;
public: void mul( )

{

n= x * y;

cout << “The Product is “<<n;

}

};

class D : public A

{

private: float l;
public:

void division( )

{

l = x / y;

cout <<”The Quotient is “<< l;

}

};

void main( )

{

obj1;

obj2;

obj3;

.get( );

.add( );

.get( );

.mul( );
D .get( );

D .division( );

}

Output of the Program

Enter two values 12 6

The Sum is 18

The Product is 72

The Quotient is 2.0

أحدث أقدم