#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Animal {
public:
Animal(int age) :m_nage(age)
{
cout << "animal constructor" << endl;
}
protected:
int m_nage;
};
class Bird :virtual public Animal{
public:
Bird(int age, int fh):Animal(age)
{
cout << "bird constructor" << endl;
m_nflightaltitude = fh;
}
int get_flightaltitude()
{
return m_nflightaltitude;
}
private:
int m_nflightaltitude;
};
class Fish :virtual public Animal {
private:
int m_nswimspeed;
public:
Fish(int age, int speed) :Animal(age)
{
cout << "fishconstructor" << endl;
m_nswimspeed = speed;
}
int get_swimspeed()
{
return m_nswimspeed;
}
};
class Waterbird :public Bird, public Fish {
public:
Waterbird(int b_age, int f_age, int fh, int speed) :Bird(b_age,fh), Fish(f_age, speed), Animal(b_age)
{
cout << "waterbird constructor" << endl;
}
void print_animalage()
{
cout << "age=" << m_nage << endl;
}
};
int main()
{
Waterbird waterbird(5, 6, 20, 30);
cout << waterbird.get_flightaltitude() << ' ' << waterbird.get_swimspeed() << endl;
waterbird.print_animalage();
system("pause");
return 0;
}
虚基类
原创
©著作权归作者所有:来自51CTO博客作者李响Superb的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章