answersLogoWhite

0


Best Answer

There is no difference other than that a derived class inherits from a base class. That is, the derived class inherits all the public and protected members of its base class, and is a more specialised form of its base class. Thus both can be treated as if they really were base classes. The derived class can also override the virtual methods of the base class, thus allowing polymorphic behaviour without the need to know the exact type of the derived class.

User Avatar

Wiki User

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

Wiki User

10y ago

The definition of a base class is any class that declares one or more virtual functions. If any function is declared virtual, the destructor must also be declared virtual.

The only other type of base class is an abstract base class (ABC), also known as an abstract data type (ADT). An ADT must provide one or more pure virtual functions in addition to a virtual destructor. A virtual interface is optional.

The main differences between a virtual function and a pure virtual function is that a pure virtual function must be overridden by a derivative but the ADT that declared it need not provide any implementation. By contrast, a virtual function need not be overridden by a derivative but must be implemented by the base class that declared it.

When a derivative implements a pure-virtual function, that function becomes virtual (non-pure) with regards subsequent derivatives, and those derivatives can therefore inherit that implementation. However, if a derivative does not implement or inherit a complete implementation of a pure-virtual interface, it becomes an ADT itself.

Some people regard virtual base classes as being a type of base class, but this is not strictly true. Virtual base classes simply alter the order of construction, nothing more. In the hierarchy A->B->C where C is the most-derived class, when you instantiate an instance of C the compiler will automatically invoke B's constructor which, in turn, will automatically invokes A's constructor. Only when A is fully constructed can B be fully constructed and only when B is fully constructed can C be fully constructed. But if B inherits from A virtually rather than directly, then A is constructed via C, not via B. Thus when C invokes B's constructor, A is already constructed. Virtual base classes are always constructed first by the most-derived class in the hierarchy (thus if D derived from C, D would invoke A's constructor rather than B). This is useful when two or more base classes share a common base class as the most-derived class simply instantiates a single instance of the virtual base class that is subsequently shared by those base classes. Without virtual base classes, you would have two or more instances of the same base class which not only increases the memory footprint, it also leads to ambiguity regarding which instance is being referred to unless you use explicit member-of references.

In addition, classes that do not declare or inherit a virtual interface can still be used as base classes, however they are not base classes in the strictest sense. At the very least, a base class must have a virtual destructor to ensure correct teardown (most-derived to least-derived), regardless of which class is destroyed first. But without a virtual interface the onus is placed entirely upon the programmer rather than the compiler to ensure the correct polymorphic behaviour is invoked. Virtual interfaces cost a little memory to accommodate the virtual table (v-table), but without this the cost would be even greater because the programmer must use costly runtime type information to enable polymorphic behaviour. This, in turn, increases the maintenance burden upon the programmer. If the class is to be distributed, this would be an unacceptable burden to place upon your consumers.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

As many as required. There is no practical limit.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between base class and derived class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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 is the difference between the direct and indirect base class?

Direct base classes are those that are specified in a derived class' inheritance declaration. Indirect base classes are those that are inherited via the direct base classes. class A{}; class B : public A{}; class C : public B{}; With respect to C, B is direct while A is indirect. With respect to B, A is direct.


Difference between base class and derived class?

The derived class derives, or inherits, from the base class. The derived class is like a "child", and the base class, the "parent". The child class has the attributes of the parent class - its variables (those defined at the class level) and methods. Changes done to the parent class (the base class) will affect the child class (the derived 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.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.

Related questions

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.


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 is the difference between the direct and indirect base class?

Direct base classes are those that are specified in a derived class' inheritance declaration. Indirect base classes are those that are inherited via the direct base classes. class A{}; class B : public A{}; class C : public B{}; With respect to C, B is direct while A is indirect. With respect to B, A is direct.


Difference between base class and derived class?

The derived class derives, or inherits, from the base class. The derived class is like a "child", and the base class, the "parent". The child class has the attributes of the parent class - its variables (those defined at the class level) and methods. Changes done to the parent class (the base class) will affect the child class (the derived 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.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.


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).


How do you call base class method using derived class object?

If the base class method is non-private, the derived class can call the base class method implicitly. However, if the derived class overrides or overloads the method, the base class method must be called explicitly. The following demonstrates explicit calls to base class methods: #include <iostream> using namespace std; class Base { public: Base(){} void Foo(){ cout << "Base::Foo" << endl; } }; class Derived : public Base { public: Derived(){} void Foo(){ cout << "Derived::Foo" << endl; Base::Foo(); } }; int main() { Derived derived; derived.Foo(); derived.Base::Foo(); // Explicit call to base class method. return(0); } Output: Derived::Foo Base::Foo Base::Foo


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.


Can a derived class make a public base function private true or false?

True. A derived class can make a public base function private. The derived function is private, within the derived class, but public in other contexts.


Which base class member functions are not inherited by a derived class?

Derived classes only inherit the protected and public members of their base classes. Private member functions cannot be inherited by a derived class.


When the concept of method overriding is necessary?

concept of overriding is very important as due to overriding the derived class can use the function of the base class! when the function has same name and prototype in both the classes(base and derived) then the derived class can use the funtion of base class!