bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/C++/C++ Classes
C++•C++ Classes

C++ Inheritance

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind C++ Inheritance?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// Base ___ class Vehicle { public: string brand = "Ford"; void honk() { cout << "Tuut, tuut! \n" ;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

- derived class (child) - the class that inherits from another class - base class (parent) - the class being inherited from
We group the "inheritance concept" into two categories:
Inheritance allows one class to reuse attributes and methods from another class.

Inheritance

Inheritance allows one class to reuse attributes and methods from another class. It helps you write cleaner, more efficient code by avoiding duplication.

We group the "inheritance concept" into two categories:

  • derived class (child) - the class that inherits from another class
  • base class (parent) - the class being inherited from

To inherit from a class, use the : symbol.

In the example below, the Car class (child) inherits the attributes and methods from the Vehicle class (parent):

Example

// Base class class Vehicle { public: string brand = "Ford"; void honk() { cout << "Tuut, tuut! \n" ;
}
};
// Derived class class Car: public Vehicle { public: string model = "Mustang";
};
int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model;
  return 0;
}
  • It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.

Previous

C++ The Friend Keyword

Next

C++ Multilevel Inheritance