Base class and Derived class in C++
Object-oriented programming (OOP) is a robust paradigm that permits developers to shape their code exceedingly efficiently. A vital idea in OOP is inheritance, which permits the introduction of the latest classes primarily based on current ones. In C++, this concept is realized through the ideas of base and derived classes.
What is Base Class?
The base class is the architecture of the inheritance gadget, generaly known as the superclass. It acts as a template from which a derived class can inherit not unusual houses and behaviors. In C, you outline a base class with the magnificence keyword, followed by way of the magnificence call and the braces of its contributors. For example, recall the base class Shape, which defines color and area:
Code:
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
string color;
double area;
public:
Shape(string c) : color(c), area(0.0) {}
void setColor(string c) { color = c; }
string getColor() { return color; }
double getArea() { return area; }
virtual void calculateArea() = 0; // Pure virtual function
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(string c, double l, double w) : Shape(c), length(l), width(w) {}
void calculateArea() override { area = length * width; }
double getLength() { return length; }
double getWidth() { return width; }
};
int main() {
Rectangle rect("blue", 5.0, 3.0);
rect.calculateArea();
cout<< "Color: " <<rect.getColor() <<endl;
cout<< "Length: " <<rect.getLength() <<endl;
cout<< "Width: " <<rect.getWidth() <<endl;
cout<< "Area: " <<rect.getArea() <<endl;
return 0;
}
Output:
![Base class and Derived class in C++](https://static.tutorialandexample.com/cpp/base-class-and-derived-class-in-cpp1.png)
Explanation:
In this example, the protected access specifier allows derived classes to access the color attribute while keeping it hidden from other external codes. We also declare a pure virtual function, calculateArea(), which derived classes must override with their own implementation.
What is Derived Class?
A derived class is created by using inheriting from a base class, additionally known as a subclass. It inherits the base class's participants (attributes and methods) and might introduce its specific members or override the bottom class behavior. In C++, you outline a derived class using the class keyword, observed through the class call, a colon, the get entry to type for inheritance (public, covered, or non-public), and the base class name. Let's create a derived base known as Rectangle based on the Shape base class:
Code:
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
string color;
double area;
public:
Shape(string c) : color(c), area(0.0) {}
void setColor(string c) { color = c; }
string getColor() { return color; }
double getArea() { return area; }
virtual void calculateArea() = 0; // Pure virtual function
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(string c, double l, double w) : Shape(c), length(l), width(w) {}
void calculateArea() override { area = length * width; }
double getLength() { return length; }
double getWidth() { return width; }
};
int main() {
Rectangle rect("blue", 5.0, 3.0);
rect.calculateArea();
cout<< "Color: " <<rect.getColor() <<endl;
cout<< "Length: " <<rect.getLength() <<endl;
cout<< "Width: " <<rect.getWidth() <<endl;
cout<< "Area: " <<rect.getArea() <<endl;
return 0;
}
Output:
![Base class and Derived class in C++](https://static.tutorialandexample.com/cpp/base-class-and-derived-class-in-cpp2.png)
Explanation:
In this example, we specify public inheritance, indicating that the Rectangle class inherits the public and protected members of the Shape class. The derived class introduces its own members, length, and width, which are specific to a rectangle. We also override the calculateArea() function to provide the implementation for calculating the area of a rectangle.
Inheritance and Usage:
Now that we have both base and derived classes let's explore how they work together. We can create objects of the derived class, which inherit the attributes and methods from the base class. Consider the following code snippet:
Code:
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
string color;
double area;
public:
Shape(string c) : color(c), area(0.0) {}
void setColor(string c) { color = c; }
string getColor() { return color; }
double getArea() { return area; }
virtual void calculateArea() = 0; // Pure virtual function
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(string c, double l, double w) : Shape(c), length(l), width(w) {}
void calculateArea() override { area = length * width; }
double getLength() { return length; }
double getWidth() { return width; }
};
int main() {
Rectangle rect("blue", 5.0, 3.0);
rect.calculateArea();
cout<< "Color: " <<rect.getColor() <<endl;
cout<< "Length: " <<rect.getLength() <<endl;
cout<< "Width: " <<rect.getWidth() <<endl;
cout<< "Area: " <<rect.getArea() <<endl;
return 0;
}
Output:
![Base class and Derived class in C++](https://static.tutorialandexample.com/cpp/base-class-and-derived-class-in-cpp3.png)
Explanation:
In this example, we create an example of the Rectangle class, imparting the colour, length, and width as arguments to the constructor. After that, we name the calculateArea() function to compute the region of the rectangle. Finally, we retrieve and display the colour, length, width, and place of the rectangle using the corresponding member functions.
As demonstrated, the Rectangle
object inherits the color
attribute and the getArea()
function from the Shape
base class. Additionally, it introduces its own specific attributes and overrides the calculateArea()
function to accommodate its unique behavior.
Head-to-head comparison between Base Class and Derived Class
![Base class and Derived class in C++](https://static.tutorialandexample.com/cpp/base-class-and-derived-class-in-cpp4.png)
There are several differences between Base Class and Derived Class. Some main differences between Base Class and Derived Class are as follows:
Features | Base Class | Derived Class |
Relationship | Parent class to derived classes | Child class of a base class |
Inheritance | It can be inherited by one or more derived classes. | It inherits properties and behavior from the base class. |
Accessibility | It can have public, protected, or private members. | It can access public and protected members of the base class. |
Access Specifiers | It determines member access for derived classes. | It can override the access specifiers of base class members. |
Member Functions | It can define and implement its own member functions. | It can inherit and override base class member functions. |
Member Variables | It can define and use its own member variables. | It can inherit and use member variables from the base class. |
Object Creation | It can be instantiated independently. | It can be created as an object of the derived class. |
Conclusion:
Understanding the connection between the base and derived classes is vital for effectively utilizing inheritance in C++. By defining a base class and creating derived lessons from it, you can reuse code, decorate modularity, and sell code reusability. The concepts of base and derived classes defined their syntax and furnished a realistic example. With this expertise, leverage the power of inheritance in your C++ packages, main to nicely-dependent and efficient codebases.