专题三经典问题解析

当多态遇见对象数组会发生什么?

#include <cstdlib>
#include <iostream>

using namespace std;

class Parent
{
protected:
    int i;
public:
    virtual void f()
    {
        cout<<"Parent::f"<<endl;
    }
};

class Child : public Parent
{
protected:
    int j;
public:
    Child(int i, int j)
    {
        this->i = i;
        this->j = j;
    }
    
    void f()
    {
        cout<<"i = "<<i<<" "<<"j = "<<j<<endl;
    }
};

int main(int argc, char *argv[])
{
    Parent* p = NULL;
    Child* c = NULL;
    Child ca[3] = {Child(1, 2), Child(3, 4), Child(5, 6)};
    
    
    p = ca;
    c = ca;

    cout<<hex<<p+1<<endl;
    
    p->f();
    c->f();
    
    p++;
    c++;
    
    p->f(); //此处导致崩溃!
    c->f();
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3qGp1wxg-1584618608756)(C:\Users\WGJ\AppData\Roaming\Typora\typora-user-images\1584583324133.png)]

不要将多态应用于数组

​ 指针运算是通过指针的类型进行的

​ 多态通过虚函数表实现的

多态与指针运算玩混搭的后果
在这里插入图片描述
在这里插入图片描述

结论:

不要在数组上使用多态!!

为什么没有讲解多重继承?

C++在语法上直接支持多重继承

#include <cstdlib>
#include <iostream>

using namespace std;

class P1{
protected:
    int i;
};

class P2{
protected:
    int j;
};

class Child : public P1, public P2{ //多继承
public:
    Child(int i, int j){
        this->i = i;
        this->j = j;
    }

    void print(){
        cout << "i = " << i<< " "<< "j = " << j<<endl;
     }
};



int main(int argc, char *argv[])
{
    Child c(1,2);
    c.print();

    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Unj8GTfE-1584618608775)(C:\Users\WGJ\AppData\Roaming\Typora\typora-user-images\1584584578229.png)]

被实际开发经验抛弃的多继承

​ 工程开发中真正意义上的多继承是几乎不被使用的

​ 多重继承带来的代码复杂性远多于其带来的便利

​ 多重继承对代码维护性上的影响是灾难性的

​ 在设计方法上,任何多继承都可以用单继承代替

多继承复杂性示例

#include <cstdlib>
#include <iostream>

using namespace std;

class Object
{
protected:
    int d;
public:
    void f()
    {
        cout<<"Object::f"<<endl;
    }
};

class P1 : public Object
{
protected:
    int i;
};

class P2 : public Object
{
protected:
    int j;
};

class Child : public P1, public P2
{
public:
    Child(int i, int j)
    {
        this->d = 0; //error,存在二义性
        this->i = i;
        this->j = j;
    }
    
    void print()
    {
        cout<<"i = "<<i<<" "<<"j = "<<j<<endl;
    }
};

int main(int argc, char *argv[])
{
    Child c(1, 2);
    
    c.print();
    c.f(); //error,存在二义性
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

在这里插入图片描述
在这里插入图片描述

为什么多继承复杂?

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DinVEInE-1584618608778)(C:\Users\WGJ\AppData\Roaming\Typora\typora-user-images\1584585864317.png)]

在只有单继承的系统中,类之间的继承关系为一颗树 。

在引入多重继承的系统中,类之间的继承关系呈现为一张图。【数据结构中的图示非常复杂的】

C++中对多继承二义性的解决方案

虚继承:

为了解决从不同途径继承来的同名数据成员造成的二义性问题,可以将共同基类设置为虚基类。 这时从不同的路径继承过来的同名数据成员在内存中就只有一个拷贝

#include <cstdlib>
#include <iostream>

using namespace std;

class Object{ //虚基类
protected:
    int d;
public:
    void f(){
        cout <<  "Object::f()" <<endl;
    }
};

class P1 : virtual public Object{ //虚继承
protected:
    int i;
};

class P2 : virtual public Object{ //虚继承
protected:
    int j;
};

class Child : public P1, public P2{ //多继承
public:
    Child(int i, int j){
        this->i = i;
        this->j = j;

        this->d = 0; //只存在一个拷贝
    }

    void print(){
        cout << "i = " << i<< " "<< "j = " << j<<endl;
     }
};



int main(int argc, char *argv[])
{
    Child c(1,2);
    c.print();
    c.f();

    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ABiAmUvo-1584618608780)(C:\Users\WGJ\AppData\Roaming\Typora\typora-user-images\1584586183431.png)]

C++是否有Java中接口的概念?

绝大多数面向对象语言都不支持多继承

绝大多数面向对象语言都支持接口的概念

C++中没有接口的概念

C++中可以使用纯虚函数实现接口

实际工程经验证明

多重继承接口不会带来二义性和复杂性等问题

多重继承可以通过精心设计用单继承和接口来代替

tips:

接口类只是一个功能说明,而不是功能实现。

子类需要根据功能说明定义功能实现。

接口使用示例

#include <cstdlib>
#include <iostream>

using namespace std;

class Interface1 //接口1
{
public:
    virtual void print() = 0;
    virtual int add(int i, int j) = 0;
};

struct Interface2 //接口2
{
    virtual int add(int i, int j) = 0;
    virtual int minus(int i, int j) = 0;
};

class Child : public Interface1, public Interface2 //多继承 接口
{
public:
    void print()
    {
        cout<<"Child::print"<<endl;
    }

    int add(int i, int j) //实现
    {
        return i + j;
    }

    int minus(int i, int j)
    {
        return i - j;
    }
};

int main(int argc, char *argv[])
{
    Child c;

    c.print();

    cout<<c.add(3, 5)<<endl;
    cout<<c.minus(4, 6)<<endl;

    cout << endl;

    Interface1* i1 = &c; //c++赋值兼容性原则
    Interface2* i2 = &c;

    cout<<i1->add(7, 8)<<endl;
    cout<<i2->add(7, 8)<<endl; //两种效果相同

    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0M2dxNI4-1584618608781)(C:\Users\WGJ\AppData\Roaming\Typora\typora-user-images\1584609345317.png)]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值