Wednesday, April 29, 2020

Inheritance


The mechanism of deriving a new class from old class is called inheritance or derivation. The old class is referred to as a base class and the new class is called the derived call or subclass
There are different types of inheritance
1)    Single inheritance: A derived class with only one base calls is called single inheritance.
2)    Multiple inheritances: A derived class with several base classes is called multiple inheritance.
3)    Hierarchical inheritance: Several derived classes which are inherited by single base class is called hierarchical inheritance.

1)    Multilevel inheritance: The mechanism of deriving a class from another derived class is known as multilevel inheritance.

2)    Hybrid inheritance: Hybrid inheritance is the collection of different form of inheritance such as multilevel, multilevel, hierarchical inheritance etc.

Defining derived class:-

The general form of defining the derived class is

class derived_class_name : visibility mode base class_name

{

member of the derived classes

};

E.g. class A

{

};

class B:public A

{

member of the derived class
};

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

Example

class ABC: private xyz//private derivation

{

member of ABC
};

class ABC: public  xyz//public derivation
{
member of ABC
};

class ABC:   xyz//private derivation by default
{
member of ABC
};

When a base class is privately inherited by a derived class, public member of a base class becomes private member of the derived class and therefore the public member of the base class only be access by the member function of the derived class. They are inaccessible to the object of the derived class. Remember a public member of a class can be accessed by its own objects using the dot (. ) operator. The result is that no member of the base class is accessible to the object of the derived class.

When the base class is publicly inherited, public member of the base class becomes the public member of the derived class and therefore they are accessible to  the objects of the derived class. In both the case, the private member are not inherited and therefore the private member of  a base class will never become the member of its derived class.

Single inheritance (Public)

class base

{
public:
void display1( );
};
class derived: public base
{
public:
void display2( );
};
void base::display1( )
{
cout<< “Member function of the base class”;
}
void derived::display2( )
{
cout<< “ derived class memebre function \n”;
}
void main ()
{
Derived d;
d.display1( );
d.display2( );
}

Privately:
class base
{
public:
void display1( );
};
class derived: base
{
public:
void display2( );
};
void base::display1( )
{
cout<< “public Member function of the base class”;
}
void derived::display2( )
{
display1( );
cout<< “ derived class memebre function \n”;
}
void main ()
{
Derived d;
d.display2( );
getch( );
} 

class base

{

int a; // private data member
public:
int b; //  public; ready for inheritance
void get_ab( );
int get_a( );
};
class derived: public base // public derivation
{
int c;
public:
void mul( );
void display( );
};
void base::get_ab( )
{
a=5,b=10;
}
int base::get_ab( )
{
return a;
}
void derived:: mul( )
{
c=b*get_a( );
}
void derived::display( )
{
cout<< “a=” get_a( );
cout<< “b=” b;
cout<< “c=” c;
}
void  main( )
{
clrscr( );
Derived d;
d.get_ab;
d.mul( );
d.display ( );
getch( );
}
 Making a private member inheritance:-

   C++ provides third visibility modifier protected which serve a limit purpose in inheritance. A member limit declared as ‘protected’ is accessible by the member function within its class and any class immediately derived from it. It cannot be accessed by the funct outside this two classes.
    When a protected member is inherited in the public mode it becomes protected in the derived class too and therefore is accessible by the member function of the derived class. It is also ready for further inheritance. A protected member, inheritance in the private mode derivation, becomes private in the derived class. Although it is available to the member function of the derived class it is not available for futher inheritance.
     It is also possible to inheritance the base class protected mode known as protected.
    In protected derivation both public and protected member of the base class become protected in the derived class.

#include<iostream.h>

#include<conio.h>

class B
{
protected:
int a,b;
public:
void get_ab( );
};
class D:: public B
{
public:
int sum( );
};
void B::get_ab( )
{
a=5,b=10;
}
int D::sum( )
{
return a+b;
}
void main( )
{
clrscr( );
D d;
d.get_ab( );
cout<< “Addition=”  << d.sum;
getch( );
}


Write a program to show an example of using ‘protected’ visibility label for public derivation.

#include<iostream.h>

