🟣 OOP  ·  Lesson 45

Inheritance in C++

Inheritance in C++

What is Inheritance in C++?

Inheritance in C++
Inheritance allows a class to acquire properties and functions of another class. It supports code reuse and hierarchical design.
Level
🟣 Object-Oriented Programming
Example File
inheritance.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
base classBase class used in Inheritance in C++ programming.
derived classDerived class used in Inheritance in C++ programming.
is-a relationshipIs-a relationship used in Inheritance in C++ programming.
reuseReuse used in Inheritance in C++ programming.

Syntax / Pattern

class Derived : access Base { };

Example Program

#include <iostream>
using namespace std;
class Person{ public: void show(){ cout << "Person "; } };
class Student : public Person{ public: void study(){ cout << "Student"; } };
int main(){ Student s; s.show(); s.study(); }

Expected Output

Person Student

Program Explanation

  • Student inherits show() from Person.
  • study() belongs to Student.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • person-student model
  • employee-manager model
  • game characters

Common Mistakes

  • Using inheritance when composition is better.
  • Forgetting access mode in inheritance.

Practice Tasks

  1. Create Vehicle and Car classes.
  2. Explain public inheritance.

Summary

Inheritance in C++ is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

What is Inheritance in C++?

Inheritance in C++
Inheritance allows a class to acquire properties and functions of another class. It supports code reuse and hierarchical design.
Level
🟣 Object-Oriented Programming
Example File
inheritance.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
base classBase class used in Inheritance in C++ programming.
derived classDerived class used in Inheritance in C++ programming.
is-a relationshipIs-a relationship used in Inheritance in C++ programming.
reuseReuse used in Inheritance in C++ programming.

Syntax / Pattern

class Derived : access Base { };

Example Program

#include <iostream>
using namespace std;
class Person{ public: void show(){ cout << "Person "; } };
class Student : public Person{ public: void study(){ cout << "Student"; } };
int main(){ Student s; s.show(); s.study(); }

Expected Output

Person Student

Program Explanation

  • Student inherits show() from Person.
  • study() belongs to Student.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • person-student model
  • employee-manager model
  • game characters

Common Mistakes

  • Using inheritance when composition is better.
  • Forgetting access mode in inheritance.

Practice Tasks

  1. Create Vehicle and Car classes.
  2. Explain public inheritance.

Summary

Inheritance in C++ is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

← Back to C++ Tutorial