Search code examples
c++copy-constructorcomposition

How do I write the copy Constructor for Composition?


below is my code:

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

Now, when I run this code I get a lots of error, I don't know what are those and how to resolve them. The one error I understood is of Copy Constructor, so can anyone explain me that how can I write the copy constructor for pointer to characters transmission, model and color. Also tell me how do I resolve other errors. Thanks a lot.


Solution

  • 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");
    }