In this post, we will explore the variance between the Base class and the Derived class in C++. However, prior to delving into their distinctions, it is essential to comprehend the concepts of inheritance, base class, and derived class through illustrative examples.
What is Inheritance?
Inheritance establishes an "is-a" relationship, indicating that a subclass is a more specific type of the superclass. This mechanism enables the subclass to utilize the properties and functions of the superclass.
In the realm of object-oriented programming (OOP), inheritance stands as a foundational concept enabling new classes, referred to as derived or child classes, to acquire characteristics and functionalities from established classes, known as base or parent classes. This establishes a hierarchical relationship between classes, enhancing the reusability and organization of code.
Syntax:
In C++, the syntax for inheritance involves using a colon (:) followed by the access specifier and the base class name within the declaration of the derived class.
class BaseClass {
// Base class members
};
class DerivedClass: access-specifier BaseClass {
// Derived class members
};
Program:
Let's consider a scenario to demonstrate the concept of inheritance in the C++ programming language.
#include <iostream>
using namespace std;
// Base class
class Vehicle {
public:
void start() {
cout << "Vehicle started." << endl;
}
void stop() {
cout << "Vehicle stopped." << endl;
}
};
// Derived class inheriting from Vehicle
class Car: public Vehicle {
public:
void drive() {
cout << "Car is being driven." << endl;
}
};
// Main function
int main() {
Car myCar;
myCar.start(); // Inherited from Vehicle
myCar.drive(); // Function specific to Car
myCar.stop(); // Inherited from Vehicle
return 0;
}
Output:
In this instance, the Car class is a subclass that extends from the parent class Vehicle.
When inheriting from Vehicle, the Car class gains access to the start and stop functions while introducing its drive method.
Understanding C++ Base and Derived Classes:
What is the Base Class?
A foundational class is the superclass that imparts its attributes and behaviors to other classes, referred to as subclasses. It possesses features and functions that are shared among multiple subclasses. Foundational classes are often intended to be abstract, containing solely virtual methods that must be implemented in subclasses.
Program:
Let's consider a scenario to demonstrate the Base Class concept in C++.
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
virtual void draw() const {
cout << "Drawing a shape." << endl;
}
};
// Derived class
class Circle: public Shape {
public:
void draw() const override {
cout << "Drawing a circle." << endl;
}
};
// Main function
int main() {
Shape* shapePtr = new Circle();
shapePtr->draw(); // Output: "Drawing a circle."
delete shapePtr;
return 0;
}
Output:
In this instance, Shape serves as the parent class, with Circle acting as the subclass that derives from Shape. The draw function within the Circle class is redefined to offer a specific implementation.
What is the Derived Class?
A subclass is one that acquires attributes and actions from its parent class. It can introduce its unique attributes and methods while modifying or extending the parent class's capabilities. Multiple inheritances enable a subclass to inherit from multiple parent classes.
Program:
Let's consider an instance to demonstrate the Derived Class concept in C++.
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
virtual void draw() const {
cout << "Drawing a shape." << endl;
}
virtual ~Shape() {} // Virtual destructor
};
// Derived classes
class Circle: public Shape {
public:
void draw() const override {
cout << "Drawing a circle." << endl;
}
};
class Square: public Shape {
public:
void draw() const override {
cout << "Drawing a square." << endl;
}
};
// Main function with multiple derived classes
int main() {
Shape* shapes[2];
shapes[0] = new Circle();
shapes[1] = new Square();
for (int i = 0; i < 2; ++i) {
shapes[i]->draw();
delete shapes[i];
}
return 0;
}
Output:
In this instance, square represents an additional subclass that inherits from the Shape superclass. Both the Circle and Square subclasses provide personalized implementations by overriding the draw function.
Differences between Base Class and Derived Class:-
There exist numerous distinctions between the Base Class and Derived Class. Here are some primary variances between the Base Class and Derived Class:
| Features | Base Class | Derived Class |
|---|---|---|
| Definition | The base class from which all others are derived. | A base class's properties are inherited by this class. |
| Inheritance | It is not possible to inherit from another class. | The base class's properties and behaviors are inherited. |
| Creation | Directly constructed without the use of inheritance. | Developed by deriving from a base class. |
| Access Control | It can gain access to its members as well as those who are public or protected. | Members are inherited according to their access specifier (public, protected, or private). |
| Modifiers | Derived classes cannot directly modify this class. | Can change inherited members or methods. |
| Usage | Serves as a template for derived classes. | Extends or modifies the functionality of the underlying class. |
| Instance Creation | If not abstract (no pure virtual functions), it can be created. | Only through its derived classes may it be instantiated if it has pure virtual functions (abstract). |
| Function Overriding | Derived classes can override virtual functions. | Provides specialized implementations by overriding basic class methods. |
| Relationship | There is no direct connection to other classes. | Its base class has a hierarchical connection with it. |
| Example | class Circle: public Shape{...}; | class Shape: public Shape{.. }; |