Answer to Question #208947 in Programming & Computer Science for Waheed

Question #208947

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



1
Expert's answer
2021-06-20T18:14:08-0400
#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;
}

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