Object-Oriented Programming (OOP) is usually taught in BSCS/BSIT 2nd semester, right after Programming Fundamentals. This guide explains classes, objects and the four pillars of OOP in C++ (encapsulation, abstraction, inheritance and polymorphism) with simple examples you can type and run.
What Is OOP?
In procedural programming you write functions that work on separate data. In OOP you group data and the functions that work on it together into a single unit called a class. This makes large programs easier to organise, reuse and maintain.
Classes and Objects
A class is a blueprint; an object is a real thing made from that blueprint. "Student" is a class; Ali and Sara are objects.
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
double cgpa;
void show() {
cout << name << " has CGPA " << cgpa << endl;
}
};
int main() {
Student s1; // object
s1.name = "Ali";
s1.cgpa = 3.45;
s1.show();
return 0;
}
Constructors and Destructors
A constructor runs automatically when an object is created and is used to give initial values. It has the same name as the class and no return type. A destructor (~ClassName()) runs when the object is destroyed.
class Student {
public:
string name;
Student(string n) { name = n; } // constructor
~Student() { cout << "Bye " << name; } // destructor
};
Pillar 1: Encapsulation
Encapsulation means hiding data inside a class and allowing access only through public functions (getters and setters). This protects data from invalid changes.
class Account {
private:
double balance = 0;
public:
void deposit(double amount) {
if (amount > 0) balance += amount;
}
double getBalance() { return balance; }
};
Access specifiers: private (only inside the class), protected (class and derived classes) and public (everywhere).
Pillar 2: Abstraction
Abstraction means showing only the essential features and hiding the details. When you call deposit(), you don't need to know how the balance is stored. In C++, abstract classes (with pure virtual functions) are a common way to define what a class must do without saying how.
Pillar 3: Inheritance
Inheritance lets a new class (derived class) reuse the members of an existing class (base class).
class Person {
public:
string name;
void greet() { cout << "Hi, I am " << name << endl; }
};
class Teacher : public Person { // Teacher inherits Person
public:
string subject;
};
Types of inheritance: single, multiple, multilevel, hierarchical and hybrid.
Pillar 4: Polymorphism
Polymorphism means "many forms": the same function name behaves differently in different situations.
- Compile-time polymorphism: function overloading and operator overloading.
- Run-time polymorphism: function overriding with
virtualfunctions.
class Shape {
public:
virtual double area() { return 0; }
};
class Circle : public Shape {
double r;
public:
Circle(double radius) { r = radius; }
double area() override { return 3.14159 * r * r; }
};
A Shape* pointer that points to a Circle calls Circle::area() at run time because the function is virtual.
OOP vs Procedural Programming
| Procedural | Object-Oriented |
|---|---|
| Program is divided into functions | Program is divided into objects |
| Data moves freely between functions | Data is hidden and protected |
| Harder to reuse and extend | Easy reuse through inheritance |
| Example: C | Examples: C++, Java, C#, Python |
Practice Questions
- Create a
Rectangleclass with length and width and a function that returns the area. - Add a constructor to the
Rectangleclass and create three objects. - Make a
BankAccountclass with private balance and public deposit/withdraw functions. - Create a base class
Animalwith a virtualsound()function and override it inDogandCat.
Test yourself with our free C++ MCQ practice test, or revise the basics in C++ guides.
FAQ
What are the four pillars of OOP?
Encapsulation, abstraction, inheritance and polymorphism.
What is the difference between a class and an object?
A class is a blueprint that defines data and functions; an object is an instance of that class created in memory.
0 comments:
Post a Comment