answersLogoWhite

0


Best Answer

As many as required. The only practical limits are those imposed by the hardware. For instance, 32-bit systems can only address a maximum of 4GB, but only 2GB is actually available to applications. On 64-bit systems there is no practical limit other than hard-drive space (for the virtual memory paging file).

Remember that every class of object requires memory to store its member variables (plus padding for alignment), which has to be multiplied up by the number of instances of those classes. Thus the more complex the hierarchy, the fewer instances you can create overall. Since alignment padding can add a substantial overhead, it's best to declare member variables from largest to smallest within each class because memory is allocated in the same order the members are declared. If a v-table is required (which it typically will be in a multi-level hierarchy) this will consume additional memory: essentially one function pointer per virtual function per override.

That said, it is difficult to imagine any hierarchy so large that you will hit a memory limitation, even on a 32-bit system, unless you happen to embed a particularly large member variable in your class, such as a hi-resolution image or video, rather than use a disk-based file stream. Aside from that the main concern is in how many instances of that hierarchy can you physically construct at one time. However, you need only look at some of the hierarchies within the Microsoft Foundation Classes to realise that your probably just scratching the surface of what is actually possible.

User Avatar

Wiki User

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

Wiki User

10y ago

As many as desired. The only practical limit is the available memory on the system.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

One each. One base class and One derived class.

Ex:

public class A extends B {

...

..

..

}

Here A is the derived class and B is the base class

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Multiple inheritance.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How many base class and derived class can be created in inheritance?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is single inheritance in c plus plus?

Multiple inheritance occurs when a class is derived directly from two or more base classes. class b1 {}; class b2 {}; class d: public b1, public b2 {}; // multiple inheritance class


What is multilevel inheritance in C plus plus?

Multi-level inheritance involves at least 3 classes, a, b and c, such that a is derived from b, and b is derived from c. c is therefore the least-derived base class, b is an intermediate base class and a is the most-derived class.


Does the derived class take the memory of base class in inheritance?

Yes, the derived class includes the memory of the base class. The derived class inherits everything in the base class, data and function, and provides additional data and function as needed.


What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.


How you can override base class member in derived classexplain with example?

To override a base class method you simply need to declare the base class method as being virtual. As well as creating a v-table, this also gives a visual hint to other developers that you expect the function to be overridden. The v-table ensures that all calls to the base class method are routed to the derived class method, thus ensuring objects behave polymorphically, according to their actual type, and not what we're actually pointing at. Consider the following example: #include <iostream> class base { public: virtual ~base(); virtual void PrintMe() const { std::cout << "I am a base class!" << std::endl; } }; class derived: public base { public: void PrintMe() const { std::cout << "I am a derived class!" << std::endl; } }; int main() { base b; derived d; base* pb = &d; b.PrintMe(); d.PrintMe(); pb->PrintMe(); return( 0 ); } Output: I am a base class! I am a derived class! I am a derived class! Note that although pb points to the base class instance of d, it still knows that it really is a derived class, as can be seen from the third line of output. Now try removing the virtual keyword from the base class method. The output will change as follows: Output: I am a base class! I am a derived class! I am a base class! Now your derived class thinks it is a base class. This is because the v-table no longer has no entry for that method, and therefore the call cannot be routed to the overridden derived class method. The base class method is called because that's what we're actually pointing at and the object no longer behaves polymorphically according to its actual type. Note also that if any method is declared virtual in a class, the class constructor must also be declared virtual. If you fail to do this, your classes will not be destroyed properly. The virtual keyword ensures that the most-derived class is always destroyed first, before working up the hierarchy of destructors to eventually destroy the least-derived class, the base class itself. Consider the following example without a virtual destructor: #include <iostream> class base { public: base(){ std::cout << "Base class created" << std::endl; } ~base(){ std::cout << "Base class destroyed" << std::endl; } }; class derived: public base { public: derived(){ std::cout << "Derived class created" << std::endl; } ~derived(){ std::cout << "Derived class destroyed" << std::endl; } }; int main() { derived* d = new derived(); base* b = d; delete( b ); return( 0 ); } Output: Base class created Derived class created Base class destroyed As you can see, the derived class was created but was not destroyed. We've created a memory leak: that memory cannot be recovered until the program ends. Now add the virtual keyword to the base class destructor: #include <iostream> class base { public: base(){ std::cout << "Base class created" << std::endl; } virtual ~base(){ std::cout << "Base class destroyed" << std::endl; } }; class derived: public base { public: derived(){ std::cout << "Derived class created" << std::endl; } ~derived(){ std::cout << "Derived class destroyed" << std::endl; } }; int main() { derived* d = new derived(); base* b = d; delete( b ); return( 0 ); } Output: Base class created Derived class created Derived class destroyed Base class destroyed Now we have the expected behaviour and have resolved the memory leak. Remember, if ANY method of a class is declared virtual, the destructor must also be declared virtual. Note that although derived classes need not use the virtual keyword in front of overrides (it is implied by the base class), there is no harm in explicitly declaring them as such, if only to give a visual hint that the methods are expected to be overridden by derivatives of the derivative (multi-level inheritance).

