Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 12 / virtual_functions_playground.cpp
blob: 17f999731f12f6136281321fbfcdcacc7ecc9def (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>

struct A
{
	virtual void f()
	{
		std::cout << "A::f()" << std::endl;
	}
	virtual void g()
	{
		std::cout << "A::f()" << std::endl;
	}
	void non_virtual_function()
	{
		std::cout << "A::non-virt()" << std::endl;
	}
};

struct B : public A
{
	void f()
	{
		std::cout << "B::f()" << std::endl;
	}
	void g()
	{
		std::cout << "B::f()" << std::endl;
	}
	void h()
	{
		std::cout << "B::f()" << std::endl;
	}
};

struct C : public B
{
	void f()
	{
		std::cout << "C::f()" << std::endl;
	}
	void non_virtual_function()
	{
		std::cout << "C::non_virtual_function()" << std::endl;
	}

};

struct D : public C
{
	void f()
	{
		std::cout << "C::f()" << std::endl;
	}
	void d()
	{
		std::cout << "D::h()" << std::endl;
	}
};




int main()
{
	A objA;
	B objB;
	C objC;
	D objD;

	A* ptrA = &objA;

	ptrA->f();
	ptrA->g();
	ptrA->non_virtual_function();

	A* ptrB = &objB;

	ptrB->f();
	ptrB->g();
	ptrB->non_virtual_function();
	
	B* ptrC = &objC;

	ptrC->f();
	ptrC->g();
	ptrC->non_virtual_function();

	C* ptrD = &objD;

	ptrD->f();
	ptrD->g();
	ptrD->non_virtual_function();


}