Answer to Question #294840 in C++ for Muhammad Ahmed

Question #294840

Create a base class called Vehicle. Use this class to store two double type values that could be used to compute

the speed of the vehicle. Derive two specific classes called train and truck from the base Vehicle. Add to base

class, a member function get_speed() to initialize base class data members and another member functions

get_name() to compute and display the area of figures. Mark the display_area() as a abstract function and redefine

this function in the derived class to suit their requirements.


1
Expert's answer
2022-02-07T12:50:21-0500
#include <iostream>
#include <string>


using namespace std;


class Vehicle{
private:
	float S;
	float T;
public:
	//Constructor
	Vehicle(){}
	//Constructor
	Vehicle(float S,float T){
		this->S=S;
		this->T=T;
	}
	float get_speed(){
		return this->S/this->T;
	}
	virtual string get_name(){
		return "";
	}
};


class Train :public Vehicle{
public:
	//Constructor
	Train(){}
	//Constructor
	Train(float S,float T):Vehicle(S,T){
	}
	string get_name(){
		return "Train";
	}
};




//define class Truck inherits Vehicle
class Truck:public Vehicle{
public:
	//Constructor
	Truck(){}
	//Constructor
	Truck(float S,float T):Vehicle(S,T){
	}
	string get_name(){
		return "Truck";
	}
};




int main(){
	
	Train train(80,1.5);
	Truck truck(95,3);
	cout<<"The speed of "<<train.get_name()<<" is: "<<train.get_speed()<<"\n";
	cout<<"The speed of "<<truck.get_name()<<" is: "<<truck.get_speed()<<"\n";


	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