Employing the Base Class Function in C++
According to C++, the syntax is as follows to invoke a base class function from a derived class:
The base class that the derived class is descended from is known as BaseClass. The function to be called from the base class is identified by its name which is the function name. The function’s input parameters are known as the arguments. When a function is referred to as being in the base class, the scope resolution operator:: is used to denote this. The derived class can invoke the function of the base class, even if it has overridden the function. The base class must include a “public” or “protected” declaration for the function that is being called.
Example of a C++ Derived Class Calling a Base Class Function
Let’s begin creating the first example in the C++ programming language where we learn how to invoke a base class function from a derived class. The fundamental header file #include <iostream> which is the program’s default input/output library is included first. Next, we use the “using namespace std” which is a declaration that makes the standard namespace that is available in the current scope of the program.
using namespace std;
class Animal {
public:
virtual void speak() {
cout << "Animal speaking" << endl;
}
};
class Dog: public Animal {
public:
void speak() {
Animal::speak();
cout << "Dog barking" << endl;
}
};
int main() {
Animal* animal = new Dog();
animal->speak();
delete animal;
return 0;
}
In this example, we have two defined classes: “Animal” and “Dog”. Since the dog is a descendant of the “Animal” class, all of the “Animal” class’ members are inherited by the “Dog” class. The “Animal” class has a virtual function named speak() which is defined with a default implementation that outputs the “Animal speaking” string to the console using the cout statement. Then, we create the “Dog” class which overrides the speak() function of the “Animal” class with its implementation. The speak() function of the “Dog” class, the base class’s speak() function, is called using the Animal::speak() syntax, and the “Dog barking” string is the output.
The reference to an “Animal” object is generated and initialized to a brand-new “Dog” object in the main() method. “Dog” can be given as a reference to an “Animal” object since it descended from an animal. The speak() function is then called on the animal pointer which invokes the speak() function of the “Dog” class. This demonstrates polymorphism where a derived class object can be treated as if it is an object of the base class. Finally, the memory that is allocated to the “Dog” object is released using the delete operator. The program returns 0 to indicate a successful execution.
This shows that the speak() function of the “Dog” class is able to call the speak() function of the “Animal” class using the:: operator, and then add its implementation to make the dog bark.
Demonstration of Base Class “Shape” and a Derived Class “Circle” that Overrides a Virtual Area() Function
Here is another demonstration of the base class “shape” and a driven class “circle” that overrides a virtual “area()” function. For that, the iostream library is first included and the std namespace is declared to avoid having to prefix the standard library functions with std::.
using namespace std;
class Shape
{
protected:
int width;
int height;
public:
Shape(int w = 0, int h = 0) {
width = w;
height = h;
}
virtual int area() {
cout << "Parent class area:" << endl;
return 0;
}
};
class Circle: public Shape {
public:
Circle(int r = 0): Shape(r, r) { }
int area() {
cout << "Circle class area: ";
return (3.14 * width * width);
}
};
int main() {
Shape* shape = new Circle(5);
cout <<< "Area: " << shape->area() << endl;
delete shape;
return 0;
}
After that, a base class named “Shape” is defined. Any classe that descended from this class inherits the protected data member’s width and height. Along with a simulated function called area(), it also has a public constructor that initializes the width and height to the values that are provided or to 0 if none are. The standard way to implement this technique is simply to print the “Parent class area:” string to the prompt and return 0, while the virtual keyword indicates that this function can be altered by the derived classes. The “Circle” class, which is derived from the “Shape” class, is then defined. This class has a public constructor that takes a single argument “r” which is used to initialize the width and height data members of the base class to the same value. It also overrides the area() function of the “Shape” class to calculate and return the area of a circle using the pi * r^2 formula.
In the main() function, a pointer to a “Shape” object is declared and initialized to a new “Circle” object with a radius of 5. This is achieved by calling the “Circle” constructor with an argument of 5 and storing the resulting object as a pointer to the “Shape” base class. This allows polymorphism to be demonstrated since the same pointer can be used to access the objects of different derived classes that inherit from the same base class.
The area() function is then called on the shape pointer which invokes the area() function of the “Circle” class since it is a virtual function and the shape pointer points to a “Circle” object. The area() function of the “Circle” class then outputs the “Circle class area:” string to the console and returns the calculated area of the circle. In the end, the memory that is allocated to the “Circle” object is released using the delete operator, and the program returns 0 to the main() function that indicates a successful execution. When the program is executed, it outputs the following:
Conclusion
This article showed us how to utilize the “Call Base Class Function” capability to call a method from a derived class. Two examples are given in this article to show how to invoke a base class function from a derived class. In the first illustration, a “Dog” class is shown which inherits from an “Animal” class and overrides the animal’s speak() method to cause the dog to bark. In the second example, a “Shape” class with a “Circle” class that descended from it is shown. To calculate and return the area of a circle, this circle class overrides the area() method.