-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathanimal.cpp
More file actions
39 lines (31 loc) · 861 Bytes
/
Copy pathanimal.cpp
File metadata and controls
39 lines (31 loc) · 861 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
#include<iostream>
#include<string>
using namespace std;
class Animal {
protected:
string name;
public:
Animal(const string &s="NoName") : name(s) { } // Default value = "NoName"
void speak() { cout << " ANIMAL " << name << " : Hello, I'm " << name << endl; }
void move() { cout << " ANIMAL " << name << " : I'm moving" << endl; }
void eat() { cout << " ANIMAL " << name << " : I'm hungry!" << endl; }
};
int main() {
Animal a;
Animal * aPtr;
Animal & aRef = a;
cout << "Animal object:" << endl;
a.speak();
a.move();
a.eat();
cout << endl << "Animal pointer:" << endl;
aPtr = new Animal("Dale");
aPtr->speak();
aPtr->move();
aPtr->eat();
cout << endl << "Animal reference" << endl;
aRef.speak();
aRef.move();
aRef.eat();
return 0;
}