answersLogoWhite

0

You use inheritance whenever you need a more specialised version of an existing class (the base class). Rather than creating a new class entirely from scratch, and therefore duplicating tried and tested code, you simply build upon the existing class, overriding the existing methods and adding more specialised methods, whilst retaining all the generic functionality of the base class.

User Avatar

Wiki User

11y ago

Still curious? Ask our experts.

Chat with our AI personalities

ReneRene
Change my mind. I dare you.
Chat with Rene
RossRoss
Every question is just a happy little opportunity.
Chat with Ross
ProfessorProfessor
I will give you the most educated answer.
Chat with Professor
More answers

Inheritance in C++ allows us to derive new classes from existing classes. The new class (the derived class) inherits all the public and protected properties of the existing class (the base class), known as the generic interface. The derived class can then modify this interface, enhancing it to produce a more specialised interface.

Base classes cannot possibly know what derivatives might exist in the future thus if we call a method in the base class we would rightly expect the base class method to execute. However, if we declare the method virtual, the most-derived override of that method is executed instead, even when the runtime type of that derivative cannot possibly be known in advance. This is extremely useful because it means that existing code doesn't have to be updated every time we create a new derivative, we can simply use the generic, virtual interface of the base class (the only class the code need know about) and the most-derived override for each method will be invoked for us, completely automatically. In other words, inheritance and virtual functions allow runtime polymorphic behaviour.

User Avatar

Wiki User

10y ago
User Avatar

Without inheritance we wouldn't be able to derive new classes from existing classes. This would make it much more difficult to deal with collections of objects of different types; we could only work with objects of the same type. With inheritance, we can derive a variety of different classes from a common base class, and thus create a collection which refers to the common base of those objects. The objects themselves behave according to their runtime time (the actual type) but from the collection's point of view, they are all of the same type.

User Avatar

Wiki User

7y ago
User Avatar

Inheritance is the process by which the properties of base class is derived by derived class or it's sub classes.

User Avatar

Wiki User

13y ago
User Avatar

Add your answer:

Earn +20 pts
Q: When to use inheritance in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp