Tuesday, February 12, 2008

private inheritance?

Usually composition are used instead of private inheritance, suck like

class Y
{
private:
X x;
public:
...
}
private inheritance is useful if you want to hide part of the functionality of the base class:

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 { // Private inheritance
public:
Pet::eat; // Name publicizes member
Pet::sleep; // Both overloaded members exposed
};
int main() {
Goldfish bob;
bob.eat();
bob.sleep();
bob.sleep(1);
//! bob.speak();// Error: private member function
}
--Thinkinginc++

No comments:

Post a Comment