Abstraction
Introduction to Abstraction
• Abstraction is one of the fundamental principles of Object-Oriented
Programming (OOP).
• It means hiding the internal implementation details and showing
only the essential features of an object to the outside world.
• Abstraction allows you to focus on what an object does rather than
how it does it.
Introduction to Abstraction
Think of a TV remote:
• You press a button to increase the volume.
• You don’t care how the signal is transmitted inside the remote.
• You only know what happens when you press the button — the volume
increases.
That's abstraction: focusing on what it does, not how.
Introduction to Abstraction
Abstraction vs Encapsulation
• Encapsulation
• the process of binding data (variables) and functions (methods) that operate on the data
into a single unit (class), and restricting direct access to some of the object's components.
Purpose:
• To protect the data and ensure it is used only in intended ways.
How it works:
• Achieved using access specifiers like private, public, and protected.
• Getter and setter methods control access.
Abstraction vs Encapsulation
• Abstraction
• The process of hiding the complex implementation details and showing only the
essential features of the object.
• Purpose:
• To reduce complexity and allow the programmer to focus on interactions at a higher level.
• How it works:
• Achieved through abstract classes, interfaces, or pure virtual functions.
• Only the function signature is visible to the user, not the internal logic.
Feature Abstraction Encapsulation
Focus Hiding implementation details Hiding data and
restricting access
Goal Reduce complexity Protect data integrity
Achieved by Abstract classes, interfaces
Classes, access
specifiers
Visibility
Only essential features are
shown
Internal state is
hidden from the
outside
Example
keyword
abstract, virtual (C++), interface
private, public,
protected
Abstraction vs Encapsulation
Abstraction in C++
•In C++, abstraction is achieved using:
• Classes and Objects
• Access Specifiers (private, protected, public)
• Abstract Classes and Pure Virtual Functions
Using Classes and Objects
• Classes allow you to group data (variables) and functions (methods)
together, and expose only what is necessary via public methods. The
complex logic inside the methods remains hidden from the user.
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) {
return a + b; // implementation hidden from user
}
int multiply(int a, int b) {
return a * b;
}
};
int main() {
Calculator calc;
cout << "Sum: " << calc.add(5, 3) << endl;
cout << "Product: " << calc.multiply(5, 3) << endl;
return 0;
The user of Calculator class only knows how to
use add() and multiply(), not how they work
internally.
• C++ access specifiers — private, protected, and public — allow to hide secret
data and expose only what’s needed
class Account {
private:
double balance;
public:
void deposit(double amount);
void withdraw(double amount);
};
How private members hide details from users of a class
user see only interface (deposit/withdraw), not how balance is handled internally.
Abstract Classes and Pure Virtual Functions
• A virtual function that is only declared but not defined (no
implementation) in the base class is called a pure virtual function.
• Syntax of pure virtual function
• A function is made pure virtual by preceding its declaration with the keyword
virtual and post-fixing it with zero.
• virtual int area() = 0; // pure virtual function
• An abstract class in C++ is a class that must contain at least one pure
virtual function.
• You cannot create an object of an abstract class.
Abstract Classes and Pure Virtual Functions
Why Use Pure Virtual Functions?
• To define common interface for all derived classes.
• To enforce implementation of certain functions in child classes.
• Supports runtime polymorphism
Case study
• Imagine you're hired by a software company to develop an Employee
Management System for a large organization. The company has
different types of employees: Permanent, Contract, and Intern. Every
employee must be able to:
• Calculate their salary.
• Display their employee details.
• However, the way the salary is calculated is different for each type of
employee:
• Permanent employees have a fixed base salary and bonuses.
• Contract employees are paid based on hourly rates.
• Interns get a fixed stipend.
// Abstract Base Class
class Employee {
public:
string name;
int id;
Employee(string n, int i) :
name(n), id(i) {}
// Pure virtual functions
virtual void calculateSalary() = 0;
virtual void displayInfo() = 0;
};
Notable Things in case study:
Imagine you're hired by a software company to develop an Employee Management System for a large
organization. The company has different types of employees: Permanent, Contract, and Intern. Every employee
must be able to:
Calculate their salary.
Display their employee details.
However, the way the salary is calculated & information to be displayed are different for each
type of employee
// Derived class for Permanent Employee
class PermanentEmployee : public Employee {
float baseSalary;
float bonus;
public:
PermanentEmployee(string n, int i, float b, float bn) : Employee(n, i), baseSalary(b), bonus(bn) {}
void calculateSalary() override {
cout << "Total Salary (Permanent): " << baseSalary + bonus;
}
void displayInfo() override {
cout << "Permanent Employee: " << name << " (ID: " << id << ")";
}
};
Permanent employees have a fixed base salary and bonuses
// Derived class for Contract Employee
class ContractEmployee : public Employee {
float hourlyRate;
int hoursWorked;
public:
ContractEmployee(string n, int i, float rate, int hours)
: Employee(n, i), hourlyRate(rate), hoursWorked(hours) {}
void calculateSalary() override {
cout << "Total Salary " << hourlyRate * hoursWorked;
}
void displayInfo() override {
cout << "Contract Employee: " << name << " (ID: " << id << ")";
}
};
Contract employees are paid based on hourly rates
// Derived class for Intern
class Intern : public Employee {
float stipend;
public:
Intern(string n, int i, float s) : Employee(n, i), stipend(s) {}
void calculateSalary() override {
cout << "Total Salary (Intern): " << stipend << endl;
}
void displayInfo() override {
cout << "Intern: " << name << " (ID: " << id << ")" << endl;
}
};
Interns get a fixed stipend
// Main Function
int main() {
Employee* e1 = new PermanentEmployee("Alice", 101, 50000, 5000);
Employee* e2 = new ContractEmployee("Bob", 102, 100, 120);
Employee* e3 = new Intern("Charlie", 103, 8000);
e1->displayInfo();
e1->calculateSalary();
e2->displayInfo();
e2->calculateSalary();
e3->displayInfo();
e3->calculateSalary();
// Clean up
delete e1;
delete e2;
delete e3;
return 0;
}
Key Takeaways:
•The Employee class defines the interface with pure virtual functions- calculateSalary() and displayInfo(),.
•Each derived class must implement calculateSalary() and displayInfo().
•You can use a base class pointer to handle multiple employee types using runtime polymorphism.
Abstract class
•An abstract class is a class that
• contain at least one pure virtual function.
• cannot be instantiated meaning that object of an abstract class cannot created
• Is used to provide a common interface for derived classes.
• A concrete class is a class that:
• Implements all inherited pure virtual functions (if any),
• Can be instantiated (i.e., you can create objects from it),
• Is not abstract.
Type Can Be Instantiated? Has Pure Virtual
Functions?
Used For
Abstract Class ❌ No ✅ Yes Interface/blueprint
Concrete Class ✅ Yes ❌ No (or implements all) Actual objects
Must all derived classes override a pure
virtual function?
• Yes, if Derived class overrides all pure virtual functions.
• Then it becomes a concrete class, and you can create objects of it.
• No, if the Derived class does NOT override all pure virtual functions
• Then the derived class is still abstract and cannot be instantiated.
#include <iostream>
using namespace std;
class Base {
public:
virtual void func1() = 0;
virtual void func2() = 0;
};
class Derived : public Base {
public:
void func1() override {
cout << "func1 implementedn";
}
};
int main() {
// Derived d; // Error: Derived is still abstract
}
class Derived : public Base {
public:
void func1() override {
cout << "func1 implementedn";
}
void func2() override {
cout << "func2 implementedn";
}
};
int main() {
Derived d; // OK
d.func1();
d.func2();
}
Class Derived overrides all pure virtual functions. So it is concrete
class
Class Derived overrides only func1() so it is an absract class
Abstraction in Object-oriented Programming
Abstraction in Object-oriented Programming
Abstraction in Object-oriented Programming
Abstraction in Object-oriented Programming

Abstraction in Object-oriented Programming

  • 1.
  • 2.
    Introduction to Abstraction •Abstraction is one of the fundamental principles of Object-Oriented Programming (OOP). • It means hiding the internal implementation details and showing only the essential features of an object to the outside world. • Abstraction allows you to focus on what an object does rather than how it does it.
  • 3.
    Introduction to Abstraction Thinkof a TV remote: • You press a button to increase the volume. • You don’t care how the signal is transmitted inside the remote. • You only know what happens when you press the button — the volume increases. That's abstraction: focusing on what it does, not how.
  • 4.
  • 5.
    Abstraction vs Encapsulation •Encapsulation • the process of binding data (variables) and functions (methods) that operate on the data into a single unit (class), and restricting direct access to some of the object's components. Purpose: • To protect the data and ensure it is used only in intended ways. How it works: • Achieved using access specifiers like private, public, and protected. • Getter and setter methods control access.
  • 6.
    Abstraction vs Encapsulation •Abstraction • The process of hiding the complex implementation details and showing only the essential features of the object. • Purpose: • To reduce complexity and allow the programmer to focus on interactions at a higher level. • How it works: • Achieved through abstract classes, interfaces, or pure virtual functions. • Only the function signature is visible to the user, not the internal logic.
  • 7.
    Feature Abstraction Encapsulation FocusHiding implementation details Hiding data and restricting access Goal Reduce complexity Protect data integrity Achieved by Abstract classes, interfaces Classes, access specifiers Visibility Only essential features are shown Internal state is hidden from the outside Example keyword abstract, virtual (C++), interface private, public, protected Abstraction vs Encapsulation
  • 8.
    Abstraction in C++ •InC++, abstraction is achieved using: • Classes and Objects • Access Specifiers (private, protected, public) • Abstract Classes and Pure Virtual Functions
  • 9.
    Using Classes andObjects • Classes allow you to group data (variables) and functions (methods) together, and expose only what is necessary via public methods. The complex logic inside the methods remains hidden from the user. #include <iostream> using namespace std; class Calculator { public: int add(int a, int b) { return a + b; // implementation hidden from user } int multiply(int a, int b) { return a * b; } }; int main() { Calculator calc; cout << "Sum: " << calc.add(5, 3) << endl; cout << "Product: " << calc.multiply(5, 3) << endl; return 0; The user of Calculator class only knows how to use add() and multiply(), not how they work internally.
  • 10.
    • C++ accessspecifiers — private, protected, and public — allow to hide secret data and expose only what’s needed class Account { private: double balance; public: void deposit(double amount); void withdraw(double amount); }; How private members hide details from users of a class user see only interface (deposit/withdraw), not how balance is handled internally.
  • 11.
    Abstract Classes andPure Virtual Functions
  • 12.
    • A virtualfunction that is only declared but not defined (no implementation) in the base class is called a pure virtual function. • Syntax of pure virtual function • A function is made pure virtual by preceding its declaration with the keyword virtual and post-fixing it with zero. • virtual int area() = 0; // pure virtual function • An abstract class in C++ is a class that must contain at least one pure virtual function. • You cannot create an object of an abstract class. Abstract Classes and Pure Virtual Functions
  • 13.
    Why Use PureVirtual Functions? • To define common interface for all derived classes. • To enforce implementation of certain functions in child classes. • Supports runtime polymorphism
  • 14.
    Case study • Imagineyou're hired by a software company to develop an Employee Management System for a large organization. The company has different types of employees: Permanent, Contract, and Intern. Every employee must be able to: • Calculate their salary. • Display their employee details. • However, the way the salary is calculated is different for each type of employee: • Permanent employees have a fixed base salary and bonuses. • Contract employees are paid based on hourly rates. • Interns get a fixed stipend.
  • 15.
    // Abstract BaseClass class Employee { public: string name; int id; Employee(string n, int i) : name(n), id(i) {} // Pure virtual functions virtual void calculateSalary() = 0; virtual void displayInfo() = 0; }; Notable Things in case study: Imagine you're hired by a software company to develop an Employee Management System for a large organization. The company has different types of employees: Permanent, Contract, and Intern. Every employee must be able to: Calculate their salary. Display their employee details. However, the way the salary is calculated & information to be displayed are different for each type of employee
  • 16.
    // Derived classfor Permanent Employee class PermanentEmployee : public Employee { float baseSalary; float bonus; public: PermanentEmployee(string n, int i, float b, float bn) : Employee(n, i), baseSalary(b), bonus(bn) {} void calculateSalary() override { cout << "Total Salary (Permanent): " << baseSalary + bonus; } void displayInfo() override { cout << "Permanent Employee: " << name << " (ID: " << id << ")"; } }; Permanent employees have a fixed base salary and bonuses
  • 17.
    // Derived classfor Contract Employee class ContractEmployee : public Employee { float hourlyRate; int hoursWorked; public: ContractEmployee(string n, int i, float rate, int hours) : Employee(n, i), hourlyRate(rate), hoursWorked(hours) {} void calculateSalary() override { cout << "Total Salary " << hourlyRate * hoursWorked; } void displayInfo() override { cout << "Contract Employee: " << name << " (ID: " << id << ")"; } }; Contract employees are paid based on hourly rates
  • 18.
    // Derived classfor Intern class Intern : public Employee { float stipend; public: Intern(string n, int i, float s) : Employee(n, i), stipend(s) {} void calculateSalary() override { cout << "Total Salary (Intern): " << stipend << endl; } void displayInfo() override { cout << "Intern: " << name << " (ID: " << id << ")" << endl; } }; Interns get a fixed stipend
  • 19.
    // Main Function intmain() { Employee* e1 = new PermanentEmployee("Alice", 101, 50000, 5000); Employee* e2 = new ContractEmployee("Bob", 102, 100, 120); Employee* e3 = new Intern("Charlie", 103, 8000); e1->displayInfo(); e1->calculateSalary(); e2->displayInfo(); e2->calculateSalary(); e3->displayInfo(); e3->calculateSalary(); // Clean up delete e1; delete e2; delete e3; return 0; } Key Takeaways: •The Employee class defines the interface with pure virtual functions- calculateSalary() and displayInfo(),. •Each derived class must implement calculateSalary() and displayInfo(). •You can use a base class pointer to handle multiple employee types using runtime polymorphism.
  • 20.
    Abstract class •An abstractclass is a class that • contain at least one pure virtual function. • cannot be instantiated meaning that object of an abstract class cannot created • Is used to provide a common interface for derived classes. • A concrete class is a class that: • Implements all inherited pure virtual functions (if any), • Can be instantiated (i.e., you can create objects from it), • Is not abstract. Type Can Be Instantiated? Has Pure Virtual Functions? Used For Abstract Class ❌ No ✅ Yes Interface/blueprint Concrete Class ✅ Yes ❌ No (or implements all) Actual objects
  • 21.
    Must all derivedclasses override a pure virtual function? • Yes, if Derived class overrides all pure virtual functions. • Then it becomes a concrete class, and you can create objects of it. • No, if the Derived class does NOT override all pure virtual functions • Then the derived class is still abstract and cannot be instantiated.
  • 22.
    #include <iostream> using namespacestd; class Base { public: virtual void func1() = 0; virtual void func2() = 0; }; class Derived : public Base { public: void func1() override { cout << "func1 implementedn"; } }; int main() { // Derived d; // Error: Derived is still abstract } class Derived : public Base { public: void func1() override { cout << "func1 implementedn"; } void func2() override { cout << "func2 implementedn"; } }; int main() { Derived d; // OK d.func1(); d.func2(); } Class Derived overrides all pure virtual functions. So it is concrete class Class Derived overrides only func1() so it is an absract class