C++多继承与多态

没有人不辛苦,只是有人不喊疼。。。

----  网易云热评

 

一、多继承,一个子类可以同时基类多个基类,这样的继承方式被称为多重继承。

#include <iostream>using namespace std;class Phone{//电话基类public:    Phone(const string& num):m_num(num){}    void call(const string& num){        cout << m_num << "打给" << num <<endl;    }private:    string m_num;};class Player{//播放器基类public:    Player(const string& media)        :m_media(media){}    void play(const string& music){        cout << m_media << "播放器播放" <<            music << endl;    }private:    string m_media;};class Computer{//计算机基类public:    Computer(const string& os):m_os(os){}    void run(const string& app){        cout << "在" << m_os << "系统上运行"            << app << endl;    }private:    string m_os;};class SmartPhone:public Phone,public Player,    public Computer{//智能手机子类public:    SmartPhone(const string& num,        const string& media,const string& os)            :Phone(num),Player(media),                Computer(os){}};int main(void){    SmartPhone iphone(            "13688886666","MP3","Android");    iphone.call("010-12315");    iphone.play("最炫小苹果");    iphone.run("Angry Bird");       SmartPhone* p1 = &iphone;    Phone* p2 = p1;    Player* p3 = p1;//+24    Computer* p4 = p1;//+48    cout << "p1=" << p1 << endl;    cout << "p2=" << p2 << endl;    cout << "p3=" << p3 << endl;    cout << "p4=" << p4 << endl;    cout << "sizeof(string)=" <<        sizeof(string) << endl;//24    return 0;}运行结果:13688886666打给010-12315MP3播放器播放最炫小苹果在Android系统上运行Angry Birdp1=009BFB68p2=009BFB68p3=009BFB84p4=009BFBA0sizeof(string)=28

 

二、多态

多态的语法特性除了要满足虚函数覆盖的条件,还必须通过指针或者通过引用调用虚函数,才能表现出来.

调用虚函数的指针也可以是成员函数中的this指针,当通过子类对象调用基类中成员函数,这时该成员函数中的this指针就是指向子类对象的基类指针,再通过它去调用虚函数同样可以表现多态的语法特性//重点掌握

#include <iostream>

using namespace std;

class Base{

public:

    virtual int cal(int x,int y){

        return x + y;

    }

    //void func(Base* this=&d)

    void func(void){

        //cout << this->cal(100,200) << endl;

        cout << cal(100,200) << endl;//20000

    }

};

class Derived:public Base{

public:

    int cal(int x,int y){

        return x * y;

    }

};

int main(void)

{

    Derived d;

    Base b = d;

    cout << b.cal(100,200) << endl;

    

    d.func();//func(&d)

    

    return 0;

}

运行结果:

300

20000

#include <iostream>using namespace std;class PDFParser{public:    void parse(const char* pdffile){        cout << "解析出一个矩形" << endl;        onRect();        cout << "解析出一个圆形" << endl;        onCircle();        cout << "解析出一行文本" << endl;        onText();        }private:    virtual void onRect(void) = 0;    virtual void onCircle(void) = 0;    virtual void onText(void) = 0;};class PDFRender:public PDFParser{private:    void onRect(void){        cout << "绘制一个矩形" << endl;    }    void onCircle(void){        cout << "绘制一个圆形" << endl;    }    void onText(void){        cout << "显示一行文本" << endl;    }};int main(void){    PDFRender render;    render.parse("xx.pdf");    return 0;}运行结果:解析出一个矩形绘制一个矩形解析出一个圆形绘制一个圆形解析出一行文本显示一行文本

 

欢迎关注公众号:顺便编点程

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web安全工具库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值