虚基类

    xiaoxiao2023-11-08  147

    #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; }

    李响Superb 认证博客专家 机器学习 TensorFlow 图像处理 成为一名优秀的算法工程师⬆️ ,目前还在读软件工程,AI攻防、算法和深度学习方向,微博同名❤️ :李响Superb,(记得关注,有问题微博私信!)我们一起努力呀!
    最新回复(0)