Cpp Programs On Inheritance, Codes On Inheritance, Online Test Q’s
Hello guys, welcome back to my blog. In this article, I will share some Cpp programs on inheritance codes on Inheritance using Cpp and these types of questions are asked in the online tests.
If you have any doubts related to electrical, electronics, and computer science, then ask questions. You can also catch me on Instagram – CS Electrical & Electronics And Chetan Shidling.
Also, read:
- Difference Between Java And Cpp Programming Languages, Applications
- Top 10 Applications Of C++ In Different Domains, Cpp Applications
- What Is Vectors, Benefits Of Using Vectors, Cpp Code On Vectors
Cpp Programs On Inheritance
01. Consider a scenario such as ‘Game Companies’. A game company can be:
A. a software developer, like Platinum Games
B. a publisher, like for instance Agetec
C. a developer AND publisher at the same time, as in Blizzard Entertainment
D. all of the above, i.e. Nintendo
If you have to model this using C++ programming features, which feature would you select and implement? Also, a company can be multiple of those at the same time.
#include<iostream>
using namespace std;
class cssoftware{
public:
void put()
{
cout<<"A game company is a cssoftware developer"<<endl;
}
};
class cspublisher{
public:
void show()
{
cout<<"A game company is a publisher"<<endl;
}
};
class both:public cssoftware,public cspublisher{
public:
void print()
{
cout<<"A game company is a cssoftware developer and publisher"<<endl;
}
};
class all:public cssoftware,public cspublisher,public both{
public:
void display()
{
cout<<"A game company is a cssoftware developer,cspublisher,cssoftware and cspublisher also"<<endl;
}
};
int main()
{
int ch;
cssoftware a;
cspublisher b;
both c;
all d;
cout<<"enter the type of comapny"<<endl;
cout<<"1.Want only cssoftware";
cout<<endl;
cout<<"2.Want only cspublisher"<<endl;
cout<<"3. Want both"<<endl<<"4.all"<<endl;
cin>>ch;
if(ch==1)
a.put();
else if(ch==2)
b.show();
else if(ch==3)
c.print();
else if(ch==4)
d.display();
else
cout<<"INVALID"<<endl;
}
Output:
enter the type of comapny
1.Want only cssoftware
2.Want only cspublisher
3. Want both
4.all
2
A game company is a publisher
02. Consider the following classes, Parent class 1 “team member”, Parent class 2 “Worker” and Deriving a child class from the two-parent classes “TeamLeader”. Implement the specified scenario using the appropriate C++ inheritance feature. Each class should have at least 3 relevant data members and member functions.
#include<iostream>
using namespace std;
class csteam_member{
protected:
string name;
int team_id;
string status;
csteam_member()
{
cout<<"enter team member name"<<endl;
cin>>name;
cout<<"enter team member ID"<<endl;
cin>>team_id;
cout<<"enter the status of team member"<<endl;
cin>>status;
}
void display_member()
{
cout<<"name:"<<name<<endl;
cout<<"Status:"<<status<<endl;
cout<<"team_id:"<<team_id<<endl;
}
};
class worker{
protected:
string name;
int worker_id;
string status;
worker()
{
cout<<"enter worker name "<<endl;
cin>>name;
cout<<"enter ID of worker"<<endl;
cin>>worker_id;
cout<<"enter the status"<<endl;
cin>>status;
}
void display_worker()
{
cout<<"name:"<<name<<endl;
cout<<"Status:"<<status<<endl;
cout<<"team_id:"<<worker_id<<endl;
}
};
class csteam_leader:protected csteam_member,protected worker{
public:
string name;
int leader_id;
string status;
public:
csteam_leader()
{
cout<<"enter the name of the leader"<<endl;
cin>>name;
cout<<"enter ID"<<endl;
cin>>leader_id;
cout<<"enter the status"<<endl;
cin>>status;
//display();
}
void display()
{
cout<<"team leader details"<<endl;
cout<<"name:"<<name<<endl;
cout<<"Status:"<<status<<endl;
cout<<"team_id:"<<worker_id<<endl;
cout<<"---------------------------------------"<<endl;
cout<<"worker under this team leader"<<endl;
display_worker();
cout<<"-----------------------------------------"<<endl;
cout<<"team member under this leader"<<endl;
display_member();
}
};
int main()
{
csteam_leader t1;
t1.display();
return 0;
}
Output:
enter team member name
india
enter team member ID
12
enter the status of team member
good
enter worker name
Chetan
enter ID of worker
420
enter the status
good
enter the name of the leader
Chetu
enter ID
123
enter the status
goof
team leader details
name:Chetu
Status:goof
team_id:420
---------------------------------------
worker under this team leader
name:Chetan
Status:good
team_id:420
-----------------------------------------
team member under this leader
name:india
Status:good
team_id:12
03. KLE Technological University has to start the first year Admission process in the month of May 2020 for the year 2020-2021. Admission has to be accepted for students appearing in the different entrance exams. University has approved to consider the students who have cleared the entrance exam in one of the following: KCET, GATE, COMEDK, or JEE. University has approved to consider guidelines for each entrance exam. Implement the above scenario considering entrance exams as parent classes and University as child classes. If a student approaches admission who has cleared any one of these exams, verify the information of the student and decide whether he/she can be provided admission to University.
#include<iostream>
using namespace std;
class CS_KCET{
protected:
char ask;
int K_Rank;
CS_KCET()
{
cout<<"Are you appeared for KCET?"<<endl;
cin>>ask;
if(ask=='yes')
{
cout<<"Rank?:"<<endl;
cin>>K_Rank;
if(K_Rank<=10000)
cout<<"candidate is eligible to take admission"<<endl;
else
cout<<"ineligible"<<endl;
}
}
};
class CS_GATE{
protected:
char ask;
int G_Rank;
CS_GATE(){
cout<<"ARE u appeared for GATE?"<<endl;
cin>>ask;
if(ask=='yes')
{
cout<<"Rank?:"<<endl;
cin>>G_Rank;
if(G_Rank<=500)
cout<<"candidate is eligible to take admission"<<endl;
else
cout<<"ineligible"<<endl;
}
}
};
class CS_COMEDK{
protected:
char ask;
int C_Rank;
CS_COMEDK()
{
cout<<"Are you appeared for COMEDK?"<<endl;
cin>>ask;
if(ask=='yes')
{
cout<<"Rank?:"<<endl;
cin>>C_Rank;
if(C_Rank<=1500)
cout<<"candidate is eligible to take admission"<<endl;
else
cout<<"ineligible"<<endl;
}
}
};
class CS_university:protected CS_KCET,protected CS_GATE,protected CS_COMEDK
{
public:
string name;
CS_university()
{
if(K_Rank<=10000||G_Rank<=500||C_Rank<=1500)
{
cout<<"enter the name of the student"<<endl;
cin>>name;
cout<<"you are enrolled!"<<endl;
}
else
cout<<"THANK YOU"<<endl;
}
};
int main()
{
CS_university s1;
return 0;
}
Output:
Are you appeared for KCET?
YES
ARE u appeared for GATE?
Are you appeared for COMEDK?
enter the name of the student
nAME
you are enrolled!
04. Consider we have three classes – Car, Maruti, and MarutiSwift. We have done a setup – class Maruti extends Car and class MarutiSwift extends Maruti. With the help of this, setup the MarutiSwift class should be able to use the methods of both the classes (Car and Maruti). We should be able to display the basic features of a Car, new features added in Maruti and also specific features of MarutiSwift
#include<iostream>
using namespace std;
class Car{
protected:
int cylinder=1;
int range=25;
int cost=20000;
void show_car()
{
cout<<"car details"<<endl;
cout<<"number of cylinders:"<<cylinder<<endl;
cout<<"range of the vehicle"<<range<<endl;
cout<<"cost of the vehicle"<<cost<<endl;
}
};
class Maruti:protected Car{
protected:
char AC = 'YES';
char steering = 'Mechanical';
void show_Maruti()
{
cout<<"Basic features of Maruti car"<<endl;
cout<<"number of cylinders:"<<cylinder<<endl;
cout<<"range of the vehicle"<<range<<endl;
cout<<"cost of the vehicle"<<cost<<endl;
cout<<" ***** "<<endl;
cout<<"Added features of Maruti car details"<<endl;
cout<<"Presence of AC?:"<<AC<<endl;
cout<<"type of steering:"<<steering<<endl;
}
};
class Maruti_swift:protected Maruti{
protected:
char Braking:'ABS';
char steering='power';
public:
void show_Maruti_swift()
{
show_car();
cout<<"---------------------------------------------------------------------"<<endl;
show_Maruti();
cout<<"---------------------------------------------------------------------"<<endl;
cout<<"Basic features of Maruti Swift car"<<endl;
cout<<"number of cylinders:"<<cylinder<<endl;
cout<<"range of the vehicle"<<range<<endl;
cout<<"cost of the vehicle"<<cost<<endl;
cout<<"Presence of AC?:"<<AC<<endl;
cout<<" ***** "<<endl;
cout<<"Added features of Maruti swift car details"<<endl;
cout<<"Brakingh type"<<Braking;
cout<<"type of steering:"<<steering<<endl;
cout<<"----------------------------------------------------------------------"<<endl;
}
};
int main()
{
Maruti_swift C1;
C1.show_Maruti_swift();
return 0;
}
Output:
car details
number of cylinders:1
range of the vehicle25
cost of the vehicle20000
---------------------------------------------------------------------
Basic features of Maruti car
number of cylinders:1
range of the vehicle25
cost of the vehicle20000
*****
Added features of Maruti car details
Presence of AC?:S
type of steering:l
---------------------------------------------------------------------
Basic features of Maruti Swift car
number of cylinders:1
range of the vehicle25
cost of the vehicle20000
Presence of AC?:S
*****
Added features of Maruti swift car details
Brakingh type type of steering:r
05. Consider a scenario of a parent-child relationship. We have declared three classes Father class, Son class, and Grandson class. Father class have protected variable ‘a’ which is inherited in Son class, therefore Son class have two protected data members ‘a’ and ‘b’, GrandSon class has three data members ‘a’, ‘b’ and ‘c’. ‘a’ and ‘b’ is inherited and ‘c’ is its own data member. In the main function, we create a GrandSon object and call the function “Show()” which will add the inherited members of the GrandSon class. Implement the scenario applying a suitable inheritance feature
#include<iostream>
using namespace std;
class father{
protected:
char f='a';
};
class son:protected father
{
protected:
char s='b';
};
class grand_son:protected son
{
protected:
char g='c';
public:
void show()
{
cout<<"The character of grandson are-----"<<f<<"\t"<<s<<"\t"<<g<<endl;
}
};
int main()
{
grand_son p1;
p1.show();
return 0;
}
Code:
The character of grandson are-----a b c
06. Create a class called Car to store company name, headquarters, year of establishment. Derive 2 specific classes – petrol car and electric car with additional required members. Add to the base class, a member function get details() to initialize base class data members. and another member function display details() to compute mileage and display along with details for both types of cars. Make display details() as virtual functions in derived classes to suit their requirements. Design a C++ program to perform the above operations.
#include<iostream>
using namespace std;
class car
{
protected:
string name;
string head_quarter;
int year;
public:
void getdetails()
{
cout<<"Enter the Company Name:"<<endl;
cin>>name;
cout<<"Enter the name of Headquarters:"<<endl;
cin>>head_quarter;
cout<<"Enter Year of Establishment:"<<endl;
cin>>year;
}
virtual void displaydetails()=0;
};
class petrol_car: public car
{
int capacity;
int milage;
public:
void displaydetails()
{
cout<<"Enter the capacity of car:"<<endl;
cin>>capacity;
cout<<"Enter the milage of car:"<<endl;
cin>>milage;
cout<<"Company Name:"<<name<<endl;
cout<<"Headquarters:"<<head_quarter<<endl;
cout<<"Year of establishment:"<<year<<endl;
cout<<"Milage of petrol car="<<milage<<"km/l"<<endl;
}
};
class electric_car: public car
{
int capacity;
int milage;
public:
void displaydetails()
{
cout<<"Enter battery ah:"<<endl;
cin>>capacity;
cout<<"Enter milage"<<endl;
cin>>milage;
cout<<"Milage of electric car="<<milage<<"Km/Wh"<<endl;
cout<<"Company Name:"<<name<<endl;
cout<<"Headquarters:"<<head_quarter<<endl;
cout<<"Year of establishment:"<<year<<endl;
}
};
int main()
{
petrol_car p;
electric_car e;
int ch;
while(1)
{
cout<<"Enter choice:"<<endl;
cout<<"1.Petrol car"<<endl;
cout<<"2. Electric car"<<endl;
cin>>ch;
switch(ch)
{
case 1:p.getdetails();
p.displaydetails();
break;
case 2:e.getdetails();
e.displaydetails();
break;
default: break;
}
}
return 0;
}
Output:
Enter choice:
1.Petrol car
2. Electric car
1
Enter the Company Name:
Maruti
Enter the name of Headquarters:
Dehli
Enter Year of Establishment:
2000
Enter the capacity of car:
222
Enter the milage of car:
12
Company Name:Maruti
Headquarters:Dehli
Year of establishment:2000
Milage of petrol car=12km/l
Enter choice:
1.Petrol car
2. Electric car
I hope this article “Cpp Programs On Inheritance” may help you all a lot. Thank you for reading.
Also, read:
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- App Developers, Skills, Job Profiles, Scope, Companies, Salary
- Applications Of Artificial Intelligence (AI) In Renewable Energy
- Applications Of Artificial Intelligence, AI Applications, What Is AI
- Applications Of Data Structures And Algorithms In The Real World
- Array Operations In Data Structure And Algorithms Using C Programming
- Artificial Intelligence Scope, Companies, Salary, Roles, Jobs