Multi Level Inheritance in C++

MULTI LEVEL INHERITANCE

The mechanism of deriving a class from another derived class is known as multilevel inheritance in C++.

e.g:

Class base

{      };

class dervied1: public base

{         };

class deriverd2 :public derived1

{         };

 

Screenshot_27

 

Multi Level Inheritance in C++ Example

# include <iostream.h>

class person

char name[10];

public:

void read_name()

{

cout<<”\n Input Name”;

cin>>name;

}

void print_name()

{

cout <<”\n Name “<<name;

}

class student:public person

int rno;

public:

void read_rno()

{

cout<<”\n Input roll no”;

cin>>rno;

}

void print_rno()

{

cout<<”\n Rollno”<<rno;

};

clas student :: public Mca_student

int sub1,sub2;

public:

void read_subjects()

{

cout <<”\n Input two subjects”;

cin >>sub1>>sub2:

}

void print_subjects()

{

cout <<”\n Subject1”<<sub1;

cout<<”\n Subject2<<sub2;

};

void main()

{

Mca_student stud1;

stud1.read_ stud1;

stud1.read_name();

stud1.read_rno();

stud1.read_ subjects();

stud1.print_name();

stud1.print_rno();

stud1.print_subjects();

}

 

 

Constructors in derived class

  • Constructors of any class are invoked only if an object of that class is constructed.
  • If the base class constructor is having argument which assigns value to the data members of the base class then it is mandatory to invoke the base class constructor.
  • But the derived class object cannot invoke the base class constructor directly.
  • In that case derived class constructor invokes the base class constructor.
  • The constructor of the derived class receives the entire list of the values and passes then to the base constructors.
  • Constructors of derived class calls implicit default or non parameterized constructor of base class.

#include <iostream.h>

class base

public:

base()

{

cout<<”\n Inside base class constructor”;

};

class derived :public base

public :

derived ()

{

 

 

cout<<”\n Inside derived class constructor”;

};

void main()

{

clrscr();

derived d1;

}

 

  • Parameterized constructor of base class is called explicitly with in constructor of derived class. In this calling is done as a part of constructor header.
  • g:

#include <iostream.h>

# include<string.h>

#include<conio.h>

class account

{

int accno;

char name[10];

public :

account(int acno,charn[ ])

accno=acno;

strcpy(name,n);

void pront_accno_name()

{

cout<”\n Account No”<<accno;

 

cout<<”\n Customer name”<< name;

}

Class saving _account: public account

char cheq_fac;

public:

saving_account(int a, char n[ ],char c)): account(a,n)

cheq_fac=c;

void print_cheq_fac()

{

cout<<”\ n Chaqfac”<<cheq_fac;

};

void main()

{

clrscr();

saving_account acc1(101,”abc”,’y’);

acc1.print_accno_name();

acc1.print_cheq_fac();

}

Non-parameter => implicitly

Parameter=> Explicitly

 

E.g:

# include<iostream.h>

# include<conio.h>

class base

public:

base()

{

cout<<”\n Inside non parameterized constructor of base class”;

}

base (int x)

{

cout <<”\n Inside parameterized constructor of base class”;

};

class derived :public base

public:

derived ():base(10)

cout<<”\n Inside non parameterised constructor of derived class”;

derived (int x) // default constructor of base class

cout <<”\n Inside parameterised constructor of derived class”;

};

void main()

{

derived d1;

derived d2(10);

}

Destructor in inheritance:

  • Complier called implicitly the destructor of base calls inside destructor of derived class.
  • Calling of base destructor inside derived class destructor is last statement.

 

 

E.g:

# include <iostream.h>

# include <conio.h>

class base

{

public:

base()

{

cout<<”\n Inside constructor of base class”;

}

base ()

cout<<”\n Inside destructor of base class”;

}

class derived :public base{

public:

derived()

{

cout<<”\n Inside constructor of derived class”;

}

derived()

{ cout <<\n  Destructor of derived class”;

};

void main()

{

clrscr();

derived d1;

}

 

Member function overriding:

  • Redefining of base class member function inside derived class is called Member function overriding.
  • Writing member function inside derived class with same name, no.of parameters, types of parameter and order of parameters of member function defined base class is called member function overriding.
  • Member function override inorder to modify functionality of base class member function inside derived class.
  • If derived class wants to have different implementation of base class member function, it override base class function inside derived class.

 

 

E.g:

# include <iostream.h>

class base

{

public:

void fun1()

{

cout<<”\n Inside base class function 1”;

}};

class derived :public base

{

public:

void fun1(){ (Inherited from base class & used in derived class)

cout<<”\n Inside overriding function”;

};

void main()

{

derived d;

d.fun1();

}

 

E.g:

 

# include <iostream.h>

# include<conio.h>

class person

{

Char name[10];

public:

void read()

{

cout<<”\n Input name”;

cin>>name;

}

void print()

{

cout<<”\n Name”>>name;

}};

class customer :public person

{

int id;

float credit_limit;

public:

void read()

{

person:: read();

cout<<”\n Input id”;

cin>>id;

cout<<”\n Input credit_limit;

}

void print()

{

person::print();

cout<<”\n Id”<<id;

cout<<”\n Credit Limit”<<credit_limit;

}};

void main()

{

clrscr();

customer cust1;

cust1.read(); // overriding base class in derived class

cust1.print();

}

 

 

O/p:

input name desy

input id 123

Input credit_limit

Name desy

Id 123

Credit_limit 1.37561

 

# include <iostream.h>

# include<conio.h>

class animal

{

public:

void walk()

{

cout<<”\n animal walk”;

}};

class dog:public animal

{

public: void walk()

{

cout<<”\n dog walk”;

}};

class cat:public animal

{

public:

void walk()

{

cout<<”\n Cat walk”;

}};

void main()

{

dog d;

cat c;

d.walk();

c.walk();

}