answersLogoWhite

0


Best Answer

To derive a new class from an existing class, your new class must inherit from the existing class. This is achieved in the declaration of the class:

class oldclass{};
class newclass : public oldclass {};

In the above example, newclass inherits publicly from oldclass. What this means is that the public and protected members of oldclass are public and protected members of newclass. If you specify protected inheritance, the public members of oldclass become protected members of newclass. If you specify private inheritance, the public and protected members of oldclass become private members of newclass. Regardless of the type of inheritance specified at this point, you can still override the access of individually inherited members. In most cases you will use public inheritance unless there is a specific need to alter access rights to the base class members. Note that this does not affect the base class itself; only objects derived from the base class are affected.

Having inherited the base class and all its public and protected members, you can then override the base class behaviour to provide more specific methods and data members for your derivative. However, note that only virtual methods are expected to be overridden. If you override a non-virtual method, you will effectively hide the base class method along with any and all overloads of that method. Thus it is important to ensure the base class has a virtual destructor. If it doesn't, this will only lead to problems when your derived class falls from scope because derived classes must be destroyed in sequence, from the most-derived to the least-derived. This can only be achieved when all destructors other than the most-derived destructor are declared virtual.

It should also be noted that there's no point in having a base class with no virtual methods other than the destructor. The only reason for having a virtual destructor in the first place is because the base class has one or more virtual methods.

Abstract base classes are similar to normal classes except they also contain one or more pure-virtual methods. This means that you must provide an implementation for all the pure-virtual methods inherited from the base class (with normal virtual methods, overriding is optional) otherwise your derivative becomes an abstract base class itself. However, derived classes can inherit implementations of pure-virtual methods from other intermediate base classes, other than the one that initially declared the method pure-virtual, which itself may or may not provide an implementation.

It's often the case that, where an implementation is provided by a base class virtual method, you are expected to call that method from your derived class override. However some implementations may be expected to be completely overridden. The documentation for the base class will determine if and when to call the base class implementation.

In some cases, you may wish to derive a class from two or more base classes, known as multiple inheritance. To do so, you simply provide a comma-separated list, like so:

class base1{};
class base2{};
class derived : public base1, public base2 {};

When you combine multiple inheritance with multi-level inheritance, be aware that this may result in two or more instances of a common base class:

class base {};
class inter1 : public base {};
class inter2 : public base {};
class derived : public inter1, inter2 {};

This creates an ambiguity because derived inherits two separate instances of base, one from inter1 and the other from inter2. There are various ways to remove the ambiguity, however the preferred method is to make base a virtual base class, like so:


class base {};
class inter1 : virtual public base {};
class inter2 : public virtual base {};
class derived : public inter1, inter2 {};

Note that the virtual keyword can be placed before or after the inheritance specifier, it makes no difference. Now derived will inherit directly from base, and that one instance is shared with both inter1 and inter2. There may still be ambiguity where both inter1 and inter2 override the same virtual method of base, but this can be easily dealt with by providing a more specific override in the derived class.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the process of creating a new derived class from a base class?
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.


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


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

Related questions

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.


Show program called stack which has two classes i.e. base class and derived class?

#include class base{public:base() { printf("Creating %s\n", get_class() ); }virtual ~base() { printf("Destroying %s\n", get_class() ); }virtual char * get_class() const { return("base"); }};class derived : public base{public:derived() { printf("Creating %s\n", get_class() ); }~derived() { printf("Destroying %s\n", get_class() ); }char * get_class() const { return("derived"); }};int main(){printf( "Program name: Stack\n" );printf( "\nBase object lifecycle:\n" );base* b = new base;delete( b );printf( "\nDerived object lifecycle:\n" );derived* d = new derived;delete( d );return( 0 );}


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


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 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!


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.


What is the Practical application of a derived class object stored in base class pointer?

If you have base class derived object pointing by base class pointer, then you have the power of run time polymorphism in your hand, which gives you the ability to call the derived class implementation of the virtual member function. If we declare the member function as virtual in base class which needs to overridden in derived class, then you can decide at run time which implementation will be called at run time.