0% found this document useful (0 votes)
85 views25 pages

Single Multiple, Multilevel Hybrid, Hirarchical Inheritance Programs

The document discusses different types of inheritance in C++ including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides code examples to demonstrate how each type of inheritance works by defining base and derived classes and invoking methods to show the inheritance relationships.

Uploaded by

Beluga GamerTM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views25 pages

Single Multiple, Multilevel Hybrid, Hirarchical Inheritance Programs

The document discusses different types of inheritance in C++ including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides code examples to demonstrate how each type of inheritance works by defining base and derived classes and invoking methods to show the inheritance relationships.

Uploaded by

Beluga GamerTM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Single Inheritance, and multiple inheritance program

// C++ program to explain

// Single inheritance

#include<iostream>

using namespace std;

// base class

class Vehicle {

public:

Vehicle()

cout << "This is a Vehicle\n";

};

// sub class derived from a single base classes

class Car : public Vehicle {


};

// main function

int main()

// Creating object of sub class will

// invoke the constructor of base classes

Car obj;

return 0;

Output
This is a Vehicle

 C++

// Example:

#include<iostream>

using namespace std;


class A

protected:

int a;

public:

void set_A()

cout<<"Enter the Value of A=";

cin>>a;

void disp_A()

cout<<endl<<"Value of A="<<a;

};
class B: public A

int b,p;

public:

void set_B()

set_A();

cout<<"Enter the Value of B=";

cin>>b;

void disp_B()

disp_A();

cout<<endl<<"Value of B="<<b;

void cal_product()
{

p=a*b;

cout<<endl<<"Product of "<<a<<" * "<<b<<" = "<<p;

};

main()

B _b;

_b.set_B();

_b.cal_product();

return 0;

Output:- Enter the Value of A= 3 3 Enter the Value of B= 5 5 Product of 3 * 5 = 15

 C++
// Example:

#include<iostream>

using namespace std;

class A

protected:

int a;

public:

void set_A(int x)

a=x;

void disp_A()

{
cout<<endl<<"Value of A="<<a;

};

class B: public A

int b,p;

public:

void set_B(int x,int y)

set_A(x);

b=y;

void disp_B()

disp_A();

cout<<endl<<"Value of B="<<b;
}

void cal_product()

p=a*b;

cout<<endl<<"Product of "<<a<<" * "<<b<<" = "<<p;

};

main()

B _b;

_b.set_B(4,5);

_b.cal_product();

return 0;

Output
Product of 4 * 5 = 20
// C++ program to explain

// multiple inheritance

#include <iostream>

using namespace std;

// first base class

class Vehicle {

public:

Vehicle() { cout << "This is a Vehicle\n"; }

};

// second base class

class FourWheeler {

public:

FourWheeler()

cout << "This is a 4 wheeler Vehicle\n";


}

};

// sub class derived from two base classes

class Car : public Vehicle, public FourWheeler {

};

// main function

int main()

// Creating object of sub class will

// invoke the constructor of base classes.

Car obj;

return 0;

Output
This is a Vehicle
This is a 4 wheeler Vehicle

 C++
// Example:

#include<iostream>

using namespace std;

class A

protected:

int a;

public:

void set_A()

cout<<"Enter the Value of A=";

cin>>a;

}
void disp_A()

cout<<endl<<"Value of A="<<a;

};

class B: public A

protected:

int b;

public:

void set_B()

cout<<"Enter the Value of B=";

cin>>b;

}
void disp_B()

cout<<endl<<"Value of B="<<b;

};

class C: public B

int c,p;

public:

void set_C()

cout<<"Enter the Value of C=";

cin>>c;

void disp_C()

{
cout<<endl<<"Value of C="<<c;

void cal_product()

p=a*b*c;

cout<<endl<<"Product of "<<a<<" * "<<b<<" * "<<c<<" = "<<p;

};

main()

C _c;

_c.set_A();

_c.set_B();

_c.set_C();

_c.disp_A();

_c.disp_B();
_c.disp_C();

_c.cal_product();

return 0;

}
// C++ program to implement

// Multilevel Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle {

public:

Vehicle() { cout << "This is a Vehicle\n"; }

};

// first sub_class derived from class vehicle

class fourWheeler : public Vehicle {

public:

fourWheeler()

cout << "Objects with 4 wheels are vehicles\n";

}
};

// sub class derived from the derived base class fourWheeler

class Car : public fourWheeler {

public:

Car() { cout << "Car has 4 Wheels\n"; }

};

// main function

int main()

// Creating object of sub class will

// invoke the constructor of base classes.

Car obj;

return 0;

Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
// C++ program to implement

// Hierarchical Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle {

public:

Vehicle() { cout << "This is a Vehicle\n"; }

};

// first sub class

class Car : public Vehicle {

};

// second sub class

class Bus : public Vehicle {

};
// main function

int main()

// Creating object of sub class will

// invoke the constructor of base class.

Car obj1;

Bus obj2;

return 0;

Output
This is a Vehicle
This is a Vehicle

// C++ program for Hybrid Inheritance

#include <iostream>

using namespace std;


// base class

class Vehicle {

public:

Vehicle() { cout << "This is a Vehicle\n"; }

};

// base class

class Fare {

public:

Fare() { cout << "Fare of Vehicle\n"; }

};

// first sub class

class Car : public Vehicle {

};

// second sub class

class Bus : public Vehicle, public Fare {


};

// main function

int main()

// Creating object of sub class will

// invoke the constructor of base class.

Bus obj2;

return 0;

Output
This is a Vehicle
Fare of Vehicle

 C++

// Example:

#include <iostream>

using namespace std;


class A

protected:

int a;

public:

void get_a()

cout << "Enter the value of 'a' : ";

cin>>a;

};

class B : public A

protected:

int b;

public:

void get_b()
{

cout << "Enter the value of 'b' : ";

cin>>b;

};

class C

protected:

int c;

public:

void get_c()

cout << "Enter the value of c is : ";

cin>>c;

};

class D : public B, public C

{
protected:

int d;

public:

void mul()

get_a();

get_b();

get_c();

cout << "Multiplication of a,b,c is : " <<a*b*c;

};

int main()

D d;

d.mul();

return 0;

You might also like