【问题标题】:Understanding OOP class inheritance/relations c++了解 OOP 类继承/关系 c++
【发布时间】:2017-07-11 17:04:33
【问题描述】:

这是我的 c++ 介绍网络课程的 11 节中的第 10 节(1/1);

我可能距离完成这个练习还有很长的路要走,但我真的不知道如何独自从这一点继续。

该练习已从另一种语言翻译,如果我错过或输入错误的一些变量,我提前道歉,如果指出,我会尝试纠正它们。

当某些函数来自被继承的类时,我很难理解如何构造一个新对象,甚至不确定要搜索哪些术语,(在第 2 部分中)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)),

但我无法让代码通过Car::Car(string make, string model, string rego, bool onoff): Vechile(weight, top_speed, driven_km) 与 Web 界面编译器中的任何其他组合一起工作。

我说的对吗,还有什么我遗漏的吗?

代码中的函数大部分仍在进行中,可能会或可能不会像当前那样工作,但如果您不介意,我宁愿只在这个问题中就 OOP 相关问题提供帮助。

感谢您的宝贵时间。

第 1 部分: 不可编辑的顶部(车辆类);

    #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();
}

【问题讨论】:

  • 派生类的构造函数需要在它的参数中包含构造基类所需的一切。这些参数没有隐式包含,您必须自己使用这些参数定义构造函数。

标签: c++ oop inheritance


【解决方案1】:
Car::Car(string make, string model, string rego, bool onoff):
    Vechile(weight, top_speed, driven_km)

: 表示您正在调用Vechile 的构造函数。但是,您还没有提供 weighttop_speeddriven_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)

【讨论】:

  • 哦,哇,这真的很有道理,我现在觉得有点愚蠢。在沮丧地问之前,我把头撞了几个小时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多