-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_inheritance.cpp
More file actions
56 lines (46 loc) · 1.1 KB
/
multiple_inheritance.cpp
File metadata and controls
56 lines (46 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
Animal (string nam = "Who am I ?"):name(nam)
{
}
void eat () {cout << name << " is Eating \n";}
void sleep () {cout << name << " is tired and gets to sleep \n";}
void talk () {}
protected:
string name;
};
class Bird : public Animal
{
public:
Bird ():Animal("bird") {}
// Over-rides the base implementation
void talk () {cout << "birds talking \n";}
void fly () {cout << name << " flying \n";}
};
class Horse : public Animal
{
public:
Horse ():Animal("horse") {}
void talk () {cout << " horse talk \n";}
void run () {cout << name << " Super fast run \n";}
};
// With virtual inheritance the instantiation of the members from the Base class has
// to happen from the most derived class. The instantiations from the derived classes
// Bird and Horse will be ignored, although it compiles sucessfully
class FlyingHorse: public Bird, public Horse
{
public:
FlyingHorse ():Animal ("Flying Horse"){}
};
int main ()
{
FlyingHorse fh;
fh.run ();
fh.fly ();
fh.sleep ();
cout << "Bye Bye " << endl;
}