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

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. C++ Course

Lesson 17 of 45 · C++

Lesson 24: Abstract Classes and Interfaces

Duration: 9 min

Abstract Classes and Interfaces\n\nAn 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.\n\n---\n\n## Example: Printable interface\ncpp\nclass Printable {\npublic:\n virtual void print(std::ostream& os) const = 0;\n virtual ~Printable() = default;\n};\n\nclass Person : public Printable {\n std::string name;\n int age;\npublic:\n Person(std::string n, int a) : name(std::move(n)), age(a) {}\n void print(std::ostream& os) const override {\n os << \"Person: \" << name << ", age " << age;\n }\n};\n\nint main() {\n Person p(\"Bob\", 30);\n p.print(std::cout);\n std::cout << '\\n';\n return 0;\n}\n\n\nIf 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.\n\n> Tip: Keep interfaces minimal – only pure virtual functions and a virtual destructor.\n\n---\n\n<Alert type="info">Abstract classes can contain non‑virtual helper functions and data members, offering a blend of concrete and abstract behaviour.

Previous: Lesson 23: Polymorphism and Virtual FunctionsNext: Lesson 25: Function Templates