-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolymorphismMethods.cpp
More file actions
47 lines (41 loc) · 1.16 KB
/
polymorphismMethods.cpp
File metadata and controls
47 lines (41 loc) · 1.16 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
#include <iostream>
class Bird {
public:
virtual void Talk() const {
std::cout << "A bird talks" << std::endl;
}
virtual void Fly() const {
std::cout << "A bird flies" << std::endl;
}
};
class Pigeon:public Bird {
public:
void Talk() const {
std::cout << "Coo Coo" << std::endl;
}
void Fly() const {
std::cout << "A pigeon flies away..." << std::endl;
}
};
class Chicken:public Bird {
public:
void Talk() const {
std::cout <<"Cluck Cluck" << std::endl;
}
void Fly() const {
//backslash is used to escape the special characters
std::cout <<"I\'m just a chicken, I can\'t fly!" << std::endl;
}
};
int main() {
//base class pointers binding derived class objects
Bird* ptrPigeon = new Pigeon();
Bird* ptrChicken = new Chicken();
ptrPigeon -> Talk();
ptrPigeon -> Fly();
ptrChicken -> Talk();
ptrChicken -> Fly();
ptrPigeon -> Bird::Talk();
ptrPigeon -> Bird::Fly(); //inappropriate use of scope resolution operator(use capability class)
return 0;
}