下面是我的代码:
class Pulley{
private:
int noOfTeeth;
public:
Pulley();
Pulley(int teeth);
~Pulley();
void show();
};
Pulley::Pulley()
{
cout << "Pulley constructor called!";
noOfTeeth = 0;
}
Pulley::Pulley(int teeth)
{
cout << "Pulley constructor called!";
noOfTeeth = teeth;
}
void Pulley::show()
{
cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
Pulley::~Pulley()
{
}
class GearBox{
private:
char *transmission;
Pulley p;
public:
GearBox();
GearBox(char *trans, int pTeeth);
~GearBox();
void show();
};
GearBox::GearBox(): p()
{
cout << "Gearbox constructor called!";
transmission = NULL;
}
GearBox::GearBox(char *trans, int pTeeth): p(pTeeth)
{
cout << "Gearbox constructor called!";
if(trans != NULL)
{
transmission = new char[strlen(trans)+1];
strcpy(transmission, trans);
}
else
{
transmission = NULL;
}
}
void GearBox::show()
{
cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
GearBox::~GearBox()
{
}
class Vehicle{
private:
char *model;
char *color;
GearBox g;
public:
Vehicle();
Vehicle(char *mod, char *col, char *gr);
Vehicle(const Vehicle &vh);
~Vehicle();
void show();
};
Vehicle::Vehicle(): g()
{
cout << "Vehicle constructor called!";
model = NULL;
color = NULL;
}
Vehicle::Vehicle(char *mod, char *col, char *gr): g(gr)
{
cout << "Vehicle constructor called!";
if(mod != NULL)
{
model = new char[strlen(mod)+1];
strcpy(model, mod);
}
else
{
model = NULL;
}
if(col != NULL)
{
color = new char[strlen(col)+1];
strcpy(color, col);
}
else
{
color = NULL;
}
}
void Vehicle::show()
{
cout << "\n\nModel of Vehicle: " << model;
cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
system("PAUSE");
}
现在,当我运行此代码时,我遇到了很多错误,我不知道这些是什么以及如何解决它们。我理解的一个错误是Copy Constructor,所以任何人都可以解释我如何编写指向字符transmission, model and color
的指针的复制构造函数。还告诉我如何解决其他错误。非常感谢。
答案 0 :(得分:-4)
试试这样:
#include <string>
#include <iostream>
class Pulley{
private:
int noOfTeeth;
public:
Pulley(int teeth = 0);
void show();
};
Pulley::Pulley(int teeth)
: noOfTeeth(teeth)
{
std::cout << "Pulley constructor called!";
}
void Pulley::show()
{
std::cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
class GearBox{
private:
std::string transmission;
Pulley p;
public:
GearBox(std::string trans = std::string(), Pulley p = Pulley());
void show();
};
GearBox::GearBox(std::string trans, Pulley p)
: transmission(std::move(trans))
, p(std::move(p))
{
std::cout << "Gearbox constructor called!";
}
void GearBox::show()
{
std::cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
class Vehicle{
private:
std::string model;
std::string color;
GearBox g;
public:
Vehicle();
Vehicle(std::string mod = "", std::string col = "", GearBox gr = GearBox());
void show();
};
Vehicle::Vehicle(std::string mod,std::string col, GearBox gr)
: model(std::move(mod))
, color(std::move(col))
, g(std::move(gr))
{
std::cout << "Vehicle constructor called!";
}
void Vehicle::show()
{
std::cout << "\n\nModel of Vehicle: " << model;
std::cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
// system("PAUSE");
}