#include<conio.h>
class B
{
protected:
int a,b;
public:
void get_ab( );
};
class D:: public B
{
public:
int sum( );
class E:public D
{
public:
int sub( );
};
void B::get_ab( )
{
a=5,b=10;
}
int D::sum( ) // function defination
{
return ( a+b);
}
int E:sub( ) // function definition
{
return (a-b);
}
void main( )
{
clrscr( );
E e;//object
e.get_ab( ); // calling the function
cout<< “Addition=”  << e.sum( );
cout<< “Subtration=”  << e.sub( );

getch( );
}


Multilevel inheritance
Syntax
class A
{
};
class B:visibility A
{
};
classC: visibility B
{
};
Where visibility may be either private or public or protected.

A class A server as a base class for the derived class B, which turn serves as a base class for a derived class C. The class B is known as intermediate base class since it provides a link for the inheritance between A and C. The chain A,B,C is known as inheritance path.

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int r_no;
public:
void get_number(int x);
void put_number ( );
};
void student::get_number(int x)
{
r_no=x;
}
void student::put_number()
{
cout<< “Roll_no=” <<r_no<< “\n”;
}
class test:public student
{
protected:
float sub1,sub2;
public:
void get_marks( float x, float y);
void put_marks();
};
Void test::get_marks(float x, float y )
{
sub1=x;
sub2=y;
}
void test::put_marks( )
{
cout<< “Marks in sub1=”  << “\n”;
cout<< “Marks in sub2=”  << “\n”;
}
class result::public test
{
float total;
public:
void display( );
};
void result:display( )
{
total=sub1+sub2;
put_number( );
put_marks( );
cout<< “total=” << total “\n”
}
void main ( )
{
clrscr ( );
ob.reg_number(100);
ob.put_marks(3.5,7.5);
ob.display( );
getch( );
}

Multiple inheritance:
Syntax
class A
{
};
class B
{
};
class C
{
};
class D:visibility A, visibility B, visibility C
{
};
A class can inherit the attributes of two or more classes is known as multiple inheritance. Multiple inheritances allow us to combine the features of several existing classes as a starting point for defining new classes.

#include<iostream.h>
#include<conio.h>
class B1
{
protected:
int x;
public:
void get_x(int a);
};
class B2
{
protected:
int y;
public:
void get_y(int b)
}
class D: public B1,public B2
{
public:
void display( );
};
void B1::get_x(int a )
{
x=a;
}
void B2::get_y(int b)
{
y=b;
}
void D:;display( )
{
cout<< “x=”  <<x<< “\n”;
cout<< “y=”  <<y<< “\n”;
cout<< “x+y=”  <<x+y<< “\n”;
}
void  main( )
{
clrscr( );
D d;
d.get_x(10);
d.get_y(20);
d.display( );
getch( );
}
 
Ambiguity resolution in inheritance
#include<iostream.h>
#include<conio.h>
class B1
{
public:
void display( );
};
class B2
{
public:
void display( );
};
class D:public B1, public B2
{
void display( );
}
void B1:display( );
{
cout<< “In class B1 \n”;
}
void B2:display( );
{
cout<< “In class B2 \n”;
}
void D:display( );
{
cout<< “In class D \n”;
}
void  main( )
{
clrscr( );
D d;
d.display( );
d.B1::display( );
d.B2::display( );
getch( );
}


The function in the derived class overwrites the inherited function and therefore a simple called display ( ) by type of object will invoked the function defined in D only. However we may invoked the function defined in B1 and b2 by using the scope resolution operator (: ) to specify the class.
Hierarchical inheritance:-

Several derived classes which are inherited by base class.

Syntax:

class A

{

};

class B : visibility A      

{

};

class C : visibility A

{

};

class D : visibility A

{

};


#include<iostream.h>
#include<conio.h>
class B
{
protected:
int a,b;
public:
void get_ab( );
};
class D1:: public B
{
public:
int sum( );
};
class D2:public B
{
public:
int mul( );
};
void B::get_ab( )
{
a=5,b=10;
}
int D1::sum( )
{
return a+b;
}
int D2::mul( )
{
returna*b;
}
void main( )
{
clrscr( );
D1  ob1;
Ob1.get_ab( );
cout<< “Addition=”  << ob1.sum( );
D2  ob2;
Ob2.get_ab( );
cout<< “Multiplication=”  << ob2.mul( );
getch( );
}

Hybrid inheritance:-
Syntax:
class A
{
};
class B : visibility A      
{
};
class D
{
};
class C : visibility B, visibility D      
{
};
  




Where, visibility may be either private or public.
#include<iostream.h>

#include<conio.h>

class A
{
protected:
int a;
};
class B:: public A
{
protected:
int b;
};
class D
{
protected:
int c;
};
class c:public B, public D
{
public:
void get_abc( )
int sum( );
};
void c:get_abc( )
{
a=5,b=6,c=7;
}
int c::sum( )
{
return a+b+c;
}
void main
{
clrscr( );
c  ob;
ob.get_abc( );
cout<< “Addition=”  << ob1.sum( );
getch( );
}

Virtual Base class

Syntax:

class A

{

};

class B1 : public virtual A      

{

};

class B2 : public virtual A

{

};

class C : public virtual  B1, public virtual B2      
{
//only one copy of class A will be inherited//
};

Consider a situation when all the three kinds of inheritance, namely multilevel, multiple and hierarchical inheritance are involved. The class c has two direct base classes B1 and B2 whose themselves have common base class A. The class C inherits, the data member and member function of class A via two separate path.
All the public and protected member of class A are inherited in class C twice first via class B1 and next via class B2. This means class C would have duplicate set of member inherited form class A. this introduces ambiguity and should be avoided.
The duplicate of inheritance members due to these multiple path can be avoided by making the common base class as virtual base class awhile declaring the direct or intermediate base classes.
When a class is made a virtual base class, C++ takes necessary care to see that only  one copy of that class is inherited, regardless of how many inheritance path exists between the virtual class and derives class.

