8000 Virtual table · DhirajGadekar/Cpp_programming@014b305 · GitHub
[go: up one dir, main page]

Skip to content

Commit 014b305

Browse files
committed
Virtual table
1 parent 335fe08 commit 014b305

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Polymorphism/13_vptr.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<iostream>
2+
3+
class Parent {
4+
5+
public:
6+
virtual void getData() {
7+
8+
std::cout << "Paren Getdata" << std::endl;
9+
}
10+
};
11+
12+
class Child : public Parent {
13+
14+
public:
15+
virtual void getData() {
16+
17+
std::cout << "Child Getdata" << std::endl;
18+
}
19+
};
20+
21+
int main() {
22+
23+
Parent *obj = new Child();
24+
obj->getData();
25+
//Internal Call : obj->_vptr->getData();
26+
27+
std::cout << obj->_vptr << std::endl;
28+
29+
return 0;
30+
}

Polymorphism/14_VirtualTable.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<iostream>
2+
3+
void add(int x , int y) {
4+
5+
std::cout << (x + y) << std::endl;
6+
}
7+
8+
void division(int x , int y) {
9+
10+
std::cout << (x / y) << std::endl;
11+
}
12+
13+
void mul(int x , int y) {
14+
15+
std::cout << (x * y) << std::endl;
16+
}
17+
18+
int main() {
19+
20+
void (*funArray[3])(int , int) = {add, division, mul};
21+
22+
for(int i = 0; i < 3; i++) {
23+
24+
funArray[i](10, 20);
25+
}
26+
return 0;
27+
}

0 commit comments

Comments
 (0)
0