How do I call a parent class function in a child class?

Asked 2 years ago, Updated 2 years ago, 117 views

Is there a way to call a member function of a parent class in a child class? How can I implement call_print_in_Base below?

class Base{
public:
    void print() { cout << "i'm base!" << endl; }
};

class Derived : public Base{
public:
    void print() {
        call_print_in_Base() ///<-here
        cout << "i'm Derived!" << endl;
    }
};

oop c++ inheritance

2022-09-21 21:51

1 Answers

C++ supports multiple inheritance, so to eliminate ambiguity There are no base class keywords such as super in java or base in C#. Instead, add :: (two colons) after the name of the base class.

class Base{
public:
    void print() { cout << "i'm base!" << endl; }
};

class Derived : public Base{
public:
    void print() {
        Base::print();
        cout << "i'm Derived!" << endl;
    }
};


2022-09-21 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.