C++ derived and base Class

C++ Base Class

Inheritance is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with new capabilities. Software reusability saves time during program development. It also encourages the reuse of proven and debugged high-quality software, which increases the likelihood that a system will be implemented effectively.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. A derived class represents a more specialized group of objects. Typically, a derived class contains behaviors inherited from its base class plus additional behaviors. Where a direct base class is a base from which a derived class explicitly inherits. An indirect base class is inherited from two or more levels up the class hierarchy. In the case of single inheritance, a class is derived from one base class.C++ also supports multiple inheritances, in which a derived class inherits from multiple bases classes. The derived class object “is an” object of its base class and one base class can have many derived classes, the set of objects represented by a base class typically is larger than the set of objects represented by any of its derived classes. For Example, the base class Vehicle represents all vehicles, including cars, trucks, boats, bicycles and so on. By contrast, derived class Car represents a smaller, more-specific subset of all vehicles.

  • A base class exists in a hierarchical relationship with its derived classes.
  • Although classes can exist independently, once they are employed in inheritance relationships, they become affiliated with other classes
  • A class becomes either a base class, supplying data and behaviors to other classes, or a derived class, inheriting its data and behaviors from other classes

C++ Derived Class

A class that inherits the members of another class is called Derived Class. A derived class member functions might require access to base-class data members and member functions. A derived class can access the non-private members of its base class. Base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. A derived class can effect state changes in private base-class members, but only through non-private member functions provided in the base class and inherited into the derived class.

Member functions of a derived class cannot directly access private members of their class’s base class. If a derived class could access its base class’s private members, classes that inherit from that derived class could access that data as well. This would propagate access to what should be private data and the benefits of information hiding would be lost.

One problem with inheritance is that a derived class can inherit data members and member functions it does not need or should not have. It is the class designer’s responsibility to ensure that the capabilities provided by a class are appropriate for future derived classes. Even when a base-class member function is appropriate for a derived class, the derived class often requires that member function behaves in a manner specific to the derived class. In such cases, the base-class member function can be redefined in the derived class with an appropriate implementation

Examples for C++ Derived Class and Base Class

Base and Derived Class in C++
Base and Derived Class in C++

Definition of Data abstraction?

General characteristics and abilities can be handled by generic (base) classes and specializations can be organized in hierarchical relationships by means of derived classes. It makes easier to manage complex situations and relationships.

Definition of Re-usability?

Classes that you have defined and tested can be reused and adapted to perform new tasks. The base class implementation need not be known for this purpose only required public interface

Access to Public Members in the Base Class

Let us assume B be the base class designated by the public keyword and C be the derived class

  • Hence, all the public members in base class B are publicly available in the derived class C.

Access to Private Members of the Base Class

The private members of the base class are protected in all cases.

  • The methods of the derived class cannot access the private members of the base class.

Translate »