-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance_types.cpp
More file actions
55 lines (45 loc) · 916 Bytes
/
inheritance_types.cpp
File metadata and controls
55 lines (45 loc) · 916 Bytes
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
#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 Sleeping \n";}
void speak () {}
void run () {cout << name << " is Running \n";}
protected:
string name;
};
class Cat : public Animal
{
public:
Cat ():Animal("cat") {}
// Over-rides the base implementation
void speak () {cout << "Meoow \n";}
};
class Dog : public Animal
{
public:
Dog ():Animal("dog") {}
void speak () {cout << "Woof \n";}
};
// Robot is implemented in terms of Animal
// It is more like composition. But can take advantage of typedefs inside Animal
class Robot : private Animal
{
public:
Robot ():Animal ("Bot") {}
using Animal::run;
};
int main ()
{
Robot bot;
bot.run ();
//Uncomment me
//bot.speak ();
cout << "Bye Bye " << endl;
}