Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
76 views

Multiple Inheritance (Object Oriented Programming)

The document defines classes for Home, Car, and MotorHome that inherits from Home and Car. Home has number of rooms and area data members, Car has car number, seating capacity, and model name. MotorHome inherits these and adds a category data member. A menu-driven program tests object creation and displays MotorHome details by calling parent class display methods.

Uploaded by

krushi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Multiple Inheritance (Object Oriented Programming)

The document defines classes for Home, Car, and MotorHome that inherits from Home and Car. Home has number of rooms and area data members, Car has car number, seating capacity, and model name. MotorHome inherits these and adds a category data member. A menu-driven program tests object creation and displays MotorHome details by calling parent class display methods.

Uploaded by

krushi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

AIM: Program on Inheritance: Implement a Program to demonstrate multiple

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.

PROGRAM: #include <iostream>


using namespace std;

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;
}
};

class MotorHome : public Home, public Car


{
private:
string category;

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:

You might also like