Saturday, June 17, 2023

INHERITANCE IN CPP

 

C++ Inheritance

In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically.

Inheritance called 3 different names



Advantage of C++ Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.

Types Of Inheritance

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance


C++ Single Inheritance



Syntax

class baseclass

{

member;

};

class derived:access specifier baseclass

{

member;

};

 

#include <iostream.h> 

#include <conio.h> 

 class father

{

   public:

    void house()

    {

        cout<<"Have own House."<<endl;

    }

};

class son:public father

{

    public:

    void car()

    {

        cout<<"Have own Car."<<endl;

    }

};

void main()

{

    son o;

    clrscr();

    o.house();

    o.car();

   getch();

}

 

 

Output:

Have own House

Have own Car.

C++ Multilevel Inheritance



Syntax

class baseclass

{

member;

};

class derived1:access specifier baseclass

{

member;

};

class derived2:access specifier derivedclass1

{

member;

};

 

#include<iostream.h>

#include<conio.h>

class train{

private:

char train_no[10];

char train_name[15];

float train_fare;

public:

void getTrainDetails()

{

cout<<"Type Train Number";

cin>>train_no;

cout<<"type train name";

cin>>train_name;

cout<<"type fare";

cin>>train_fare;

}

void printTrainDetails()

{

cout<<"Train number:"<<train_no<<endl;

cout<<"Train Name:"<<train_name<<"\n";

cout<<"Train fare:"<<train_fare<<endl;

}

};

class ElectricTrain:public train

{

private:

int coaches;

int seatspercoach;

public:

void getCoachDetails()

{

cout<<"get number of coaches";

cin>>coaches;

cout<<"get number of seat per coach";

cin>>seatspercoach;

}

void printCoachDetails()

{

cout<<"number of coaches="<<coaches<<"\n";

cout<<"number of seat per coach="<<seatspercoach<<"\n";

}

};

class Passenger:public ElectricTrain

{

private:

char pname[40];

int age;

public:

void getPassengerDetails()

{

cout<<"get name and age";

cin>>pname>>age;

}

void printPassengerDetails()

{

cout<<"Passenger Name"<<pname<<"\n";

cout<<"Age"<<age<<"\n";

}

};

void main()

{

clrscr();

Passenger P;

P.getTrainDetails();

P.getCoachDetails();

P.getPassengerDetails();

P.printTrainDetails();

P.printCoachDetails();

P.printPassengerDetails();

getch();

}

 

Output:




C++ Multiple Inheritance




Syntax of Multiple Inheritance in C++

class BaseClass1 { 

    // Data members of BaseClass1.

    // Member functions of BaseClass1.

} ;

class BaseClass2 { 

    // Data members of BaseClass2.

    // Member functions of BaseClass2.

} ;

 

// Multiple inheritance

class DerivedClass: public BaseClass1, public BaseClass2 {

    // Data members of DerivedClass.

    // Member functions of DerivedClass.

}  ;

Example

 

#include<iostream.h>

#include<conio.h>

class Item{

int Itemno;

int rate;

public:

void getItem()

{

cout<<"get Itemno and rate";

cin>>Itemno>>rate;

}

void putItem()

{

cout<<"Itemno="<<Itemno<<endl;

cout<<"Rate="<<rate<<endl;

}

};

class ItemDetail

{

char desc[40];

int qty;

public:

void getItemDetail()

{

cout<<"Description and qty";

cin>>desc>>qty;

}

void putItemDetail()

{

cout<<"Desc="<<desc<<endl;

cout<<"Qty="<<qty<<endl;

}

};

class cust:public Item,public ItemDetail

{

char custname[40];

public:

void get()

{

cout<<"enter customer name";

cin>>custname;

}

void put()

{

cout<<custname<<endl;

}

};

void main()

{

clrscr();

cust K;

K.getItem();

K.getItemDetail();

K.get();

K.putItem();

K.putItemDetail();

K.put();

getch();

}

Output



 

C++ Hierarchical Inheritance

 


Syntax

class baseclass

{

member;

};

class derived1:access specifier baseclass

{

member;

};

class derived2:access specifier baseclass

{

member;

};

class derived3:access specifier baseclass

{

member;

};

 

 

#include<iostream.h>

#include<conio.h>

class company

{

private:

char name[40];

public:

void get()

{

cout<<"Get Company Nmae";

cin>>name;

}

void put()

{

cout<<"Company Name="<<name;

}

};

class RegularEmployee:public company

{

int empno;

char ename[50];

float sal;

public:

void getemp()

{

cout<<"get employee details one by one";

cin>>empno>>ename>>sal;

}

void putemp()

{

cout<<empno<<ename<<sal;

}

};

class labour:public company

{

int code;

char name[40];

float wage;

public:

void getdetail()

{

cout<<"Get Code,Name,wage";

cin>>code>>name>>wage;

}

void putdetail()

{

cout<<"Code="<<code<<"\n";

cout<<"Name="<<name<<"\n";

cout<<"Wages="<<wage<<"\n";

}

};

void main()

{

clrscr();

labour L;

L.get();

L.getdetail();

L.put();

L.putdetail();

RegularEmployee R;

R.getemp();

R.putemp();

getch();

}

Output



Hybrid Inheritance



class baseclass

{

member;

};

class derived1:access specifier baseclass

{

member;

};

class derived2

{

member;

};

class derived3:access specifier derived1, access specifier derived2

{

member;

};

 

 

 

 

#include<iostream.h>

#include<conio.h>

class stu{ //First base Class//

    int id;

    char name[20];

    public:

    void getstu(){

         cout << "Enter stuid, name";

         cin >> id >> name;

    }

};

 

class marks: public stu{//derived class//

    protected:

    int m, p, c;

    public:

    void getmarks(){

         cout << "Enter 3 subject marks:";

         cin >> m >> p >> c;

    }

};

 

class sports{//Second base class//

    protected:

    int spmarks;

    public:

    void getsports(){

         cout << "Enter sports marks:";

         cin >> spmarks;

    }

};

 

class result : public marks, public sports{//Derived class by hybrid inheritance//

    int tot;

    float avg;

    public :

    void show(){

         tot=m+p+c;

         avg=tot/3.0;

         cout << "Total=" << tot << endl;

         cout << "Average=" << avg << endl;

         cout << "Average + Sports marks =" << avg+spmarks;

    }

};

 

void main(){

    result r;//object//

    r.getstu();

    r.getmarks();

    r.getsports();

    r.show();

    getch();

}

 

output



0 comments:

Post a Comment