answersLogoWhite

0


Best Answer

The only real disadvantage to inheritance is that additional memory must be set aside for the derived class virtual table. This is essentially a table of function pointers where each entry refers to the most-derived override for each virtual function. In order to use inheritance, the least-derived base class (or classes) must declare a virtual destructor. All derived class destructors are then implicitly virtual. Any class within the hierarchy can (optionally) declare one or more virtual functions which any derivative can override with its own implementation. If any class declares a pure-virtual method, that class is automatically an abstract base class and must be overridden by a more derived class. Only classes that provide or inherit a complete implementation can be instantiated. The base class that declares a pure-virtual method can (optionally) provide an implementation but that implementation cannot be inherited, it can only be called, explicitly, by a derived class override. When we instantiate objects of a derived class, there is only one virtual table regardless of the number of objects instantiated, thus the cost is minimal (dependant upon the actual number of most-derived overrides). When referring to base classes, the same table is used, but only the portion that is visible to the base class is actually accessible to it. That is, it cannot invoke overrides for virtual functions that were declared by any of its derivatives, it can only invoke those it declared itself or that were inherited from its base classes. However, the pointers for those it knows about will always point to the most-derived override, as determined by the most-derived class in the hierarchy.

Since virtual tables are only required to enable runtime polymorphic behaviour, it would be disadvantageous to use inheritance upon a class that was not intended to be used polymorphically. That is, where you wished to create a derived class but did not wish others to derive from your class. The simplest method of doing so would be to use composition rather than inheritance, by embedding the base class instead of inheriting from it. The virtual table for the base class would still exist, of course, but would not be associated with your derived class. The derived class could declare its own virtual methods (including a virtual destructor) but that would defeat the purpose of using composition, and, in turn, would generate a second virtual table specific to your composite. However, in some cases this may be desirable because embedded objects can greatly simplify the composite interface (through delegation rather than overrides) whilst allowing your composite class to be used polymorphically. Ultimately it all comes down to what it is you are trying to achieve and what implementation details you are trying to hide.

User Avatar

Wiki User

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

Wiki User

8y ago

C is not an object-oriented language and therefore does not natively support the notion of inheritance. It is possible to model inheritance in C, but without the means of encapsulating objects it is just as error-prone as "traditional" C and simply not worth the additional effort and complexity. It's far easier to use a language that is specifically intended for the purpose. In this case, C++ would be a good choice because virtually everything you can do in C you can also do in C++ (and in many cases do it better), to the extent that you rarely need to use anything lower than C++. Plus you gain all the advantages C++ has to offer, including native support for object-oriented programming paradigms.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Disadvantage of inheritance in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


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.


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.


What is an inheritance Explain different types of inheritance in c?

C is not an object oriented language and therefore has no native support for inheritance.


Drawbacks of multiple inheritance in c plus plus?

There are no drawbacks to multiple inheritance if multiple inheritance is precisely what is required to achieve your goal. If there are any drawbacks then it is only because of poor design, not multiple inheritance itself. For instance, when designing classes to simulate vehicles, an amphibious vehicle would inherit the properties of both an off-road vehicle and a marine vehicle, therefore multiple inheritance would be an appropriate usage.

Related questions

Does c plus plus supports hierarchical inheritance?

Yes.


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.


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.


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 { // ... };


Javas major disadvantage over c and c plus plus?

In C it's easier to work with hardware directly. Also C programs are usually more efficient.


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.


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.


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.


Why not java allowed multiple inheritance and c plus plus allowed?

Because that's the way Java is designed. The designers felt that multiple inheritance was an unnecessary complication that offered little value so they did not include it in the JBC/JVM.


What is an inheritance Explain different types of inheritance in c?

C is not an object oriented language and therefore has no native support for inheritance.


What are the various concepts of c plus plus?

1.Classes and Objects 2.Constructors and Destructors 3.Inheritance 4.Polymorphism 5.Dynamic Binding