Answer to Question #235213 in C++ for SOM

Question #235213

WAPs in C++ which creates a multilevel inheritance hierarchy of Person, Employee and Teacher classes by explaining differences between public, private and protected inheritances with public, private and protected data.


1
Expert's answer
2021-09-13T18:29:43-0400
#include<iostream>
#include<bits/stdc++.h>
using namespace std;


class Person{
	
	private:
	string lastName;
	
	public:
		string name;
		void setName(string s)
		{
			name = s;
		}
};


class Employee : public Person{
	public:
	string profession;
		
	public:
		int ID;
		
		void setID(int n)
		{
			ID = n;
		}
		
};




class Teacher : private Employee{
	private:
		int teacherCode;
		
	public:
		string subject;
		void setSubj(string s)
		{
			subject = s;
		}
};




int main()
{
	Teacher t1;


//	t1.setID(10);    ---->this will not work as function setID is private for class Teacher 
//	cout<<t1.ID;       so we cannot access the memeber functions of class Employee outside the class


	t1.setSubj("math");
	cout<<t1.subject;
	
//	t1.setName("ABC"); ----> this will not work since class Employee is inherited private 
//								so all features of class Person are also inherited private 
//	cout<<t1.name;   			so we cannot access class Person member functions outside class Teacher
	
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS