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

Inheritance Program

The document provides examples of different types of inheritance in C++ including single inheritance, multilevel inheritance, multiple inheritance, and hybrid inheritance. Single inheritance examples show inheriting fields and methods. Multilevel inheritance builds upon single inheritance by inheriting from a parent and grandparent class. Multiple inheritance inherits from multiple parent classes. Hybrid inheritance combines multilevel and multiple inheritance with an example inheriting from two parent classes and a grandparent class.

Uploaded by

amolia2004
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)
42 views

Inheritance Program

The document provides examples of different types of inheritance in C++ including single inheritance, multilevel inheritance, multiple inheritance, and hybrid inheritance. Single inheritance examples show inheriting fields and methods. Multilevel inheritance builds upon single inheritance by inheriting from a parent and grandparent class. Multiple inheritance inherits from multiple parent classes. Hybrid inheritance combines multilevel and multiple inheritance with an example inheriting from two parent classes and a grandparent class.

Uploaded by

amolia2004
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/ 18

// C++ Single Level Inheritance Example: Inheriting Fields

// When one class inherits another class, it is known as single level inheritance. Let's see the example of single level inheritance which
inherits the fields only.

#include <iostream>

using namespace std;

class Account
{
public:
float salary = 60000;
};

class Programmer: public Account


{
public:
float bonus = 5000;
};

int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
// C++ Single Level Inheritance Example: Inheriting Methods

#include <iostream>

using namespace std;

class Animal
{
public:
void eat()
{
cout<<"Eating..."<<endl;
}
};

class Dog: public Animal


{
public:
void bark()
{
cout<<"Barking...";
}
};

int main(void)
{
Dog d1;
d1.eat();
d1.bark();
return 0;
}
// class A is privately inherited. Therefore, the mul() function of class 'A' cannot be accessed by the
object of class B. It can only be accessed by the member function of class B.

#include <iostream>

using namespace std;

class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};

class B : private A
{
public:
void display()
{
int result = mul();
std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
}
};
int main()
{
B b;
b.display();

return 0;
}
Inheritance Type Programs

// C++ Single Level Inheritance Example:

#include <iostream>

using namespace std;

class Animal
{
public:
void eat()
{
cout<<"Eating..."<<endl;
}
};

class Dog: public Animal


{
public:
void bark()
{
cout<<"Barking...";
}
};

int main(void)
{
Dog d1;
d1.eat();
d1.bark();
return 0;
}
//Single Inheritance program demonstration

#include <iostream>

using namespace std;

class Publisher
{
string pname;
string place;

public:

void getdata()
{
cout<<"Enter name and place of publisher:"<<endl;
cin>>pname>>place;
}

void show()
{
cout<<"Publisher Name:"<<pname<<endl;
cout<<"Place:"<<place<<endl;
}

};

class Book:public Publisher


{
string title;
float price;
int pages;

public:

void getdata()
{
Publisher::getdata();
cout<<"Enter Book Title, Price and No. of pages"<<endl;
cin>>title>>price>>pages;
}

void show()
{
Publisher:: show ();
cout<<"Title:"<<title<<endl;
cout<<"Price:"<<price<<endl;
cout<<"No. of Pages:"<<pages<<endl;
}

};

int main()
{
Book b;
b.getdata();
b.show();
return 0;
}
// Multiple inheritance is the process of deriving a new class that inherits the attributes from two or
more classes.
#include <iostream>
using namespace std;

class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
cout << "The value of a is : " <<a<< endl;
cout << "The value of b is : " <<b<< endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
}
//This is example of Multilevel Inheritance

#include <iostream>

using namespace std;

class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};

class B: public A
{
protected:
int b;
public:
void get_b(int n)
{
a=a+5;
b = n;
}
};
class C :public B
{
public:
void display()
{
cout << "The value of a is : " <<a<<endl;
cout << "The value of b is : " <<b<<endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C obj1;
obj1.get_a(10);
obj1.get_b(20);
obj1.display();

return 0;
}
// Hierarchical inheritance (Multilevel) is defined as the process of deriving more than one class
from a base class.

#include <iostream>

using namespace std;

class Shape // Declaration of base class.


{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = a*b;
return result;
}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
cout << "Enter the length and breadth of a rectangle: " << endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
cout << "Area of the rectangle is : " <<m<< endl;
cout << "Enter the base and height of the triangle: " << endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
cout <<"Area of the triangle is : " << n<<endl;
return 0;
}
// C++ Hybrid Inheritance

// Hybrid inheritance is a combination of more than one type of inheritance.

#include <iostream>

using namespace std;

class A
{
protected:
int a;
public:
void get_a()
{
cout << "Enter the value of 'a' : " << endl;
cin>>a;
}
};

class B : public A
{
protected:
int b;
public:
void get_b()
{
cout << "Enter the value of 'b' : " <<endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
cout << "Enter the value of c is : " << endl;
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<< endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}
// C++ program to demonstrate the working of protected inheritance

#include <iostream>
using namespace std;

class Base {
private:
int pvt = 1;

protected:
int prot = 2;

public:
int pub = 3;

// function to access private member


int getPVT() {
return pvt;
}
};

class ProtectedDerived : protected Base {


public:
// function to access protected member from Base
int getProt()
{
return prot;
}

// function to access public member from Base


int getPub()
{
return pub;
}
};

int main()
{
ProtectedDerived object1;
//cout<<object1.pvt;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
//Hybrid inheritance = multilevel + multilpe

#include <iostream>

using namespace std;

//Hybrid inheritance = multilevel + multilpe


class student
{ //First base Class
int id;
string name;
public:
void getstudent()
{
cout << "Enter student Id and student name";
cin >> id >> name;
}
};

class marks: public student


{ //derived from student
protected:
int marks_math,marks_phy,marks_chem;
public:
void getmarks()
{
cout << "Enter 3 subject marks:";
cin >>marks_math>>marks_phy>>marks_chem;
}
};

class sports
{
protected:
int spmarks;
public:
void getsports()
{
cout << "Enter sports marks:";
cin >> spmarks;
}
};

class result : public marks, public sports


{//Derived class by multiple inheritance//
int total_marks;
float avg_marks;
public :
void display()
{
total_marks=marks_math+marks_phy+marks_chem;
avg_marks=total_marks/3.0;
cout << "Total marks =" << total_marks<< endl;
cout << "Average marks =" << avg_marks<< endl;
cout << "Average + Sports marks =" <<avg_marks+spmarks;
}
};

int main()
{
result res;//object//
res.getstudent();
res.getmarks();
res.getsports();
res.display();

return 0;
}

You might also like