0% found this document useful (0 votes)
44 views

Inheritance Program

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

Uploaded by

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

Inheritance Program

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

Uploaded by

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

INHERITANCE PROGRAM

SINGLE INHERITANCE:
EXAMPLE 1:

#include<iostream>

using namespace std;

class Vehicle {

public:

Vehicle()

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

};

class Car : public Vehicle {

};

int main()

Car obj;

return 0;

EXAMPLE 2:

#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:

EXAMPLE 3:
#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:

Multiple inheritance:
Program 4:
#include <iostream>

using namespace std;

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:

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;

Output:

MULTI LEVEL INHERITANCE


Program 6:
#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:

Hierarchical Inheritance:
Program 7:
#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:

Hybrid inheritance:-
Example:
#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:

You might also like