Fri Nov 22 2019

Multiple Inheritance

C++ Programming2917 views

File Name: multiple-inheritance.cpp

#include<iostream>
using namespace std;

class person {
	string name, gender;
	int age;

	public:
		person(string desig) {
			cout << "Enter details a " << desig << endl;
			cout << "Name:" << endl;
			cin >> name;
			cout << "Age:" << endl;
			cin >> age;
			cout << "Gender:" << endl;
			cin >> gender;
		}
	
		void display(string desig) {
			cout << "Information of a " << desig << endl;
			cout << "Name: " << name << endl;
			cout << "Age: " << age << endl;
			cout << "Gender: " << gender << endl;
		}
};

class employee {
	int emp_id;
	float salary;
	string designation;

	public:
		employee() {
			cout << "Employee Id:" << endl;
			cin >> emp_id;
			cout << "Salary:" << endl;
			cin >> salary;
		}

		inline void get_designation(string desig) {
			designation = desig;
		}

		inline string put_designation() {
			return designation;
		}

		void display() {
			cout << "Employee Id: " << emp_id << endl;
			cout << "Salary: " << salary << endl;
			cout << "Designation: " << designation << endl;
			cout << "\n<------------------------------>\n" << endl;
		}
};

/* Inherit two parent class into one child class */
class scientist : person, employee {
	public:

		/* Call two constructor "person" & "employee" */
		scientist() : person("Scientist"), employee() {
			get_designation("Scientist");
			cout << "\n<------------------------------>\n" << endl;
		}
		
		inline void display() {

			/* Call display() from class 'person' */
			person :: display(put_designation());

			/* Call display() from class 'employee' */
			employee :: display();
		}
};

/* Inherit two parent class into one child class */
class programmer : person, employee {

	public:

		/* Call two constructor "person" & "employee" */
		programmer() : person("Programmer"), employee() {
			get_designation("Programmer");
			cout << "\n<------------------------------>\n" << endl;
		}
		
		inline void display() 		{

			/* Call display() from class 'person' */
			person :: display(put_designation());

			/* Call display() from class 'employee' */
			employee :: display();
		}
};

/* Inherit two parent class into one child class */
class service_man : person, employee {

	public:

		/* Call two constructor "person" & "employee" */
		service_man() : person("Service Man"), employee() {
			string desig;
			cout << "Designation:" << endl;
			cin >> desig;
			get_designation(desig);
			cout << "\n<------------------------------>\n" << endl;
		}
		
		inline void display() {

			/* Call display() from class 'person' */
			person :: display("Service Man");

			/* Call display() from class 'employee' */
			employee :: display();
		}
};

int main() {
	scientist s1 = scientist();
	programmer p1 = programmer();
	service_man sm1 = service_man();
	
	/* Call display() from derived class */
	s1.display();
	p1.display();
	sm1.display();
	return 0;
}	



/* Output */ 
Enter your details a Scientist
Name:
Jonh

Age:
23

Gender:
Male

Employee Id:
1234

Salary:
5000


<------------------------------>


Enter your details a Programmer
Name:
James

Age:
25

Gender:
Male

Employee Id:
5678

Salary:
3000


<------------------------------>


Enter your details a Service Man
Name:
Susan

Age:
30

Gender:
Female

Employee Id:
1359

Salary:
200

Designation:
Nurse


<------------------------------>


Information of a Scientist
Name: Jonh
Age: 23
Gender: Male
Employee Id: 1234
Salary: 5000
Designation: Scientist


<------------------------------>


Information of a Programmer
Name: James
Age: 25
Gender: Male
Employee Id: 5678
Salary: 3000
Designation: Programmer


<------------------------------>


Information of a Service Man
Name: Susan
Age: 30
Gender: Female
Employee Id: 1359
Salary: 200
Designation: Nurse
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.