C++ and Object-Oriented Programming
OOP Concepts — Overview; Encapsulation; Abstraction
C-CAT
OOP Concepts — Overview
The Four Pillars of OOP
+----------------------------------------------+
| Object-Oriented Programming |
+----------+----------+----------+-------------+
|Encaps- |Abstraction|Inheritance|Polymorphism|
|ulation | | | |
+----------+----------+----------+-------------+
Object State, Behavior, Identity
Every object has three characteristics:
| Characteristic | Definition | Example (Car) |
|---|---|---|
| State | Values stored in data members | Color=Red, Speed=60km/h, Fuel=50L |
| Behavior | What an object does (its methods) | accelerate(), brake(), turn() |
| Identity | Unique identifier distinguishing objects | VIN number (each car unique) |
class Car {
private:
// STATE (data members)
string color;
int speed;
float fuel;
public:
// BEHAVIOR (member functions)
void accelerate(int by) { speed += by; }
void brake() { if (speed > 0) speed -= 10; }
void display() {
cout << "Color: " << color << ", Speed: " << speed << endl;
}
// IDENTITY: each Car object has unique address in memory
};
Encapsulation
What is Encapsulation?
Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class), while hiding the implementation details.
Benefits:
- Data protection — external code cannot directly modify data
- Implementation can change without affecting users
- Better maintainability and readability
Access Specifiers
| Specifier | Accessible from |
|---|---|
private | Only within the class (default for class) |
protected | Within class + derived classes |
public | Anywhere (from main, other classes) |
class BankAccount {
private:
// Hidden from outside - ENCAPSULATED
double balance;
string account_number;
string password;
public:
// Controlled access through public methods
BankAccount(string acc_no, string pwd, double initial_balance) {
account_number = acc_no;
password = pwd;
balance = initial_balance;
}
bool deposit(double amount) {
if (amount <= 0) return false;
balance += amount;
return true;
}
bool withdraw(double amount, string pwd) {
if (pwd != password) {
cout << "Wrong password!\n";
return false;
}
if (amount > balance) {
cout << "Insufficient funds!\n";
return false;
}
balance -= amount;
return true;
}
double get_balance(string pwd) {
if (pwd != password) {
cout << "Wrong password!\n";
return -1;
}
return balance;
}
};
Getters and Setters
class Employee {
private:
int id;
string name;
double salary; // private - cannot be negative
public:
// Getter
double get_salary() const { return salary; }
string get_name() const { return name; }
// Setter with validation
bool set_salary(double s) {
if (s < 0) return false; // validate!
salary = s;
return true;
}
void set_name(string n) {
if (!n.empty()) name = n;
}
};
Abstraction
What is Abstraction?
Abstraction is showing only the essential features (interface) of an object while hiding the complex implementation details.
ATM Machine Example:
What you see (Interface): PIN pad, card slot, cash tray, buttons
What's hidden (Impl): Banking protocol, database, security, cash counting
Abstraction vs Encapsulation:
| Concept | Focus | How |
|---|---|---|
| Abstraction | Hiding complexity; showing essential | Interface (abstract class, pure virtual) |
| Encapsulation | Hiding internal state; controlled access | Access specifiers (private/protected) |
Continue learning
Related notes
Definition of AI; Need of AI
Artificial Intelligence
Introduction to Data Engineering; Big Data — The 5 V's; Types of Data
Big Data and Data Engineering
Introduction to C Programming; C Program Structure; Data Types and Variables
C Programming
What Is a Computer?; Machine Cycle: Fetch–Decode–Execute; CPU Organization
Computer Architecture
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.