这是我的c ++介绍webclass 11中的第10节(1/1);
我可能还没有完成这项练习,但我真的不知道如何继续这一点。
这项练习是从另一种语言翻译出来的,如果我错过或输错了一些变数,我会提前道歉,如果指出的话会尝试纠正它们。
我很难理解当一些函数来自一个被继承的类时,如何构造一个新对象,不确定哪些条件甚至可以搜索,(在第二部分中)Car::Car(string make, string model, string rego, bool onoff):
Vechile(weight, top_speed, driven_km)
应该从主要的Car carX(weight, speed, km, make, model, rego, 0);
中获取它的值
如何正确构造(?)carX对象? weight, top_speed, driven_km
的值继承自Vechile类,我假设我需要按照提供的main()以正确的顺序(例如weight, speed, km, make, model, rego, onoff
)呈现修饰符练习计划),
但我无法通过Web界面编译器中的任何其他组合使代码超过Car::Car(string make, string model, string rego, bool onoff): Vechile(weight, top_speed, driven_km)
。
我是否正确,还有其他我想念的东西吗?
代码中的函数仍然大部分正在进行中,可能会或可能不会像现在一样工作,但如果你不介意,我宁愿只帮助解决与OOP相关的问题。问题。
感谢您的时间。
第1部分:不可编辑的顶部(vechile类);
#include <iostream>
#include <string>
using namespace std;
class Vechile
{
public:
int weight;
int top_speed;
long driven_km;
Vechile(int weight, int top_speed, long driven_km);
void drive(int km_togo);
int GiveWeight();
int GiveTopspeed();
long GiveDrivenKm();
};
Vechile::Vechile(int Ap, int Ahn, long Akm)
{
weight = Ap;
top_speed = Ahn;
driven_km = Akm;
}
void Vechile::drive(int km_togo)
{
driven_km += km_togo;
}
int Vechile::GiveWeight()
{
return weight;
}
int Vechile::GiveTopspeed()
{
return top_speed;
}
long Vechile::GiveDrivenKm()
{
return driven_km;
}
第2节:到目前为止我所做的:(汽车类,检查汽车功能)
class Car : public Vechile
{
public:
string rego, model, make;
bool onoff;
Car(string make,string model,string rego, bool onoff);
char check();
char start();
};
Car::Car(string make, string model, string rego, bool onoff):
Vechile(weight, top_speed, driven_km)
{
Car::make = make;
Car::model = model;
Car::driven_km = driven_km;
Car::rego = rego;
Car::top_speed = top_speed;
Car::weight = weight;
Car::onoff = onoff;
}
char Car::check()
{
cout << "Car Info:" << endl;
cout << "Make:"<< Car::make << endl;
cout << "Model:"<< Car::model<< endl;
cout << "Driven KM:"<< Car::driven_km << endl;
cout << "Weight" << Car::weight << endl;
cout << "Top_speed:"<< Car::top_speed << endl;
cout << "rego:"<< Car::rego << endl;
if(Car::onoff = 0)
{
cout << "Car is not started"<<endl;
}
else
{
cout << "Car is started"<<endl;
}
}
char Car::start()
{
onoff = 1;
}
第3节:不可编辑的主要内容;
int main()
{
int weight, speed;
long km;
string make, model, rego;
cout << "Give make of Car: ";
cin >> make;
cout << "Give model of car: ";
cin >> model;
cout << "Give registration of car: ";
cin >> rego;
cout << "Give weight of car: ";
cin >> weight;
cout << " Give top speed of car";
cin >> speed;
cout << "Enter driven km";
cin >> km;
cout << endl;
Car carX(weight, speed, km, make, model, rego, 0);
carX.check();
carX.start();
carX.drive(95);
cout << endl;
carX.check();
}
答案 0 :(得分:1)
Car::Car(string make, string model, string rego, bool onoff):
Vechile(weight, top_speed, driven_km)
:
表示您正在调用Vechile
的构造函数。但是,您尚未提供weight
,top_speed
和driven_km
的内容。
您必须在Car
构造函数中包含这些参数。
Car::Car(int weight, int top_speed, long driven_km, string make, string model, string rego, bool onoff):
Vechile(weight, top_speed, driven_km)