Related questions

What is single inheritance in c plus plus?

Multiple inheritance occurs when a class is derived directly from two or more base classes. class b1 {}; class b2 {}; class d: public b1, public b2 {}; // multiple inheritance class


What is multilevel inheritance in C plus plus?

Multi-level inheritance involves at least 3 classes, a, b and c, such that a is derived from b, and b is derived from c. c is therefore the least-derived base class, b is an intermediate base class and a is the most-derived class.


What are Public and Private inheritance in c plus plus?

Public, protected and private inheritance determine how the public and protected base class members are inherited by the derived class. Private members are never inherited and are therefore unaffected by the type of inheritance (they remain private to the base class). The following table summarises how inheritance affects accessibility of base class members with respect to the derived class: public inheritanceprotected inheritanceprivate inheritancepublic member of base classpublic member of derived classprotected member of derived classprivate member of derived classprotected member of base classprotected member of derived classprotected member of derived classprivate member of derived classprivate member of base classprivate member of base classprivate member of base classprivate member of base class Note that accessibility to individual public and protected base class members can be overridden within the derived class, regardless of the type of inheritance specified.


Does the derived class take the memory of base class in inheritance?

Yes, the derived class includes the memory of the base class. The derived class inherits everything in the base class, data and function, and provides additional data and function as needed.


Type of inheritances in c plus plus?

Single, multiple, multi-level, hierarchical and hybrid/virtual inheritance. Single inheritance applies when one class inherits from just one base class. Multiple inheritance applies when one class inherits from two or more base classes. Multi-level inheritance applies to a class that inherits from at least one base class that is itself derived from another base class. Hierarchical inheritance applies to a base class that is inherited by two or more separate derived classes. Hybrid inheritance combines multiple inheritance, multi-level inheritance and hierarchical inheritance. That is, where A is a common base class of derived classes B and C, and B and C are both base classes of derived class D. Hybrid inheritance is often used with virtual inheritance where B and C inherit from A virtually rather than directly. In these cases, the virtual base class is instantiated by the most-derived class in the hierarchy, D, and this instance is then shared by both B and C.


What is public derivation in object-oriented programming?

Public derivation or public inheritance means that all the public members of the base calls are declared public in the derived class while the protected members remain protected. Protected inheritance means all the public members of the base class are declared protected in the derived class, as are the protected members. Private inheritance means all the public and protected members of the base class are declared private in the derived class. Private members of the base class are never inherited and are therefore unaffected by inheritance. Note that regardless of the type of inheritance specified, individual non-private members of the base class can be inherited with public or protected access as required of the derived class. The type of inheritance can be therefore be thought of as being the default inheritance for all base class members which can (optionally) be overridden for specific members where required.


What is the Difference between multilevel and multiple inheritance?

Multiple Inheritance : we can inherit more than one class in the same class. Multi-Level Inheritance: where one class can inherit only one base class and the derived class can become base class of some other class.


Write a c plus plus programme to illustrate single inheritance?

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


What is meant by inheritance in c plus plus language?

Inheritance in C++ and in other Object Oriented languages is the creation of a class that incorporates a different class. The child (or derived) class "inherits" all of the elements (attributes and methods) of the parent (or base) class. Depending on the design of the base class, the derived class can use methods and attributes of the base class as if they were its own. Typically, however, attributes of the base class are private to the base class and inaccessible to the derived class so as to maintain class hierarchy and data encapsulation.


What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.


What is hierarchal inheritance?

Hierarchical inheritance is a type of inheritance in object-oriented programming where classes are organized in a hierarchical structure. It means that a derived class can inherit attributes and methods from a base or parent class, and it can further be inherited by other classes. This allows for code reuse and promotes modularity in the program.


Difference bitween various visibility modes used c plus plus?

By visibility I assume you mean member accessibility. C++ uses three levels of accessibility: private, protected and public. Private members are only accessible to the class itself and friends of the class. Protected members are the same as private members except derived classes also have access. Public members are fully accessible. With regards inheritance, base class members with greater access than that specified are reduced to the specified access in the derived class. Thus public inheritance has no effect on base class member access. Protected inheritance reduces public members of the base class to protected members of the derived class. Private inheritance reduces both public and protected members of the base class to private members of the derived class. Private members of the base class are never inherited, thus they always remain private to the base class. Note that access to base class members can never be increased through inheritance, only reduced or kept the same. However, as well as defining an overall inheritance access, you can also specify member-wise inheritance access. Thus you could use public inheritance overall, but specify certain public members of the base class to be protected or private in the derived class and/or certain protected members of the base class to be private members of the derived class.