C++ Encapsulation and Abstraction | Data Hiding, Abstract Classes, Pure Virtual Functions


This complete tutorial on C++ Encapsulation and Abstraction explains how to design secure and modular classes. It covers data hiding, abstraction, abstract classes, and interfaces using pure virtual functions. Following best coding practices, it helps learners build robust object-oriented programs that are easier to maintain and extend.

Encapsulation and Abstraction – Complete Tutorial

1. What is Encapsulation?

Encapsulation is the mechanism of binding data (variables) and methods (functions) together within a class and restricting direct access from outside the class.

It provides data hiding and controlled access to class members.

Example:


#include <iostream>
using namespace std;

class BankAccount {
private:
double balance; // hidden

public:
void setBalance(double b) {
if (b >= 0) balance = b;
}
double getBalance() { return balance; }
};

int main() {
BankAccount account;
account.setBalance(1000);
cout << account.getBalance();
}

Key Points:

  1. Data members declared private
  2. Accessed through public getter/setter methods
  3. Protects data integrity

2. Data Hiding

Data hiding ensures that sensitive information is not accessible directly from outside the class.

  1. Achieved using private or protected access specifiers
  2. Provides controlled access via public functions

Example:


class Employee {
private:
int salary;

public:
void setSalary(int s) { salary = s; }
int getSalary() { return salary; }
};

3. What is Abstraction?

Abstraction focuses on what an object does, not how it does it.

It hides internal implementation details and exposes only necessary interfaces.

  1. Achieved using abstract classes and pure virtual functions
  2. Simplifies complex systems

4. Abstract Classes

An abstract class cannot be instantiated directly and is meant to be inherited by derived classes.

  1. Contains at least one pure virtual function
  2. Used to define a common interface

Syntax of Pure Virtual Function:


virtual void functionName() = 0;

Example:


class Shape {
public:
virtual void draw() = 0; // pure virtual
};

class Circle : public Shape {
public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};

int main() {
Circle c;
c.draw();
}

5. Interfaces using Pure Virtual Functions

A class with only pure virtual functions acts as an interface.

Derived classes must implement all pure virtual functions.

Example:


class Printable {
public:
virtual void print() = 0;
};

class Document : public Printable {
public:
void print() override {
cout << "Printing Document" << endl;
}
};
  1. Document implements the interface
  2. Ensures a contract for derived classes

Best Practices

  1. Always keep sensitive data private
  2. Provide getter/setter functions for controlled access
  3. Use abstract classes for common interfaces
  4. Prefer interfaces for reusable design
  5. Use override keyword for implementing pure virtual functions

Common Mistakes

  1. Making all members public (violates encapsulation)
  2. Forgetting to implement pure virtual functions in derived class
  3. Instantiating abstract classes directly
  4. Mixing abstraction and implementation carelessly

Summary

In this chapter, you learned about Encapsulation and Abstraction in C++, including:

  1. Data hiding and controlled access
  2. Abstract classes and pure virtual functions
  3. Using interfaces to enforce a contract for derived classes

Encapsulation and abstraction are fundamental for designing secure, modular, and maintainable object-oriented programs.