jeudi 16 septembre 2021

Using base class constructor in derived class contructor

Let's say that I want to make use of base class constructor in order to create derived class objects. That's my approach on how to do it:

class base
{
    int x, y, z;
public:
    base(int x, int y, int z)
        :
        x(x),
        y(y),
        z(z)
    {
        std::cout << "base class constructor called\n";
    }
    int get_x() const { return x; }
    int get_y() const { return y; }
    int get_z() const { return z; }
};
class derived : public base
{
    int value;
public:
    derived(int x, int y, int z, int value)
        :
        base(x, y, z),
        value(value)
    {
        std::cout << "derived class constructor called\n";
    };
    int get_val() const { return value; }  ;

};

My question is it correct way of solving that problem? Or if there is some better approach on how to use base class constructor in derived class constructor?

Aucun commentaire:

Enregistrer un commentaire