Learning The Object Oriented Programming in C++___Starting with class and objects

in #coding4 years ago

Hello People!

I hope everyone is safe from the Covid-19. It is spreading deadly better stay at home write posts watch movies play the game and---- Write codes just like me :).

Back to our topic, We are going to learn OOP(Object-oriented programming) from very basics to little advance. Starting with class and objects.

What is Object and classes in OOP.

In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).

The user-defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. An instance is a specific object created from a particular class. Classes are used to create and manage new objects and support inheritance—a key ingredient in object-oriented programming and a mechanism of reusing code.

We will try to understand the object and classes through a simple program. we are going to use dev c++ again because it is simple and newbies friendly like I said before (If you want a tutorial on how to install and use dev c++ you can read my previous post about it).

Open the Dev C++ IDE. Include the iostream and starting writting.

image.png

As you can see above the main function the keyword Class which means our parent class is starting from there. The syntax of a class is just like that.

image.png

Class keyword then className.

After then you need to specify either the code is public, protected or private. What are these ?? For now, you just keep in mind that these are access specifiers and for now we are public because this is what we need.

Here we will write a program just to add two number but we will use the concept of OOP. So, first of all, I need variables, Integer type variables , Just below public keyword, we will declare some variables.
image.png

As we can see in the above sanp. We have declared some variables and another keyword PUBLIC . Why we need another public keyword..?? Well in Object-Oriented Programming we have to specify the access specifier for the Member function or Methods.

Now we will initialize a Member function with name display this function will read the values from user , add then and then display them.

image.png

We are displaying a message to user to enter two values with the help of cout and the reading the values with the help of cin. Then we have declared another value named add and assigned the x+y value to it. It means that when the user enter two values the addition of these number will be stored in add variable and whenever the add variable is called it will represent the addition of those numbers.

Now we need an object to call out method or function from the main function. The syntax of object is just like that className objectName;

image.png
Our object is created now let's use it to call the function.

image.png

As we can see in the above snap it is giving us hints about which variable or function we want to access. We need the display function so let's call it.

image.png

In the above snap, we can see the proper syntax of object.
Let's run the program.

image.png
And our program is working and asking for values lets enter the values and check if it works.

image.png

The program is working fine. For now that much is enough. The most important thing in learning programming from basics is practice so keep practicing and see you in the next post.

Take care till that and stay away from covid-19.