HomeLinuxName Base Class Operate in C++

Name Base Class Operate in C++


In C++, when a brand new class is created which is known as a “derived class”, it will probably borrow the strategies and properties from one other class known as a “base class”. Generally, the brand new class desires to make use of a technique from the previous class but additionally provides some new conduct. To do that, the brand new class can use the “Name Base Class Operate” characteristic. Which means that the brand new class makes use of the previous class’s methodology as a substitute of its model of the tactic. This helps keep away from repeating the code and makes the brand new class extra environment friendly.

Using the Base Class Operate in C++

In response to C++, the syntax is as follows to invoke a base class perform from a derived class:

The bottom class that the derived class is descended from is named BaseClass. The perform to be known as from the bottom class is recognized by its title which is the perform title. The perform’s enter parameters are generally known as the arguments. When a perform is known as being within the base class, the scope decision operator:: is used to indicate this. The derived class can invoke the perform of the bottom class, even when it has overridden the perform. The bottom class should embrace a “public” or “protected” declaration for the perform that’s being known as.

Instance of a C++ Derived Class Calling a Base Class Operate

Let’s start creating the primary instance within the C++ programming language the place we discover ways to invoke a base class perform from a derived class. The elemental header file #embrace <iostream> which is this system’s default enter/output library is included first. Subsequent, we use the “utilizing namespace std” which is a declaration that makes the usual namespace that’s accessible within the present scope of this system.

#embrace <iostream>
utilizing namespace std;
class Animal {
   public:
      digital void converse() {
         cout << “Animal talking” << endl;
      }
};
class Canine: public Animal {
   public:
      void converse() {
         Animal::converse();
         cout << “Canine barking” << endl;
      }
};
int major() {
   Animal* animal = new Canine();
   animal>converse();
   delete animal;
   return 0;
}

On this instance, we’ve got two outlined courses: “Animal” and “Canine”. Because the canine is a descendant of the “Animal” class, all the “Animal” class’ members are inherited by the “Canine” class. The “Animal” class has a digital perform named converse() which is outlined with a default implementation that outputs the “Animal talking” string to the console utilizing the cout assertion. Then, we create the “Canine” class which overrides the converse() perform of the “Animal” class with its implementation. The converse() perform of the “Canine” class, the bottom class’s converse() perform, is known as utilizing the Animal::converse() syntax, and the “Canine barking” string is the output.

The reference to an “Animal” object is generated and initialized to a brand-new “Canine” object in the principle() methodology. “Canine” might be given as a reference to an “Animal” object because it descended from an animal. The converse() perform is then known as on the animal pointer which invokes the converse() perform of the “Canine” class. This demonstrates polymorphism the place a derived class object might be handled as whether it is an object of the bottom class. Lastly, the reminiscence that’s allotted to the “Canine” object is launched utilizing the delete operator. This system returns 0 to point a profitable execution.

This reveals that the converse() perform of the “Canine” class is ready to name the converse() perform of the “Animal” class utilizing the:: operator, after which add its implementation to make the canine bark.

Demonstration of Base Class “Form” and a Derived Class “Circle” that Overrides a Digital Space() Operate

Right here is one other demonstration of the bottom class “form” and a pushed class “circle” that overrides a digital “space()” perform. For that, the iostream library is first included and the std namespace is asserted to keep away from having to prefix the usual library capabilities with std::.

#embrace <iostream>
utilizing namespace std;
class Form
{
   protected:
      int width;
      int top;
   public:
      Form(int w = 0, int h = 0) {
         width = w;
         top = h;
      }
      digital int space() {
         cout << “Mum or dad class space:” << endl;
         return 0;
      }
};

class Circle: public Form {
   public:
      Circle(int r = 0): Form(r, r) { }

      int space() {
         cout << “Circle class space: “;
         return (3.14 * width * width);
      }
};
int major() {
   Form* form = new Circle(5);
   cout <<< “Space: “ << form>space() << endl;
   delete form;
   return 0;
}

After that, a base class named “Form” is outlined. Any classe that descended from this class inherits the protected knowledge member’s width and top. Together with a simulated perform known as space(), it additionally has a public constructor that initializes the width and top to the values which can be offered or to 0 if none are. The usual option to implement this method is just to print the “Mum or dad class space:” string to the immediate and return 0, whereas the digital key phrase signifies that this perform might be altered by the derived courses. The “Circle” class, which is derived from the “Form” class, is then outlined. This class has a public constructor that takes a single argument “r” which is used to initialize the width and top knowledge members of the bottom class to the identical worth. It additionally overrides the realm() perform of the “Form” class to calculate and return the realm of a circle utilizing the pi * r^2 method.

In the principle() perform, a pointer to a “Form” object is asserted and initialized to a brand new “Circle” object with a radius of 5. That is achieved by calling the “Circle” constructor with an argument of 5 and storing the ensuing object as a pointer to the “Form” base class. This permits polymorphism to be demonstrated for the reason that identical pointer can be utilized to entry the objects of various derived courses that inherit from the identical base class.

The world() perform is then known as on the form pointer which invokes the realm() perform of the “Circle” class since it’s a digital perform and the form pointer factors to a “Circle” object. The world() perform of the “Circle” class then outputs the “Circle class space:” string to the console and returns the calculated space of the circle. In the long run, the reminiscence that’s allotted to the “Circle” object is launched utilizing the delete operator, and this system returns 0 to the principle() perform that signifies a profitable execution. When this system is executed, it outputs the next:

Conclusion

This text confirmed us find out how to make the most of the “Name Base Class Operate” functionality to name a technique from a derived class. Two examples are given on this article to point out find out how to invoke a base class perform from a derived class. Within the first illustration, a “Canine” class is proven which inherits from an “Animal” class and overrides the animal’s converse() methodology to trigger the canine to bark. Within the second instance, a “Form” class with a “Circle” class that descended from it’s proven. To calculate and return the realm of a circle, this circle class overrides the realm() methodology.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments