C++继承和包含

在面向对象中,有两个很重要的东西,继承和包含。

更多的时候,我们不会去分析两种方式究竟哪种更好,能够达到自己想要的目的即可。但是存在即道理,还是要仔细分析一下。

继承:

Class Animal{};
Class Cat : public Animal{};

包含:

class Animal 
{};

class Cat
{
private:
    Animal *animal;
};

继承的弊端:

#include <iostream>
using namespace std; 

class Animal 
{
public: 
    int makeSound() {
        cout << "Animal is making sound" << endl;
        return 1;
    }
private:
    int ntimes;
};

class Cat : public Animal
{};

int main() 
{
    Cat *cat = new Cat();
    cat->makeSound();
    return 0;
}
//输出:
//Animal is making sound

此时此刻,如果我们想改变makesound函数:

Sound *makeSound(int n) {
    cout << "Animal is making sound" << endl;
    return new Sound;
}

因此,我们的代码也要进行改变:

#include <iostream>
using namespace std; 

class Sound{};

class Animal 
{
public: 
    Sound *makeSound() {
        cout << "Animal is making sound" << endl;
        return new Sound;
    }
};

class Cat : public Animal
{};

int main() 
{
    Cat *cat = new Cat();
    cat->makeSound();
    return 0;
}

如果使用包含呢:

#include <iostream>
using namespace std; 

class Animal 
{
public: 
    int makeSound() {
        cout << "Animal is making sound" << endl;
        return 1;
    }
};

class Cat
{
private:
    Animal *animal;
public:
    int makeSound() {
        return animal->makeSound();
    }
};

int main() 
{
    Cat *cat = new Cat();
    cat->makeSound(3);
    return 0;
}

同样是对makeSound进行修改:

#include <iostream>
using namespace std; 

class Sound{};

class Animal 
{
public: 
    Sound* makeSound() {
        cout << "Animal is making sound" << endl;
        return new Sound();
    }
};

class Cat 
{
private:
    Animal *animal;
public:
    Sound* makeSound() {
        return animal->makeSound();
    }
};

int main() 
{
    Cat *cat = new Cat();
    cat->makeSound();
    return 0;
}

This example shows that the ripple effect caused by changing a back-end class stops (or at least can stop) at the front-end class. Although Animal’s makeSound() method had to be updated to accommodate the change to Animal, our main()required no changes.

Object composition helps us keep each class encapsulated and focused on one task. Our classes and class hierarchies will remain small and will be less likely to grow and become unmanageable.

However, a design based on object composition will have more objects (if fewer classes), and the system’s behavior will depend on their (remote) interrelationships instead of being defined in one class.

Favor object composition over class inheritance.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一苇渡江694

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

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

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

打赏作者

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

抵扣说明:

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

余额充值