Classes & Objects
OOP Concepts
Why object-oriented programming exists, the difference from procedural code, and the four pillars — encapsulation, abstraction, inheritance and polymorphism.
Procedural programming (what you did in C) organises a program around functions that act on separate data. As programs grow, any function can modify any data, and changes ripple everywhere.
Object-oriented programming (OOP) organises a program around objects — bundles of data (attributes) and the functions that operate on it (methods). A bank account object owns its balance; nothing else may change it except through the account's own deposit and withdraw methods.
| Procedural | Object-oriented |
|---|---|
| program = functions + separate data | program = interacting objects |
| top-down design | bottom-up design: build objects, then combine them |
| data is often global and exposed | data is hidden inside objects |
| harder to reuse and extend | reuse through inheritance and composition |
| C, Pascal, Fortran | C++, Java, Python, C# |
Class and object
- A class is a blueprint: it describes what data and behaviour objects of that kind have.
- An object is an instance of a class: a real thing built from the blueprint, with its own data.
Car is a class; your red Swift with registration KA-01-1234 is an object.
The four pillars
1. Encapsulation
Bundling data with the methods that use it, and hiding the data from outside code (making it private). Outside code must go through public methods, which can check every change.
A capsule hides the medicine inside; you just swallow it.
2. Abstraction
Showing what an object does while hiding how it does it. You call car.start() without knowing about fuel injection. In C++, abstraction comes from public interfaces, abstract classes and header files.
3. Inheritance
Creating a new class from an existing one, reusing its members and adding or changing some. A Student and a Teacher can both inherit from Person. It models an is-a relationship: a Student is a Person.
4. Polymorphism
"Many forms": the same call behaves differently depending on the object. shape->area() computes a circle's area for a Circle and a rectangle's for a Rectangle.
- Compile-time polymorphism: function and operator overloading.
- Run-time polymorphism: virtual functions.
A preview in code
You will understand every line by the end of this unit:
#include <iostream>
#include <string>
using namespace std;
class Shape { // abstraction: what every shape can do
public:
virtual double area() const = 0;
virtual string name() const = 0;
virtual ~Shape() {}
};
class Circle : public Shape { // inheritance
double r; // encapsulation: hidden data
public:
Circle(double r) : r(r) {}
double area() const override { return 3.14159 * r * r; }
string name() const override { return "Circle"; }
};
class Rectangle : public Shape {
double w, h;
public:
Rectangle(double w, double h) : w(w), h(h) {}
double area() const override { return w * h; }
string name() const override { return "Rectangle"; }
};
int main() {
Circle c(1.5);
Rectangle r(4, 2.5);
const Shape *shapes[] = {&c, &r};
for (const Shape *s : shapes) // polymorphism: one call, many behaviours
cout << s->name() << " area = " << s->area() << endl;
return 0;
}Circle area = 7.06858 Rectangle area = 10
Other OOP terms
| Term | Meaning |
|---|---|
| Message passing | objects interact by calling each other's methods |
| Dynamic binding | the method to run is chosen at run time (virtual functions) |
| Composition | a class contains objects of other classes — a has-a relationship (a Car has an Engine) |
| Coupling | how much classes depend on each other — keep it low |
| Cohesion | how focused a class is on one job — keep it high |
Test yourself
4 questions · pick an answer to check it instantly
- 1
What is an object?
The class is the blueprint; each object is one concrete instance with its own data. - 2
Hiding a class's data and allowing access only through its methods is called...
Encapsulation bundles data with methods and restricts direct access. - 3
"A
Dogis anAnimal" is modelled with...An is-a relationship is inheritance; has-a is composition. - 4
Function overloading is an example of...
The compiler picks the overload from the argument types at compile time.