C++ Inheritance Question. Why are Operators and constructors not inherited but included in child classes?

Asked 2 years ago, Updated 2 years ago, 20 views

While studying, in the lecture notes Overloaded assignment operator not inherited – But can be invoked from derived class Constructors are not inherited– Are invoked from derived class’s constructor That's what it says.

If I were to translate it Overloaded operators are not inherited but can be included from child classes (?) What does this mean?) It doesn't include a constructor, but it's included in the child class constructor. What is the reason? If it's going to be inherited, it's all inherited. Is there a reason why it's confusing?

c++

2022-09-22 08:26

1 Answers

You interpreted it a little differently.

If you try the translation again,

Overloaded assignment operator not inherited – But can be invoked from derived class

(Overloaded substitution operators are not inherited. - However, it can be called from the derived class.)

Constructors are not inherited – Are invoked from derived class’s constructor

(Generators are not inherited. - Called from the constructor of the derived class.)

To get this straight... I'll have to explain it in code.

class A
{
public:
    A(); // Default constructor
    ~A(); // Extinguisher

    A(const A&other); // Copy constructor
    A&operator=(const A&other); // Copy substitution operator

    A(A&&other); // Move constructor
    A&operator=(A&&other); // Move substitution operator
};

All the functions written in the code above are those that the compiler automatically creates. So only when we want to implement additional functions, we need to declare and define them ourselves.

Usually, we use a lot of basic constructors and decipients, but we don't need to know the copy constructors, the copy substitution operators, etc. at the basic level.

First, the substitution operator is almost identical to the constructor.

But the important thing is why the constructors can't write inheritance.

If you think about how an inherited function is used, the answer comes out. (Just in case, constructors are also functions.)

class A
{
    virtual void Func();
};

class B : public A
{
    virtual void Func() override;
};

int main()
{
    A* ptr1 = new A();
    A* ptr2 = new B();

    ptr1->Func(); // call function in class A
    ptr2->Func(); // call function in class B
}

Usually, if you inherit a function, it will be used as above, right? In other words, the virtual function is to find the class type that is actually created and to call only the corresponding function.

If you also make the constructor a virtual function, when you create a child class, only the constructor of the child is called, and the constructor of the parent is not called.

By the way, the role of a constructor is usually to initialize a member variable Writing code that initializes all the member variables of the child class and parent class every time is not only a headache, but also goes against the principle of object orientation.

In other words, it makes sense for each class to initialize its own member variable in its constructor.

So, instead of using a virtual function, the constructor invokes the constructor of the parent class separately from the constructor of the child class.

But if we're going to write the code that we call each time, we can forget it by mistake, and it's annoying, so the compiler generates the code that we call on our own.

class A
{
public:
    A() // constructor
    {
        a = 0; // Initialize member variables
    }
private:
    int a;
};

class B : public A
{
public:
    B() // constructor
    {
        // A(); // You can think of this code as implicit.

        // It's not actually created like this inside the constructor
        // It will be added to the generator initialization list.
        // B() : A() {b = 0;} in this form.

        b = 0;
    }
private:
    int b;
};

In other words, when you create class B, the constructor of class A is called first, and then the constructor of class B is called.

This eliminates the need to consider the initialization of member variables in the parent class.

I think you can understand the substitution operator in the same context.

Just in case you don't know what a substitution operator is

class Object {};

int main()
{
    Object a; // Default constructor is called.
    The object b = a; // copy constructor is called (copy the value of a to create an instance of b)
    Object c;
    c = a; // The copy substitution operator is called. (because c is an instance that has already been created)
    return 0;
}


2022-09-22 08:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.