0% found this document useful (0 votes)
2 views1 page

C++ Virtual Function Example Code

The document presents a C++ program demonstrating the use of virtual functions with a base class 'person' and a derived class 'student'. It includes methods to print names for both classes, showcasing polymorphism where the derived class method overrides the base class method. The program attempts to use pointers to call the appropriate print function based on the object type, although it contains some errors in pointer usage and function calls.

Uploaded by

babababububu98
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

C++ Virtual Function Example Code

The document presents a C++ program demonstrating the use of virtual functions with a base class 'person' and a derived class 'student'. It includes methods to print names for both classes, showcasing polymorphism where the derived class method overrides the base class method. The program attempts to use pointers to call the appropriate print function based on the object type, although it contains some errors in pointer usage and function calls.

Uploaded by

babababububu98
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 7:

/* Write a program in c++ for virtual function*/


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
public :
virtual void print()
{
cout<<"\n The name of person is Bob";
}
virtual void show()
{
cout<<"\n show base"<<"\n";
}
};
class student: public person
{
public:
void print()
{
cout<<"\n The name of the student Tom";
}
};
void main()
{
clrscr();
person *p;
person p1;
*p=&p1;
[Link]();
student s1;
*p=&s1;
[Link]();
getch();
}

Output

You might also like