Skip to content

Commit a227b6c

Browse files
authored
Merge pull request Astrodevil#398 from gaonkarsahil/virtual-function-cpp
Program to implement virtual function.
2 parents 031ac67 + bd2935a commit a227b6c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

C++/virtualFunction.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Program to implement virtual function.
2+
3+
#include <iostream>
4+
5+
using namespace std;
6+
7+
class Base {
8+
public:
9+
void display() {
10+
cout<< "Display Base function.\n";
11+
}
12+
13+
virtual void show() {
14+
cout<< "Show Base function.\n\n";
15+
}
16+
};
17+
18+
class Derived: public Base {
19+
public:
20+
void display() {
21+
cout<< "Display Base function.\n";
22+
}
23+
24+
void show() {
25+
cout<< "Show Base function.\n";
26+
}
27+
};
28+
29+
int main() {
30+
Base B;
31+
Derived D;
32+
Base *bptr;
33+
34+
cout<<"bptr points to Base\n";
35+
bptr = &B;
36+
bptr->display();
37+
bptr->show();
38+
39+
cout<<"bptr points to Derived\n";
40+
bptr = &D;
41+
bptr->display();
42+
bptr->show();
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)