Function 在派生类的函数中调用基类构造函数

Function 在派生类的函数中调用基类构造函数,function,constructor,derived-class,base-class,Function,Constructor,Derived Class,Base Class,我试图在派生类的函数中调用基类的构造函数。代码如下: 课程: #pragma once #include "CAR_TYRE_DOOR.h" #include <string> using namespace std; //#ifndef 1_A_H_B //#define 1_A_H_B class Honda_Civic: public Car { private: string CNG_y_n; public: Honda_Civic(); Hond

我试图在派生类的函数中调用基类的构造函数。代码如下:

课程:

#pragma once

#include "CAR_TYRE_DOOR.h"
#include <string>
using namespace std;
//#ifndef 1_A_H_B
//#define 1_A_H_B

class Honda_Civic: public Car
{
private:
    string CNG_y_n;
public:
    Honda_Civic();
    Honda_Civic(string CNG);
    Honda_Civic(Honda_Civic& H1);

    void set_CNG_y_n(string S);
    string get_CNG_y_n();
    void print();
};
class BMW: public Car
{
private:
    string conv_y_n;
public:
    BMW();
    BMW(string S);
    BMW(BMW& BMW1);
    void set_conv_y_n(string S);
    string get_conv_y_n();
    void print();
};
class Mercedes: public Car
{
private:
    int no_WS;
    string SGR_y_n;
public:
    Mercedes();
    Mercedes(int no_WS, string SGR_y_n);
    Mercedes(Mercedes& Merc);

    //::Car( Merc1);

    void set_no_WS(int n);
    void set_SGR(string SGR);
    int get_no_WS();
    string get_SGR();
    void print();
};

//#endif

The BaseClass functions:
//#include "BMW+MERC.h"
#include "CAR_TYRE_DOOR.h"
#include "Honda.h"
#include "S_R.h"
#include <iostream>
#include <string>

using namespace std;

void Car::set_color(string S)
{
    S = this->color;
}

void Car::set_model(string S)
{
    S = this->model;
}

void Car::set_cost(float x)
{
    x = this->cost;
}

string Car::get_color()
{
    return this->color;
}

string Car::get_model()
{
    return this->model;
}

float Car::get_cost()
{
    return this->cost;
}
Car::Car()
{
}
Car::Car(string color, string model, float cost)
{
    this->color = "white";
    this->model = "2011";
    this->cost = 1000000;
}
Car::Car(Car& C1)
{
    this->color = C1.color;
    this->model = C1.model;
    this->cost = C1.cost;

    for(int i=0; i<4; i++)
    {
        DX[i] = C1.DX[i];
    }

    for(int i=0; i<4; i++)
    {
        TX[i] = C1.TX[i];
    }
}
void Car::print_car()
{
    cout <<"Car color: "<<get_color()<<endl;
    cout <<"Car model: "<<get_model()<<endl;
    cout <<"Car door color: "<<DX[0].get_color()<<endl;
    cout <<"Car door vendor: "<<DX[0].get_vendor()<<endl;

    cout <<"Tyre vendor: "<<TX[0].get_vendor()<<endl;
    for(int i=0; i<4; i++)
    {
        cout <<"Tyre"<< i+1 <<"type: "<<TX[i].get_rubber_type()<<endl;
    }


}
The Derived Class:
#include "Honda.h"
#include <iostream>
#include <string>
using namespace std;

Mercedes::Mercedes()
{
}
Mercedes::Mercedes(int no_WS, string SGR_y_n)
{
    this->no_WS = 4;
    this->SGR_y_n = "Yes";
}
Mercedes::Mercedes(Mercedes& Merc)
{
    Mercedes::Car( Merc);

    this->no_WS = Merc.no_WS;
    this->SGR_y_n = Merc.SGR_y_n;
}
void Mercedes::set_no_WS(int n)
{
    this->no_WS = n;
}
void Mercedes::set_SGR(string SGR)
{
    this->SGR_y_n = SGR;
}
int Mercedes::get_no_WS()
{
    return this->no_WS;
}
string Mercedes::get_SGR()
{
    return this->SGR_y_n;
}
void Mercedes::print()
{
    Mercedes.print_car();

    cout <<"Number of Woofer Speakers: "<<get_no_WS()<<endl;
    cout <<"Sunglass Roof: "<<get_SGR()<<endl;
}

要实现此功能,请告诉我语法。

调用构造函数层次结构的正确方法如下:

class Car
{
    public:
        Car () { }
        Car (cosnt Car &) {}
};

class Mercedes : public Car
{
    public:
        Mercedes () { }
        Mercedes (const Mercedes &) {}
};

Mercedes :: Mercedes () : Car() { }
Mercedes :: Mercedes (const Mercedes &car) : Car(car) { }
复制构造函数如下所示(注意const):

类::类(常量类&)

class Car
{
    public:
        Car () { }
        Car (cosnt Car &) {}
};

class Mercedes : public Car
{
    public:
        Mercedes () { }
        Mercedes (const Mercedes &) {}
};

Mercedes :: Mercedes () : Car() { }
Mercedes :: Mercedes (const Mercedes &car) : Car(car) { }