C++ OOP Classes Inheritance Polymorphism & Templates
Introduction
Object-Oriented Programming (OOP) is one of the most important concepts in modern programming. When learning C++, understanding C++ OOP concepts such as classes, inheritance, polymorphism, and templates helps developers build scalable, reusable, and maintainable software.
In simple terms, C++ OOP allows programmers to organize code using objects that represent real-world entities. Instead of writing long procedural programs, developers can structure their code into logical units that model real-world systems.
For example, imagine a university management system in Lahore. Instead of writing separate functions for students, courses, and teachers, we can create classes like Student, Teacher, and Course. These classes contain data and functions that operate on that data.
For Pakistani students studying programming at universities or preparing for software development careers, mastering C++ classes, inheritance, polymorphism, and templates is essential. These concepts are used in:
- Game development
- Operating systems
- Financial systems
- Embedded systems
- Large-scale software projects
Many software engineers in Karachi, Islamabad, and Lahore tech companies rely on these concepts to build efficient systems.
By the end of this tutorial, you will understand:
- How C++ classes work
- How inheritance helps reuse code
- How polymorphism allows flexible behavior
- How templates enable generic programming
Let’s begin.
Prerequisites
Before learning C++ OOP, you should already understand the following concepts:
- Basic C++ syntax
- Variables and data types
- Functions
- Control structures (
if,for,while) - Basic memory concepts
If you are new to C++, consider reviewing:
- Basic C++ syntax
- Functions and arrays
- Input and output using
cinandcout
Once you are comfortable with these fundamentals, you are ready to explore object-oriented programming in C++.
Core Concepts & Explanation
Classes and Objects in C++
A class is a blueprint used to create objects. It defines properties (variables) and behaviors (functions).
For example, consider a Student class used in a university system.
#include <iostream>
using namespace std;
class Student {
public:
string name;
int rollNumber;
void display() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
}
};
int main() {
Student s1;
s1.name = "Ahmad";
s1.rollNumber = 101;
s1.display();
return 0;
}
Line-by-line explanation:
class Student {→ Defines a new class namedStudent.public:→ Members under this section can be accessed outside the class.string name;→ Variable storing the student's name.int rollNumber;→ Variable storing the roll number.void display()→ Member function that prints student data.Student s1;→ Creates an object of classStudent.s1.name = "Ahmad";→ Assigns a name to the object.s1.rollNumber = 101;→ Assigns a roll number.s1.display();→ Calls the function to display information.
Classes help organize code and represent real-world entities.
Inheritance in C++
Inheritance allows a class to inherit properties and methods from another class.
This helps avoid code duplication and improves code reuse.
Example: A University system where Student inherits from Person.
#include <iostream>
using namespace std;
class Person {
public:
string name;
void showName() {
cout << "Name: " << name << endl;
}
};
class Student : public Person {
public:
int rollNumber;
void showRoll() {
cout << "Roll Number: " << rollNumber << endl;
}
};
int main() {
Student s;
s.name = "Fatima";
s.rollNumber = 202;
s.showName();
s.showRoll();
return 0;
}
Explanation:
class Person→ Base class containing common attributes.class Student : public Person→ Student inherits from Person.s.name→ Accessible because of inheritance.showName()→ Function inherited fromPerson.
Inheritance is useful in many systems like banking software, school systems, and employee management systems.
Polymorphism in C++
Polymorphism means one interface with multiple behaviors.
There are two main types in C++:
- Compile-time polymorphism (Function Overloading)
- Runtime polymorphism (Virtual Functions)
Example using virtual functions.
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
class Cat : public Animal {
public:
void sound() {
cout << "Cat meows" << endl;
}
};
int main() {
Animal* a;
Cat c;
a = &c;
a->sound();
return 0;
}
Explanation:
virtual void sound()→ Declares a virtual function.Cat : public Animal→ Cat inherits Animal.Animal* a→ Pointer to base class.a = &c→ Points to Cat object.a->sound()→ Calls Cat’s version due to runtime polymorphism.
This mechanism uses a vtable internally.

Templates in C++
Templates allow generic programming, meaning the same code works with different data types.
Example: Template function.
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << add<int>(5, 3) << endl;
cout << add<double>(2.5, 3.7) << endl;
return 0;
}
Explanation:
template <typename T>→ Declares a template with typeT.T add(T a, T b)→ Function that works with any type.add<int>(5,3)→ Uses integers.add<double>(2.5,3.7)→ Uses double values.
Templates are heavily used in the Standard Template Library (STL).
Practical Code Examples
Example 1: Bank Account Class
A simple banking system for a student in Karachi.
#include <iostream>
using namespace std;
class BankAccount {
public:
string name;
double balance;
void deposit(double amount) {
balance += amount;
}
void display() {
cout << "Account Holder: " << name << endl;
cout << "Balance: PKR " << balance << endl;
}
};
int main() {
BankAccount acc;
acc.name = "Ali";
acc.balance = 5000;
acc.deposit(2000);
acc.display();
return 0;
}
Explanation
class BankAccount→ Represents a bank account.deposit()→ Adds money to the balance.balance += amount→ Updates the balance.display()→ Prints account details.
This demonstrates how classes model real-world systems.
Example 2: Real-World Application (Employee System)
#include <iostream>
using namespace std;
class Employee {
public:
virtual void salary() {
cout << "Base salary calculated" << endl;
}
};
class Manager : public Employee {
public:
void salary() {
cout << "Manager salary with bonus" << endl;
}
};
int main() {
Employee* e;
Manager m;
e = &m;
e->salary();
return 0;
}
Explanation
Employee→ Base class.Manager→ Derived class.salary()→ Overridden function.Employee* e→ Base pointer referencing derived object.e->salary()→ Calls manager version due to polymorphism.

