C++中私有继承公有化

当私有继承时,基类的所有public成员都变成了private。如果希望它们中的任何一个 是可视的,只要用派生类的public部分声明它们的名字即可:

#include<iostream>
using namespace std;

class Pet {
public:
char eat() const {return 'a';}
int speak() const {return 2;}
float sleep() const {return 3.0;}
float sleep(int) const {return 4.0;}
};

class Goldfish : Pet {
public:
using Pet::eat;
using Pet::sleep;
using Pet::speak;
};

int main(){
Goldfish bob;
cout << bob.eat() <<' ';
cout << bob.sleep()<<' ';
cout << bob.sleep(1) <<' ';
cout << bob.speak();
}

原文地址:https://www.cnblogs.com/shiheyuanfang/p/14332672.html