当前位置 : 主页 > 手机开发 > harmonyos >

第13周-程序阅读(3)

来源:互联网 收集:自由互联 发布时间:2023-08-28
代码: #include iostreamusing namespace std;class Base{public: virtual void Who() =0;};class FirstDerived:public Base{public: void Who() { cout"F"; }};class SecondDerived:public Base{public: void Who() { cout"S"; }};int main(){ FirstDeri


代码:

#include <iostream>
using namespace std;
class Base
{
public:
    virtual void Who() =0;
};
class FirstDerived:public Base
{
public:
    void Who() { cout<<"F"; }
};
class SecondDerived:public Base
{
public:
    void Who() { cout<<"S"; }
};
int main()
{
    FirstDerived first_obj;
    SecondDerived second_obj;
    Base &bref=first_obj;
    bref.Who();
    bref=second_obj;
    bref.Who();
    Base *bp;
    bp=&first_obj;
    bp->Who();
    bp=&second_obj;
    bp->Who();
    return 0;
}

 

运行结果:

FFFS
Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

 

总结:

上一篇:第13周-程序阅读(1)
下一篇:没有了
网友评论