Common Mistakes & How to Avoid Them
Mistake 1: Forgetting Virtual Functions
Without virtual, runtime polymorphism will not work.
Wrong
class Animal {
public:
void sound() {
cout << "Animal sound";
}
};
Correct
class Animal {
public:
virtual void sound() {
cout << "Animal sound";
}
};
Always mark base functions virtual when you expect overriding.
Mistake 2: Overusing Inheritance
Sometimes students create unnecessary inheritance chains.
Bad design
Person → Student → Undergraduate → ComputerScienceStudent
Instead, use composition when possible.
Example:
class Student {
Address addr;
};
This improves maintainability.

Practice Exercises
Exercise 1: Student Class
Problem
Create a class Student with:
- Name
- Marks
- Function to display result
Solution
#include <iostream>
using namespace std;
class Student {
public:
string name;
int marks;
void display() {
cout << name << " scored " << marks << endl;
}
};
int main() {
Student s;
s.name = "Ahmad";
s.marks = 85;
s.display();
return 0;
}
Exercise 2: Template Calculator
Problem
Create a template function that multiplies two numbers.
Solution
#include <iostream>
using namespace std;
template <typename T>
T multiply(T a, T b) {
return a * b;
}
int main() {
cout << multiply<int>(4,5) << endl;
cout << multiply<double>(2.5,4.1) << endl;
return 0;
}
Frequently Asked Questions
What is C++ OOP?
C++ OOP is a programming paradigm that organizes code into objects and classes. It helps structure large programs by modeling real-world entities and improving code reuse.
What are C++ classes used for?
C++ classes are used to define objects that combine variables and functions into a single unit. They help organize code and represent real-world concepts like students, employees, or bank accounts.
How does polymorphism work in C++?
Polymorphism allows one interface to represent multiple behaviors. In C++, this is commonly achieved using virtual functions, enabling runtime method selection.
What are templates in C++?
Templates allow programmers to write generic code that works with multiple data types. They are widely used in the Standard Template Library (STL).
When should I use inheritance?
Inheritance should be used when there is a clear “is-a” relationship between classes, such as Student being a type of Person.
Summary & Key Takeaways
- C++ classes allow developers to create structured objects.
- Inheritance enables code reuse between related classes.
- Polymorphism allows flexible behavior using virtual functions.
- Templates support generic programming for multiple data types.
- OOP makes software easier to maintain and scale.
- These concepts are essential for modern C++ development.
Next Steps & Related Tutorials
If you want to deepen your C++ knowledge, explore these tutorials on theiqra.edu.pk:
- Learn advanced data structures in C++ STL (Standard Template Library)
- Understand object-oriented programming concepts in Java OOP
- Explore memory management and C++ pointers
- Build efficient programs with C++ algorithms and data structures
These tutorials will help you continue your journey toward becoming a professional software developer.
Test Your Python Knowledge!
Finished reading? Take a quick quiz to see how much you've learned from this tutorial.