-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.cpp
More file actions
71 lines (58 loc) · 1.35 KB
/
inheritance.cpp
File metadata and controls
71 lines (58 loc) · 1.35 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
using namespace std;
class Empty
{
public :
void dummy_function () {}
Empty () { cout << "Empty Constructor \n";}
~Empty () {cout << "Empty Destructor \n";}
};
class Animal : public Empty
{
public:
Animal (string nam = "Who am I ?"):name(nam)
{
cout << "Animal Constructor \n";
}
void eat () {cout << name << " is Eating \n";}
void sleep () {cout << name << " is Sleeping \n";}
void speak () {}
~Animal () {cout << "Animal Destructor \n";}
protected:
string name;
};
class Cat : public Animal
{
public:
Cat ():Animal("cat") {cout << "Cat constructor \n";}
// Over-rides the base implementation
void speak () {cout << "Meoow \n";}
~Cat () {cout << "Cat Destructor \n";}
};
class Dog : public Animal
{
public:
Dog ():Animal("dog") {cout << "Dog Constructor \n";}
void speak () {cout << "Woof \n";}
~Dog () {cout << "Dog Destructor \n";}
};
int main ()
{
Animal a;
Cat myPet1;
Dog myPet2;
Animal *p = &myPet2;
myPet1.eat ();
myPet2.speak ();
myPet1.sleep ();
// Note that this makes animal to sleep
p->sleep ();
a.sleep ();
// Size of empty
cout << "Size of Empty: " << sizeof (Empty) << endl;
// Size of Animal is not sizeof Animal + 1
cout << "Size of Animal: " << sizeof (Animal) << endl;
cout << "Size of Dog: " << sizeof (Dog) << endl;
cout << "Bye Bye " << endl;
}