简体   繁体   中英

Understanding OOP class inheritance/relations c++

This is Section 10(1/1) out of 11 of my c++ introduction webclass;

I'm probably quite far from finishing this exercise, but I don't really know how to continue from this point on my own.

The exercise has been translated from another language, I apologize in advance if I missed or mistyped some variables and will try to correct them if pointed out.

I'm having hard time understanding how do I construct a new object when some functions are from a class that is being inherited, unsure what terms to even search for, (in 2nd section) Car::Car(string make, string model, string rego, bool onoff): Vechile(weight, top_speed, driven_km) should get it's values from Car carX(weight, speed, km, make, model, rego, 0); in the main,

how do I construct(?) the carX object properly? the values for weight, top_speed, driven_km are inherited from the Vechile class, and I'm assuming here that I need to present the modifiers in correct sequence ((eg. weight, speed, km, make, model, rego, onoff ) as per the provided main() program for the exercise),

but I cannot get the code to work past Car::Car(string make, string model, string rego, bool onoff): Vechile(weight, top_speed, driven_km) with any other combination in the web-interface compiler.

Am I correct, is there something else I'm missing?

The functions in the code are still mostly work in progress and may or may not work as they currently are, but If you do not mind, I'd rather only have help with OOP related issues in this question.

Thank you for your time.

Section 1: Uneditable top(vechile class);

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

Section 2: What I have made so far: (Car class, check car function)

    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;

    }

Section 3 : Uneditable Main;

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();
}
Car::Car(string make, string model, string rego, bool onoff):
    Vechile(weight, top_speed, driven_km)

The : means you're calling Vechile 's constructor. However, you haven't provided what weight , top_speed , and driven_km are.

You have to include those parameters in the Car constructor.

Car::Car(int weight, int top_speed, long driven_km, string make, string model, string rego, bool onoff):
    Vechile(weight, top_speed, driven_km)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM