CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Cyber Safety and Ethics · 7 min · free

Polymorphism and Virtual Functions

7 min4 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Polymorphism means one name with many forms: overloading is decided at compile time, virtual functions at run time.

On screen in this lesson

What is polymorphism?

Poly means many, morph means forms
One name behaves in many ways
Example: a + b adds numbers or joins strings
Decided at compile time or at run time

Two kinds of polymorphism

PointCompile timeRun time
Also calledstatic, earlydynamic, late
Decidedby compilerwhile running
Achieved byoverloadingvirtual functions
Speedfastera little slower
Flexibilitylessmore

Operator overloading

Give an operator a new meaning for a class
Write a function named operator+
Cannot overload :: . .* ?: and sizeof
Cannot change precedence or operand count

Rules for virtual functions

Declared with virtual in the base class
Works through a base pointer or reference
Same name and arguments in the derived class
A constructor cannot be virtual
Use a virtual destructor in a base class

Pure virtual function

A virtual function with = 0
It has no body in the base class
Example: virtual double area() = 0;
Every derived class must override it

Abstract classes

Has at least one pure virtual function
Cannot create its objects
Can have pointers and references to it
Acts as a common design for derived classes

Quick answers

Without virtual, what does p->draw() print when p points to a Circle?

Shape, because the pointer type decides.

Can you create an object of an abstract class?

No. It gives a compile error.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. You ask a circle to draw itself, but the plain Shape version runs instead. Why? The answer is one missing word, virtual. Today we learn compile time and run time polymorphism, operator overloading, virtual functions and abstract classes.

Let us start with the meaning. Poly means many and morph means forms. Polymorphism means one name that behaves in different ways. For example, the plus sign adds two numbers, but it joins two strings. The right form is chosen either at compile time, before the program runs, or at run time, while it runs.

There are two kinds, and exams often ask you to compare them. Compile time polymorphism is also called static or early binding, and run time is called dynamic or late binding. In compile time, the compiler decides which function to call, but in run time, the choice is made while the program runs. Compile time uses function and operator overloading, while run time uses virtual functions. Compile time is a little faster. Run time is more flexible.

Function overloading is compile time polymorphism. Here, two functions share the name add. One adds two integers, and the other adds two doubles, which are decimal numbers. The compiler looks at the argument types and picks the right add before the program runs. So it prints five, then three point five.

Operator overloading is also compile time polymorphism. It gives an operator like plus a new meaning for objects of our own class. We write a special function whose name is the word operator followed by the symbol, like operator plus. A few operators cannot be overloaded, such as scope resolution, dot, dot star, the ternary operator and sizeof. Overloading also cannot change an operator's precedence or how many operands it takes.

Here is a canteen example. Bill is a struct, a class with public members, holding an amount. We define operator plus, which takes another bill b. It returns a new bill whose amount is the sum. Now tea plus vada works like numbers. Ten plus twenty five gives a bill of thirty five rupees.

Now the problem from our title. Shape has a draw function, and Circle, derived from Shape, has its own draw. We make a base class pointer p, which holds the address of the circle c. The arrow calls draw through the pointer. We expect Circle, but it prints Shape. The compiler looked only at the pointer type, Shape, at compile time.

We add one word, virtual, before draw in the base class. A virtual function is a base class function that a derived class can override. Override means giving it a new body with the same name and arguments. Now the choice is made at run time, by the real object the pointer points to. The object is a Circle, so it prints Circle.

Remember these rules for virtual functions. The keyword virtual is written in the base class. Run time choice happens only when we call through a base class pointer or reference. The derived function must have the same name and arguments. A constructor cannot be virtual. But a base class used with pointers should have a virtual destructor, so derived objects are cleaned up correctly.

Here is why this is useful. S is a shape, T is a triangle, and Q is a square. Each returns its number of sides through n. The array a holds base pointers to a triangle and a square. One loop calls n through each pointer. Because n is virtual, each object answers for itself. It prints three, then four.

Sometimes a base class cannot give a sensible body. What is the area of a general shape? Nobody knows. So we declare a pure virtual function, by writing equals zero after it. It has no body in the base class. Example, virtual double area, equals zero. A derived class must override it, or that derived class also stays abstract.

This brings us to abstract classes. A class with at least one pure virtual function is called an abstract class. We cannot create an object of an abstract class, and trying gives a compile error. But we can make pointers and references of its type. An abstract class is a common design, like a form every derived class must fill.

Here Shape is abstract, because area equals zero is pure virtual. Writing Shape x in main would be an error. Sq, the square, overrides area and returns side times side. The object q is a square with side five. Through the base pointer p, the square's area runs, and it prints twenty five.

Pause and predict. Go back to the Shape and Circle program. Remove the word virtual from the base draw. The pointer p still holds the address of a Circle. What prints now? It prints Shape, because without virtual, the call is fixed at compile time by the pointer type.

Let us revise what we learned today. Compile time polymorphism uses function overloading and operator overloading. Run time polymorphism uses virtual functions called through base class pointers or references. A function named operator plus gives plus a meaning for our class. A pure virtual function ends with equals zero and has no body. A class with one is abstract, so we cannot create its objects.

Courses that teach this

CourseUnit
Programming All levels C++Object-Oriented Programming

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →