In traditional way, there are three classes: TRUCK (number, name , NoOfWheel, Price, Capacity, EnginePower, TypeOfConsump), BUS (number, name , NoOfWheel, Price, NoOfSeat, EnginePower, TypeOfConsump, Color, Speed), CAR (number, name , NoOfWheel, Price, NoOfSeat, EnginePower, TypeOfConsump, Color, Speed).
Divide the above three classes into two base classes and three derived classes. Each class will contain two member functions, getdata() to get that class data from user and putdata() to display that class data on the screen
#include <iostream>
#include <string>
using namespace std;
class VEHICLE{
private:
//(number, name , NoOfWheel, Price, NoOfSeat, EnginePower, TypeOfConsump)
string number;
string name;
int noOfWheel;
float price;
float enginePower;
string typeOfConsump;
public:
VEHICLE(){}
~VEHICLE(){}
virtual void getdata()
{
cout << "Enter number: ";
getline(cin,number);
cout << "Enter name: ";
getline(cin,name);
cout << "Enter no of wheel: ";
cin >> noOfWheel;
cout << "Enter price: ";
cin >> price;
cout << "Enter engine power: ";
cin >> enginePower;
cin.ignore();
cout << "Enter type of consump: ";
getline(cin,typeOfConsump);
}
virtual void putdata()
{
cout << "Number: " << number << "\n";
cout << "Name: " << name << "\n";
cout << "No of wheel: " << noOfWheel << "\n";
cout << "Price: " << price << "\n";
cout << "Engine power: " << enginePower << "\n";
cout << "Type of consump: " << typeOfConsump << "\n";
}
};
class CAR: public VEHICLE{
private:
//(number, name , NoOfWheel, Price, NoOfSeat, EnginePower, TypeOfConsump, Color, Speed)
int noOfSeat;
string color;
int speed;
public:
CAR(){}
~CAR(){}
void getdata()
{
VEHICLE::getdata();
cout << "Enter no of seat: ";
cin>>noOfSeat;
cin.ignore();
cout << "Enter color: ";
getline(cin,color);
cout << "Enter speed: ";
cin >> speed;
cin.ignore();
}
void putdata()
{
VEHICLE::putdata();
cout << "No of seat: " << noOfSeat << "\n";
cout << "Color: " << color << "\n";
cout << "Speed: " << speed << "\n";
}
};
class BUS: public CAR{
public:
BUS(){}
~BUS(){}
void getdata()
{
CAR::getdata();
}
void putdata()
{
CAR::putdata();
}
};
class TRUCK:public VEHICLE
{
private:
//(number, name , NoOfWheel, Price, Capacity, EnginePower, TypeOfConsump)
float capacity;
public:
TRUCK(){}
~TRUCK(){}
void getdata()
{
VEHICLE::getdata();
cout << "Enter capacity: ";
cin >> capacity;
cin.ignore();
}
void putdata()
{
VEHICLE::putdata();
cout << "Capacity: " << capacity << "\n";
}
};
int main(){
cout<<"Enter information about CAR:\n";
CAR* newCAR=new CAR();
newCAR->getdata();
cout<<"\nEnter information about BUS:\n";
BUS* newBUS=new BUS();
newBUS->getdata();
cout<<"\nEnter information about TRUCK:\n";
TRUCK* newTRUCK=new TRUCK();
newTRUCK->getdata();
cout<<"\n";
newCAR->putdata();
cout<<"\n";
newBUS->putdata();
cout<<"\n";
newTRUCK->putdata();
delete newCAR;
delete newBUS;
delete newTRUCK;
system("pause");
return 0;
}
Comments
Leave a comment