2011-04-04 148 views
-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; 
} 

现在在derivedclass的拷贝构造函数,我试图通过调用基类的拷贝构造函数:

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

实施这个,请告诉我语法。

回答

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) { } 

复制构造看起来是这样的(注意常量):

类::类(常量类&)

相关问题