Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Lesson 24: Abstract Classes and Interfac | C++ Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. C++ Course

Lesson 24 of 45

·C++

Lesson 24: Abstract Classes and Interfaces

9 min

Course progress53%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • Pure virtual functions and abstract classes
  • Inheritance hierarchy for polymorphism
  • Multiple inheritance of interfaces
  • Design guidelines for abstraction

Abstract Classes and Interfaces

An abstract class contains at least one pure‑virtual function, making it impossible to instantiate directly. Abstract classes are often used to define interfaces – contracts that derived classes must fulfil.


Example: Printable interface

class Printable {
public:
    virtual void print(std::ostream& os) const = 0;
    virtual ~Printable() = default;
};

class Person : public Printable {
    std::string name;
    int age;
public:
    Person(std::string n, int a) : name(std::move(n)), age(a) {}
    void print(std::ostream& os) const override {
        os << \"Person: \" << name << ", age " << age;
    }
};

int main() {
    Person p(\"Bob\", 30);
    p.print(std::cout);
    std::cout << '\
';
    return 0;
}

If you need multiple unrelated contracts, you can inherit from several abstract classes (multiple inheritance). The C++20 concepts feature can also express interface‑like requirements without inheritance.

Tip: Keep interfaces minimal – only pure virtual functions and a virtual destructor.


<Alert type="info">Abstract classes can contain non‑virtual helper functions and data members, offering a blend of concrete and abstract behaviour.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from C++ CourseBrowse activities
Previous lessonLesson 23: Polymorphism and Virtual FunctionsContinue to lesson 25Lesson 25: Function TemplatesBack to C++ Course curriculum