Object-Oriented Programming Lesson 14 of 17
C++ Inheritance and Polymorphism
Derive classes with public inheritance, call base constructors, override virtual functions, use polymorphism through base pointers and references, and create abstract classes.
Inheritance lets a class build on another: a Student is a Person with extra details. Polymorphism lets you treat different derived objects through the same base type, with each one behaving in its own way.
Inheritance
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
public:
Person(string n) : name(n) {}
void introduce() const { cout << "Hi, I'm " << name << "." << endl; }
};
class Student : public Person {
int gradeLevel;
public:
Student(string n, int g) : Person(n), gradeLevel(g) {}
void study() const {
cout << name << " (Grade " << gradeLevel << ") is studying." << endl;
}
};
int main() {
Student s("Ana", 10);
s.introduce(); // inherited
s.study(); // Student's own
return 0;
}
Output
Hi, I'm Ana. Ana (Grade 10) is studying.
class Student : public Personmeans Student inherits from Person.Person(n)in the initializer list calls the base class constructor.protectedmembers are hidden from outside code but visible to derived classes.
virtual functions and overriding
To let derived classes replace a function and have the right version called through a base pointer or reference, mark it virtual in the base class:
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Vehicle {
public:
virtual ~Vehicle() = default;
virtual double fare(double km) const { return 0; }
virtual string type() const { return "Vehicle"; }
};
class Jeepney : public Vehicle {
public:
double fare(double km) const override { return 13 + (km > 4 ? (km - 4) * 1.80 : 0); }
string type() const override { return "Jeepney"; }
};
class Tricycle : public Vehicle {
public:
double fare(double km) const override { return 20 + km * 5; }
string type() const override { return "Tricycle"; }
};
int main() {
vector<unique_ptr<Vehicle>> rides;
rides.push_back(make_unique<Jeepney>());
rides.push_back(make_unique<Tricycle>());
for (const auto &v : rides) {
cout << v->type() << ": P" << v->fare(6) << endl;
}
return 0;
}
Output
Jeepney: P16.6 Tricycle: P50
virtualtells C++ to decide at run time which version to call, based on the actual object.overrideasks the compiler to check that you're really overriding a base function.- A base class with virtual functions should have a
virtualdestructor, so deleting through a base pointer cleans up correctly.
Abstract classes
A pure virtual function (= 0) has no body; derived classes must provide one. A class with at least one is abstract and can't be created directly:
#include <iostream>
#include <vector>
#include <memory>
#include <cmath>
using namespace std;
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0; // pure virtual
virtual string name() const = 0;
};
class Circle : public Shape {
double r;
public:
Circle(double r) : r(r) {}
double area() const override { return M_PI * r * r; }
string name() const override { return "Circle"; }
};
class Square : public Shape {
double s;
public:
Square(double s) : s(s) {}
double area() const override { return s * s; }
string name() const override { return "Square"; }
};
int main() {
vector<unique_ptr<Shape>> shapes;
shapes.push_back(make_unique<Circle>(1));
shapes.push_back(make_unique<Square>(3));
double total = 0;
for (const auto &s : shapes) {
cout << s->name() << ": " << s->area() << endl;
total += s->area();
}
cout << "Total area: " << total << endl;
// Shape sh; // error: cannot declare variable of abstract type
return 0;
}
Output
Circle: 3.14159 Square: 9 Total area: 12.1416
Polymorphism with references
#include <iostream>
using namespace std;
class Animal {
public:
virtual ~Animal() = default;
virtual void speak() const { cout << "..." << endl; }
};
class Dog : public Animal {
public:
void speak() const override { cout << "Aw aw!" << endl; }
};
class Cat : public Animal {
public:
void speak() const override { cout << "Meow!" << endl; }
};
void makeItSpeak(const Animal &a) { a.speak(); }
int main() {
Dog d;
Cat c;
makeItSpeak(d);
makeItSpeak(c);
return 0;
}
Output
Aw aw! Meow!
Polymorphism works through pointers and references. Passing a derived object by value to a Animal parameter would copy only the Animal part ("slicing") and lose the override.
The four pillars of OOP
| Pillar | C++ tools |
|---|---|
| Encapsulation | private, getters/setters, const members |
| Inheritance | class B : public A |
| Polymorphism | virtual, override, base pointers/references |
| Abstraction | Pure virtual functions, abstract classes |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04