[C++] Virtual Function
Let's think about OOP literally while reading the code below.
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
|
#include <iostream>
using namespace std;
class AAA
{
public:
void ABC(){
cout<<"AAA is called"<<endl;
}
};
class BBB : public AAA
{
public:
void ABC(){
cout<<"BBB is called"<<endl;
}
};
class CCC : public BBB
{
public:
void ABC(){
cout<<"CCC is called"<<endl;
}
};
int main(void)
{
CCC* c = new CCC;
BBB* b = c;
AAA* a = b;
a->ABC();
b->ABC();
c->ABC();
delete c;
return 0;
}
|
Result
|
AAA is called
BBB is called
CCC is called
|
From the perspective of OOP the result is strange.
What we actually called is CCC type,
but there is AAA and BBB type coming after.
The result should follow the type of object itself not the type of pointer.
So C++ have a keyword called 'virtual'.
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
|
#include <iostream>
using namespace std;
class AAA
{
public:
virtual void ABC(){
cout<<"AAA is called"<<endl;
}
};
class BBB : public AAA
{
public:
virtual void ABC(){
cout<<"BBB is called"<<endl;
}
};
class CCC : public BBB
{
public:
virtual void ABC(){
cout<<"CCC is called"<<endl;
}
};
int main(void)
{
CCC* c = new CCC;
BBB* b = c;
AAA* a = b;
a->ABC();
b->ABC();
c->ABC();
delete c;
return 0;
}
|
Just add a keyword virtual to the function.
Then the output follows the type of object itself not the type of pointer.
Result
|
CCC is called
CCC is called
CCC is called
|
Pure Virtual Function
A virtual function that have nothing implemented inside.
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
|
#include <iostream>
using namespace std;
class Person
{
private:
virtual void Move() = 0;
public:
void Do()
{
cout<<"Let's ";
Move();
}
};
class Artist: public Person
{
private:
virtual void Move()
{
cout<<"Create"<<endl;
}
};
class Athlete: public Person
{
private:
virtual void Move()
{
cout<<"Win"<<endl;
}
};
int main()
{
Artist Twice;
Athlete Son;
Twice.Do();
Son.Do();
}
|
Result
|
Let's Create
Let's Win
|
As you see pure virtual function is a fuction, that needs an overriding by derived class.
Every classes that have declaration of pure virtual function is called Abstract Class.
Which means that it's not a perfect class so it's impossible to make an object.
If every member functions of a class is pure virtual, we call the class an Interface.
Polymorphism
Polymorphism means having many forms.
And from the perspective of OOP, Polymorphism means
"A call to a member function will cause a different function to be executed depending on the type of object that invokes the function"
And when you explain OOP theory Polymorphism is one of most important ideas with Abstraction.
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
|
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape(int a, int b) : width(a), height(b)
virtual int area() = 0;
};
class Rectangle: public Shape {
public:
Rectangle(int a, int b) : Shape(a, b) { }
int area () {
cout <<"Rectangle class area :"<<width * height<<endl;
return 0;
}
};
class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout <<"Triangle class area :"<<width * height / 2<<endl;
return 0;
}
};
int main() {
Shape *shape;
Rectangle rec(12,6);
Triangle tri(6,10);
shape = &rec;
shape->area();
shape = &tri;
shape->area();
return 0;
}
|
Result
|
Rectangle class area :72
Triangle class area :30
|
line41 and line43 are same code, but when you execute the result will be different.
This is an example of Polymorphism.
[참고자료]
- 윤성우의 열혈 C++ 프로그래밍