Derived Class
In C++, a derived class is a class that inherits the properties and behavior of a base class, similar to how it works in other object-oriented programming languages.
In simple terms, a derived class in programming is like a child who inherits traits and properties from their parents. In object-oriented programming, a class is like a blueprint for creating objects, defining their characteristics and behaviors.
Now, imagine you have a basic class, like "Animal". This class might have general traits that all animals share, like the ability to eat and sleep. A derived class, also known as a subclass, is a more specific version of this class. For example, "Bird" could be a derived class of "Animal". It inherits all the general traits of an "Animal" (like eating and sleeping), but it also has its own special traits, like flying and laying eggs.
In this way, the derived class extends the functionality of the base class (like "Animal") by adding new features or modifying existing ones to be more specific. This concept is a cornerstone of object-oriented programming because it allows for the creation of more specific and detailed objects without having to rewrite all the basic traits again. It promotes reusability and organization in code.
Creating Derived Classes in C++
In C++, inheritance allows you to create a new class (the derived class) that inherits properties and behaviors from an existing class (the base class). This promotes code reuse and enables you to build more complex systems by extending or modifying existing functionality.
Basic Syntax for Creating a Derived Class
Example:
class Base {
public:
int base_public_var;
private:
int base_private_var;
};
class Derived : public Base {
public:
int derived_public_var;
};
In this example:
Derived
is a derived class that inherits from theBase
class.By using the
public
access specifier,Derived
has access to the public members ofBase
(such asbase_public_var
).The private members of the base class (such as
base_private_var
) are not inherited and remain inaccessible to the derived class.Derived
also adds its own member variable,derived_public_var
.
Access Specifiers and Inheritance
The access specifier (public
, protected
, or private
) between the base and derived class defines how the base class members are inherited. Here’s a quick breakdown:
Public Inheritance: Public members of the base class remain public in the derived class, and protected members remain protected. Private members of the base class are not accessible.
Protected Inheritance: Public and protected members of the base class become protected in the derived class.
Private Inheritance: All public and protected members of the base class become private in the derived class.
Overriding Base Class Functions
In a derived class, you can override base class member functions by defining a function with the same name and signature. This allows the derived class to provide its own specific implementation of a base class function.
Example:
class Base {
public:
void foo() { cout << "Base::foo()" << endl; }
};
class Derived : public Base {
public:
void foo() { cout << "Derived::foo()" << endl; }
};
In this example:
Derived
overrides thefoo()
function fromBase
by providing its own implementation.When you call
foo()
on an instance ofDerived
, it will call theDerived::foo()
version, not theBase::foo()
version.
Output:
Derived obj;
obj.foo(); // Output: "Derived::foo()"
Calling Base Class Functions from a Derived Class
Even if a derived class overrides a base class function, you can still call the base class version explicitly using the scope resolution operator (::
).
Example:
In this case, the Derived::foo()
function will call both the base class version of foo()
and then its own version.
Calling Base Class Constructors
When a derived class is instantiated, the constructor of the base class is automatically called first to ensure proper initialization of the base class members. You can call a specific base class constructor using the initialization list syntax in the derived class constructor.
Example:
In this example:
Derived
calls the constructor ofBase
using the initialization list (: Base(value)
), passing thevalue
argument to initialize them_value
member inBase
.This ensures that the base class portion of the object is properly initialized before the derived class-specific initialization takes place.
Output:
Key Points to Remember:
Inheritance Hierarchies: Inheritance allows you to build hierarchies of classes, with base classes containing shared functionality and derived classes extending or specializing that functionality.
Access Control: Be mindful of how you use access specifiers (
public
,protected
,private
) during inheritance, as they affect what members are accessible in the derived class.Function Overriding: You can override base class functions in derived classes to provide specialized behavior, while still having access to the base class functions if needed.
Constructor Chaining: When a derived class is constructed, the base class constructor is called first. You can explicitly call base class constructors using an initialization list in the derived class constructor.
By understanding how inheritance works in C++, you can design more modular, reusable, and maintainable code that leverages the power of object-oriented programming.
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark