Professional Documents
Culture Documents
Virtual Function in C++
Virtual Function in C++
A virtual function is a member function which is declared within a base class and is re-
defined (overridden) by a derived class. When you refer to a derived class object using a
pointer or a reference to the base class, you can call a virtual function for that object and
execute the derived class’s version of the function.
● Virtual functions ensure that the correct function is called for an object, regardless of
the type of reference (or pointer) used for function call.
● Functions are declared with a virtual keyword in base class.
● The resolving of function call is done at runtime.
Q1
Class A and Class B both contain values of an integer. In class A the value of int is 10, in
Class B 20. Both classes have a display function that outputs the value of integer. Using
virtual function approach display both values.
Q2
This problem is to get you familiar with virtual functions. Create three classes Person,
Professor and Student. The class Person should have data members name and age. The
classes Professor and Student should inherit from the class Person.
The class Professor should have two integer members: publications and cur_id. There will
be two member functions: getdata and putdata. The function getdata should get the input
from the user: the name, age and publications of the professor. The function putdata should
print the name, age, publications and the cur_id of the professor.
The class Student should have two data members: marks, which is an array of size
and cur_id. It has two member functions: getdata and putdata. The function getdata should
get the input from the user: the name, age, and the marks of the student in
subjects. The function putdata should print the name, age, sum of the marks and the cur_id
of the student.
For each object being created of the Professor or the Student class, sequential id's should
be assigned to them starting from 1
Solve this problem using virtual functions, constructors and static variables. You can create
more data members if you want.
Q3
Write code similar to example two which will output animal behaviour according to animal
type
Pigeon flies
Chihuahua runs
Pollock swims.
THE CODE
EX1
#include<iostream>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base class\n";
}
void show()
{
cout << "show base class\n";
}
};
void show()
{
cout << "show derived class\n";
}
};
int main()
{
base* bptr; //create pointer of base
derived d;
bptr = &d; //the pointer points at derived
return 0;
}
EX2
#include <iostream>
#include <string>
using namespace std;
class Animal {
private:
string type;
public:
// constructor to initialize type
Animal() : type("Animal") {}
public:
// constructor to initialize type
Dog() : type("Dog") {}
public:
// constructor to initialize type
Cat() : type("Cat") {}
int main() {
Animal* animal1 = new Animal();
Animal* dog1 = new Dog();
Animal* cat1 = new Cat();
print(animal1);
print(dog1);
print(cat1);
return 0;
}