How to call a parent class function from derived class function?

后端 未结 7 1190
自闭症患者
自闭症患者 2020-11-22 06:16

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is deri

7条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 06:58

    Call the parent method with the parent scope resolution operator.

    Parent::method()

    class Primate {
    public:
        void whatAmI(){
            cout << "I am of Primate order";
        }
    };
    
    class Human : public Primate{
    public:
        void whatAmI(){
            cout << "I am of Human species";
        }
        void whatIsMyOrder(){
            Primate::whatAmI(); // <-- SCOPE RESOLUTION OPERATOR
        }
    };
    

提交回复
热议问题