-1

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

课程:

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

现在在派生类的复制构造函数中,我尝试使用以下方法调用基类的复制构造函数:

Mercedes::Mercedes(Mercedes& Merc)
{
    Mercedes::Car( Merc);

    this->no_WS = Merc.no_WS;
    this->SGR_y_n = Merc.SGR_y_n;
}
See this: Mercedes::Car( Merc);

要实现这一点,请告诉我语法。

4

1 回答 1

1

调用构造函数层次结构的正确方法是这样的:

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):

类::类(常量类&)

于 2011-12-27T01:29:25.417 回答