Multiple Inheritance (Object Oriented Programming)
Multiple Inheritance (Object Oriented Programming)
Inheritance
Program 1
PROBLEM Define a class called Home with two data members used to store the
STATEMENT : number of rooms and the size in square meters. Supply default values for
your constructor definition to create a default constructor. In addition to
accessor methods, also define the method display(), which outputs the data
members of an apartment. Define another class called Car with data
members used to store car number, seating capacity and model name. Add
appropriate constructors and member functions. Define a class derived from
the Car and Home classes called MotorHome, which is used to represent
motorhomes. Inheritance of public base classes is used. The MotorHome
class contains a new data member used to store one value of the
CATEGORY type. CATEGORY represents the categories “Luxury,” “First
Class,” “Middle Class,” and “Economy”.
In addition to defining a constructor with default values, also define
appropriate access methods and a display() method for output.
Write a menu driven main function to test the above scenario.
class Home
{
private:
int num_room;
float area;
public:
Home(int num_room, float area)
{
this->num_room = num_room;
this->area = area;
}
void displayHome()
{
cout<<"Of Home:"<<"\nNumber of rooms: "<<num_room<< endl;
cout<<"Area of room: "<<area<<" m^2"<< endl;
}
};
class Car
{
private:
string car_num, car_mod;
int car_capy;
public:
Car(string car_num, int car_capy, string car_mod)
{
this->car_num = car_num;
this->car_capy = car_capy;
this->car_mod = car_mod;
}
void displaycar()
{
cout<<"\nOf Car:"<<"\nCar Number: "<<car_num<<endl;
cout<<"Name of Model: "<<car_mod<<endl;
cout<<"Seating capacity: "<<car_capy<<endl;
}
};
public:
MotorHome(int num_rooms, float area, string car_num, int car_capy,
string car_mod, string category) : Home(num_rooms, area), Car(car_num,
car_capy, car_mod)
{
this->category = category;
}
void displaymotorhome()
{
displayHome();
displaycar();
cout << "Type of Car Selected: " << category << endl;
}
};
int main()
{
int n;
cout << "Enter the number of rooms: ";
cin >> n;
float ar;
cout << "Enter area of rooms: ";
cin >> ar;
string cn, mn;
int sc;
cout << "Enter Car Number: ";
cin >> cn;
cout << "Enter the Name of Model: ";
cin >> mn;
cout << "Seating Capacity of Car: ";
cin >> sc;
cout << "\nList of Categories of Car: ";
cout << "\n1. Luxury.\n2. First Class.\n3. Second Class.\n4. Economy.";
string cat;
int choice;
cout << "\nCategory of car of your choice: ";
cin >> choice;
switch (choice)
{
case 1:
{
cat = "Luxury";
break;
}
case 2:
{
cat = "First Class";
break;
}
case 3:
{
cat = "Second Class";
break;
}
case 4:
{
cat = "Economy";
break;
}
default:
break;
}
MotorHome mh(n,ar,cn,sc,mn,cat);
cout<<"\nThe Details are: \n";
mh.displaymotorhome();
return 0;
}
RESULT:
INPUT/ OUTPUT: