File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ using namespace std;
12
12
class Person {
13
13
public:
14
14
// does runtime binding of intro() based on the type of object is used to invoke the method
15
- void intro () {
15
+ void virtual intro () {
16
16
cout<<" Hello I am a Person" <<endl;
17
17
}
18
18
};
@@ -39,12 +39,19 @@ void getIntro(Person &p) {
39
39
40
40
int main () {
41
41
42
- Person p ;
42
+ Person *p1,*p2 ;
43
43
Anish a;
44
44
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);
48
55
49
56
return 0 ;
50
57
}
You can’t perform that action at this time.
0 commit comments