Hi Steemians
I warmly welcome all of you in my today's Publication which is the continuation of my series in this Steem for Nigeria community. I participated in the first two weeks but was unable to participate in the previous weeks due to my busy exam schedule. So let's start the second part of Object Oriented Programming let's start the discussion.
In my previous post I explain the introduction to Object-Oriented Programming in C++ we explored the basics of OOP, including classes and objects, inheritance, polymorphism, and encapsulation. So In this post, I will talk more deeply exploring the concepts of interfaces, virtual functions, and some others.
Abstract classes can be concretized as those classes that can be used as programs for other classes. An abstract class is a Specific class that must contain predefined functions in them. These base classes can not be used to create the objects. Abstract classes provide a common interface to the derived classes. Here is a sample code for an abstract class in C++:
- Example Code
Now I will show the Example code on the online Compiler
Interfaces in the language represent the relation and contract between two different classes. In C++, it is a way to define a relation between two classes. Classes can be implemented in our codes without the specifications. This allows different classes to implement the same interface in the same way, while maintaining the same contract. Interfaces are a powerful tool in OOP that helps in again usage of the code.
- Example Code
In this example, we have defined an interface called ICalculator which specifies four methods for performing calculations, namely, add(), subtract(), multiply(), and divide(). These methods must be implemented by any class that implements the ICalculator interface. We have implemented the ICalculator interface in the SimpleCalculator class by providing our own implementation of the four methods. We can now create an instance of the SimpleCalculator class and use it to perform calculations in the main function. |
---|
so that was how an interface can be used in our programming.
A Virtual function can be said as a member function that can be overridden in a derived class. Virtual functions can be used to provide a base class with the execution of the overridden derived class. This allows a derived class to customize the manners of the base class. The derived class can provide its own enactment of the virtual function.
In this example, we have a base class Calculator with a virtual function calculate() that takes two double arguments and returns a double value. We also have three derived classes, Adder, Subtractor, and Multiplier, each of which overrides the calculate() function with its own implementation for adding, subtracting, and multiplying two numbers.
In the main() function, we create objects of each derived class and assign them to a Calculator pointer. We then call the calculate() function on each object through the pointer, which will call the correct implementation based on the actual type of the object at runtime.
So these were some basic terminologies about object-oriented programming. Concepts of these terms are very necessary to learn deeper about object-oriented programming. In the next week, I will explain the constructor and destructor. I hope that you all will like to read this post. Thanks, @fredquentum for organizing this series.
Regards:@mateenfatima