answersLogoWhite

0


Best Answer

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

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

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

This answer is:
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
Related questions

How ploymorphism and inheritance is different from that in Java and c plus plus?

C++ allows multiple inheritance while Java does not. In my opinion, multiple inheritance is not useful because it can get very confusing very quick. For polymorphism, C++ does early binding by default, while Java does late binding by default. Late binding is more useful than early binding.


Does c plus plus supports hierarchical inheritance?

Yes.


Write a c plus plus programme to illustrate single inheritance?

struct A {}; // base class struct B : A {} // derived class (single inheritance).


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


Why is multiple inheritance not possible in C?

C is not object-oriented -- you can't even use single inheritance let alone multiple inheritance.


How do you implement inheritance in c plus plus?

You implement inheritance by deriving a new class of object from an existing class of object. The existing class is known as the base class of the derived class.Classes declared final cannot be used as bases classes and classes without a virtual destructor (or a virtual destructor override) cannot be used as polymorphic base classes.


Demonstrate single inheritance in C plus plus?

struct base1 { // ... }; struct base2 { // ... }; struct derived1 : public base1 // single inheritance { // ... }; struct derived2 : public base1, public base2 // multiple inheritance { // ... };


In C plus plus How would you use Inheritance to create an Orange?

One way would be to define a base class called fruit, from which you could derive specific types of fruit, including an orange.


How does c plus plus endeavor to represent the object oriented paradigm?

C++ endeavours to represent the object oriented programming paradigm through the use of classes. The four main pillars of OOP are encapsulation, inheritance, polymorphism and abstraction, which C++ primarily achieves through the use of classes, class hierarchies, virtual methods and templates.


How the turbo c plus plus use in the computer?

How the turbo c plus plus use what in the computer.


What are the concepts of object oriented programming in c plus plus?

The concepts of OOP in C++ are the same as for OOP in any other programming language: abstraction, encapsulation, inheritance and polymorphism.


What are the main features of OOP in c plus plus?

The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.