Calling a Base Class Construtor in a function of Derived Class
I am trying to call a constructor of a Base Class in a function of Derived Class. Here is the code:
Classes:
#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"
#inclu开发者_C百科de "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;
}
Now in the copy constructor of the derivedclass, i am trying to call the copy constructor of the base class using:
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);
to implement this, please tell me the syntax.
The proper way to call constructor hierarchies is like this:
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) { }
A copy constructor looks like this (notice the const):
Class :: Class (const Class &)
精彩评论