Learn With Steem Contest||Object Oriented programming(part-2)||Basic Terminolgies in Object Oriented programmingsteemCreated with Sketch.

in Steem4Nigeria2 years ago


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.

Copy of Black & Yellow Modern Abstract How To Tutorial Youtube Thumbnail (1).png

Image created by canva



Introduction



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

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

image.pngimage.png
Interfaces

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
image.pngimage.png
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.

Virtual Functions

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.

image.pngimage.png

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.

Conclusion

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

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.029
BTC 61155.34
ETH 2383.47
USDT 1.00
SBD 2.56