#include<iostream.h>

#include<conio.h>
class A
{
public:
void display( );
};
class B1: public virtual A
{
};
class B2:public virtual A
{
};
class C: public B1, public B2
{
};
void A::display( )
{
cout<< “In the virtual base class A\n=”;
}
void main
{
clrscr( );
c  obj;
obj.display( );
getch( );
}
 



Abstract class

An abstract class is one that is not used to create object. It is a design concept in program development and provides a base upon which other classes may be built.

Since we can’t create object abstract class, we can create pointers and reference to an abstract class. This allows abstract classes to support run time polymorphism, which relies upon base class pointer and references to select he proper virtual function.

Constructor in derived class

If any base class contains the constructor with one or many argument, then it mandatory for the derived class to have the construction and pass.

While applying inheritance, we usually create object using the derived class. Thus it make sense for the derived class to pas argument to the base class constructor. When the both derived and base classes contain contractors, the base constructor is executed first and then the constructor in derived class is executed.

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



class A

{

};
class B
{
};
class C: public A, public B
{
};

Similarly in a multilevel inheritance, the constructor will be executed in the order of inheritance.

Since the derived class takes the responsibility of supplying initial values to its base class, we supply the initial values that are required by all the classes together, when a derived class object is declared.
#include<iostream.h>
#include<conio.h>
class alpha
{
int x;
public:
alpha(int i)
{
x=i;
}
void display_x( )
{
cout<<  “x=”  << x<< “\n”;
}
};
class beta: public alpha
{
int y;
public:
beta(int a, int b):alpha (a)
{
y=b;
};
void display( )
{
cout<< “y=”  <<  “\n”;
}
};
void main
{
clrscr( );
beta  obj(10,20);
ob.display_x( );
ob.display_y( );
getch( );
}
 
The constructor of the derived class receives the entire list of the values as its arguments and passes them on to the base constructor in the order in which they are declared in the derived class. The base constructor are called and executed before executing the statement in the body of the derived constructor.
The constructor for virtual base classes are invoked before any non-virtual base classes. If there are multiple virtual base classes they are invoked in the order in which they are declared. Any non-virtual base classes are then constructor before the derived class constructor is executed.
Method of inheritance                                        Order of execution
class B: public A                                                  A( ); base constructor
{                                                                           B ( ); derived constructor
};                                                                          B( );  base constructor (1st)
class A:public B, public  C                                   C( ); base constructor (2nd)
{                                                                           A( ); derived
};
Class A:public B, virtual C                                  C( ); virtual base class
                                                                             B( ); ordinary base
                                                                             A( ); derived

The general form of defining the derived constructor
Derived _constructor( arglist1, arglist2…..arglist D):
base1(arglist1), base2(arglist2)
{
body of the derived constructor
};

#include<iostream.h>
#include<conio.h>
class alpha
{
int x;
public:
alpha(int i );
{
x=i;
}
void display_x( )
{
cout<< “x=” << “\n”
{
};
class beta
{
float y;
public:
beta(float j )
{
y=j;
}
void display_y( )
{
cout<< “y=” <<y “\n”;
}
};
class gamma:public alpha, public beta
{
int m;
float n;
public:
gamma( int a, floab, int c, float d): alpha(a), beta(b)
{
m=c;
n=d;
void display_mn( )
{
cout<< “m=”<<m<< “\t” << “n=” <<n “\n”;
}
};
void main( )
{
clrscr( );
gamma ob(5,3,5,10,3.75);
ob.display_x( );
ob.display_y( );
ob.display_mn( );
getch( );
}



Using multilevel inheritance
#include<iostream.h>
#include<conio.h>
class alpha
{
int x;
public:
alpha(int i );
{
x=i;
}
void display_x( )
{
cout<< “x=” << “\n”
}
};
class beta: public alpha
{
int y;
public:
beta(int a, int b):alpha(a)
{
y=b;
}
void display_y( )
{
cout<< “y=” <<y<< “\n”;
}
};
class gamma:public  beta
{
int z;
public:
gamma( int p, int q, int r): beta(p,q)
{
z=r;
void display_z( )
{
cout<< “z=”<<z<< “\n”;
}
};
void main( )
{
clrscr( );
gamma ob(5,10,15);
ob.display_x( );
ob.display_y( );
ob.display_z( );
getch( );
}
 
Example of virtual base class
#include<iostream.h>
#include<conio.h>
class base
{
public:
int i;
};
class d1:virtual public base
{
public:
int j;
};
class d2: virtual public base
{
public:
int k;
};
class d3:public d1,public d2
{
public:
int sum;
};
void main( )
{
clrscr( );
d3 ob;
ob.i=10;
ob.j=20;
ob.k=30;
ob.sum=ob.i+ob.j+ob.k;
cout<< “sum=” << ob.sum;
getch( );
}
 
 
 

 
 

No comments:

Post a Comment