我希望我的Dog类正确地继承Pet中的函数,以便main中的代码能够正确执行。编译器警告从基类Pet。
调用私有方法主:
Dog * d = new Dog( "Muffin" );
cout << "The " << d->getVariety( ) << " " << d->getName( ) << " speaks by saying " << d->speak( ) << endl;
类:
class Pet
{
public:
Pet( );
Pet( std::string name, std::string variety, std::string says )
{
myName=name;
myVariety=variety;
howIspeak=says;
}
std::string getName( ) const
{
return myName;
};
public:
std::string getVariety( ) const
{
return myVariety;
};
std::string speak() const
{
return howIspeak;
};
protected:
std::string myName;
std::string myVariety;
std::string howIspeak;
};
class Dog: Pet
{
public:
Dog( );
Dog( std::string name );
virtual void speak( );
};
答案 0 :(得分:1)
C ++默认为私有继承。因此使用
class Dog: public Pet
答案 1 :(得分:1)
类具有私有继承
class Dog: public Pet