C++ Abstraction
In this page:
class AbstractClass {
public:
virtual return_type method_name() = 0;
};
class Concrete : public AbstractClass {
public:
return_type method_name() override { }
};
What is Abstraction?
Abstraction is the process of hiding an object's complex internal implementation while exposing only the essential features a user actually needs to interact with it.
This lets programmers work with high-level concepts without being distracted by low-level mechanics they don't need to know.
Example: What is Abstraction?
// Include std::cout and std::cin
#include <iostream>
// Define the type Car
class Car {
// Members below can be accessed from outside the class
public:
void start() {
// Print to the console with cout
std::cout << "Car started" << std::endl;
}
};
// Program execution starts in main()
int main() {
Car car;
car.start();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Abstract Classes
An abstract class is a class specifically designed to serve as a blueprint for other classes: it contains at least one pure virtual function and, as a direct consequence, can never be instantiated on its own.
Example: Abstract Classes
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
virtual void draw() = 0;
};
// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Circle" << std::endl; }
};
// Program execution starts in main()
int main() {
Circle c;
c.draw();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pure Virtual Functions
A pure virtual function is declared in a base class with no implementation of its own — its body is replaced entirely by setting the declaration equal to 0 — leaving that specific piece of behavior entirely up to whatever subclass eventually implements it.
Example: Pure Virtual Functions
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
virtual void draw() = 0;
};
// Define the type Square
class Square : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Square" << std::endl; }
};
// Program execution starts in main()
int main() {
Square s;
s.draw();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Implementation of Abstraction
To actually put abstraction into practice, every concrete class that derives from an abstract parent must override and provide a real implementation for each pure virtual function it inherited, or else it remains abstract itself and can't be instantiated either.
Example: Implementation of Abstraction
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
virtual void draw() = 0;
};
// Define the type Triangle
class Triangle : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Triangle" << std::endl; }
};
// Program execution starts in main()
int main() {
Triangle t;
t.draw();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Advantages of Abstraction
Abstraction pays off by reducing code duplication and cleanly separating a system's design from its implementation details — a benefit that keeps large, evolving codebases scalable rather than turning into a tangle of copy-pasted logic.
Example: Advantages of Abstraction
// Include std::cout and std::cin
#include <iostream>
// Define the type PaymentMethod
class PaymentMethod {
// Members below can be accessed from outside the class
public:
virtual void pay() = 0;
};
// Define the type Cash
class Cash : public PaymentMethod {
// Members below can be accessed from outside the class
public:
void pay() override { std::cout << "Paid with cash" << std::endl; }
};
// Program execution starts in main()
int main() {
Cash cash;
cash.pay();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Confusing abstraction (hiding what a thing does) with encapsulation (bundling data and methods).
- Exposing implementation details in the public interface.
- Not implementing every pure virtual function in a concrete derived class.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: