Skip to content

Commit f0a3510

Browse files
committed
changes in virtual function program
1 parent 5006081 commit f0a3510

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

VirtualFunctions.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using namespace std;
1212
class Person {
1313
public:
1414
//does runtime binding of intro() based on the type of object is used to invoke the method
15-
void intro() {
15+
void virtual intro() {
1616
cout<<"Hello I am a Person"<<endl;
1717
}
1818
};
@@ -39,12 +39,19 @@ void getIntro(Person &p) {
3939

4040
int main() {
4141

42-
Person p;
42+
Person *p1,*p2;
4343
Anish a;
4444
Mrinal m;
45-
a.intro();
46-
getIntro(a);//without virtual base function , it would have printed the base method value
47-
getIntro(m);
45+
p1 = &a;
46+
p2= &m;
47+
p2 = &m;
48+
49+
//runtime polymorphism
50+
p1->intro();//intro() method of class Anish called
51+
p2->intro();//intro() method of class Mrinal called
52+
// a.intro();
53+
// getIntro(a);//without virtual base function , it would have printed the base method value
54+
// getIntro(m);
4855

4956
return 0;
5057
}

0 commit comments

Comments
 (0)