Try like this:
#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");
}
7
solved How do I write the copy Constructor for Composition? [closed]