C++题目 急急急

2024年11月22日 09:09
有2个网友回答
网友(1):

#include
using namespace std;
class vehicle
{
public:
virtual void travel_time()
{
cout<<"汽车运行的时间为:"<<(distance / 80)< }
double distance;
};
class truck:public vehicle
{
public:
void travel_time()
{
cout<<"汽车运行的时间为:"<<(distance / 120)< }
};

class car:public vehicle
{
public:
void travel_time()
{
cout<<"汽车运行的时间为:"<<(distance / 120)< }
};
void main()
{
truck c_truck;
car c_car;

cout<<"为每个对象输入二个测试数据"<
cin>>c_truck.distance;
cout<<"通过这段距离的时间为:";
c_truck.travel_time();

cin>>c_truck.distance;
cout<<"通过这段距离的时间为:";
c_truck.travel_time();

cin>>c_car.distance;
cout<<"通过这段距离的时间为:";
c_car.travel_time();

cin>>c_car.distance;
cout<<"通过这段距离的时间为:";
c_car.travel_time();
system("pause");

}

网友(2):

#includeusing namespace std;
class vehicle{
public:
vehicle(float d){}
virtual ~vehicle(){}
virtual float travel_time()=0;
protected:
    float distance;
};
class truck:public vehicle {
public:
truck(float d):vehicle(d){}
virtual ~truck(){}
virtual float travel_time(){
    return distance/100;
}
};
class car:public vehicle {
public:
car(float d):vehicle(d){}
virtual ~car(){}
virtual float travel_time(){
    return distance/120;
}
};
int main(){
    int i=0;
    float d;
    while(i++<2){
        cout<<"Please input truck distance:"<        cin>>d;
        vehicle* p1=new truck(d);
        cout<<"Please input car distance:"<        cin>>d;
        vehicle* p2=new car(d);
        
        cout<<"The truck'time is:"<travel_time<        cout<<"The car'time is:"<travel_time<        
        delete p1;
        delete p2;
    }
    return 0;
}