adding a multiple level inheritance example
This commit is contained in:
@@ -180,12 +180,14 @@ Create a class hierarchy of geometric objects, such as: triangle, isosceles tria
|
|||||||
rhombus, kite, trapezoid, circle, ellipse, etc. How should this hierarchy be arranged? What member variables and
|
rhombus, kite, trapezoid, circle, ellipse, etc. How should this hierarchy be arranged? What member variables and
|
||||||
member functions should be in each class?
|
member functions should be in each class?
|
||||||
|
|
||||||
## 26.10 Note: Multiple Inheritance
|
## 26.10 Multiple Inheritance
|
||||||
|
|
||||||
- When sketching a class hierarchy for geometric objects, you may have wanted to specify relationships that were more complex... in particular some objects may wish to inherit from more than one base class.
|
- When sketching a class hierarchy for geometric objects, you may have wanted to specify relationships that were more complex... in particular some objects may wish to inherit from more than one base class.
|
||||||
- This is called multiple inheritance and can make many implementation details significantly more hairy. Different programming languages offer different variations of multiple inheritance.
|
- This is called multiple inheritance and can make many implementation details significantly more hairy. Different programming languages offer different variations of multiple inheritance.
|
||||||
- See [example 1](multiple_inheritance1.cpp) and [example 2](multiple_inheritance2.cpp).
|
- See [example 1](multiple_inheritance1.cpp) and [example 2](multiple_inheritance2.cpp).
|
||||||
|
|
||||||
|
- And see [example 3](multiple_level_inheritance.cpp) for a multiple level inheritance example.
|
||||||
|
|
||||||
## 26.11 Introduction to Polymorphism
|
## 26.11 Introduction to Polymorphism
|
||||||
|
|
||||||
- Let’s consider a small class hierarchy version of polygonal objects:
|
- Let’s consider a small class hierarchy version of polygonal objects:
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// multiple-level inheritance
|
||||||
|
|
||||||
class A
|
class A
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -20,9 +24,15 @@ public:
|
|||||||
class D:public C
|
class D:public C
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
D(int a, int b, int c, int d):C(a,b,c),d(d){}
|
||||||
|
void print(){
|
||||||
|
std::cout << a << ":" << b << ":" << c << ":" << d << std::endl;
|
||||||
|
}
|
||||||
int d;
|
int d;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
D dObject(1,2,3,4);
|
||||||
|
dObject.print();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user