-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymorphism.cpp
63 lines (50 loc) · 1.21 KB
/
polymorphism.cpp
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
// polymorphism.cpp : Defines the entry point for the console application.
//
// Use same function with different outcome
// The word polymorphism means having many forms.
/*
Polymorphic class:
Virtual functions support dynamic binding and object-oriented programming.
A class that declares or inherits a virtual function is called a polymorphic class.
Abstract class:
An abstract class is a class that can be used only as a base class of some other class;
no objects of an abstract class can be created except as subobjects of a class derived from it.
A class is abstract if it has at least one pure virtual function.
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class Enemy {
protected:
int attackPower;
public:
void setAttackPower(int a) {
attackPower = a;
}
};
class Ninja : public Enemy{
public:
void attack() {
cout << "I am a ninja, ninja chop! " << attackPower << endl;
}
private:
};
class Monster : public Enemy {
public:
void attack() {
cout << "I am a monster, bam! " << attackPower << endl;
}
private:
};
int main()
{
Ninja n;
Monster m;
Enemy *enemy1 = &n;
Enemy *enemy2 = &m;
enemy1->setAttackPower(29);
enemy2->setAttackPower(99);
n.attack();
m.attack();
return 0;
}