Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

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.

Investigate the example:


Basically, a virtual function is used in the base class in order to ensure that the function is
overridden. This especially applies to cases where a pointer of base class points to an
object of a derived class.

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";
}
};

class derived : public base {


public:
void print()
{
cout << "print derived 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

// Virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();

return 0;
}

EX2

#include <iostream>
#include <string>
using namespace std;

class Animal {
private:
string type;

public:
// constructor to initialize type
Animal() : type("Animal") {}

// declare virtual function


virtual string getType() {
return type;
}
};

class Dog : public Animal {


private:
string type;

public:
// constructor to initialize type
Dog() : type("Dog") {}

string getType() override {


return type;
}
};

class Cat : public Animal {


private:
string type;

public:
// constructor to initialize type
Cat() : type("Cat") {}

string getType() override {


return type;
}
};

void print(Animal* ani) {


cout << "Animal: " << ani->getType() << endl;
}

int main() {
Animal* animal1 = new Animal();
Animal* dog1 = new Dog();
Animal* cat1 = new Cat();

print(animal1);
print(dog1);
print(cat1);

return 0;
}

You might also like