← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 8 of 11

C++ Abstraction

Abstraction means showing what something does and hiding how, like using a TV remote without knowing the electronics. It makes code simpler to use.
Syntax
cpp
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?

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing abstraction (hiding what a thing does) with encapsulation (bundling data and methods).
  2. Exposing implementation details in the public interface.
  3. Not implementing every pure virtual function in a concrete derived class.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.