To call a parent class constructor

Asked 2 years ago, Updated 2 years ago, 73 views

How does C++ call the constructor of a parent class (even a parent's parent) in a child class?

In Java, we could have called the parent class constructor on the first line What should I do with C++

c++ inheritance constructor

2022-09-21 16:57

1 Answers

Write as follows

class SuperClass
{
    public:
        SuperClass(int foo) {}
};

class SubClass : public SuperClass
{
    public:
        SubClass(int foo, int bar): SuperClass(foo) {}
};

It's on public right now, In c++, the private/protected/public keyword is the same as in Java.


2022-09-21 16:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.