Why do we need inheritance, when we can achieve the same using aggregation in c++?

We know, in C++ we can have two kinds relation btn class
: is-a relation supported by inheritance
: has relation supported compaction(generalization)

we achieve same thing from both of these then why we have choice?
Aggregation doesn't provide polymorphism.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class A{
public:
    virtual void foo() = 0;
};

class B : public A{
public:
    void foo();
};

class C : public A{
public:
    void foo();
};

//...

std::vector<A *> v;
v.push_back(new B);
v.push_back(new C);
for (auto o : v)
    o->foo();
Topic archived. No new replies allowed.