Hello People!
<p dir="auto">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 :). <p dir="auto">Back to our topic, We are going to learn OOP(Object-oriented programming) from very basics to little advance. Starting with class and objects. <h3>What is Object and classes in OOP. <p dir="auto"><code>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). <p dir="auto"><code>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. <p dir="auto">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). <p dir="auto">Open the Dev C++ IDE. Include the iostream and starting writting. <p dir="auto"><img src="https://images.hive.blog/DQmXCSKhwGAZWTpZu1Xva1MwXsHHrNQPEBQt3rtUmKLJQE6/image.png" alt="image.png" /> <p dir="auto">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. <p dir="auto"><img src="https://images.hive.blog/DQmfFQyG7K5P7p2mAYf4oZirSbN4og6j3oZjF76jEpHgmLR/image.png" alt="image.png" /> <p dir="auto">Class keyword then className. <p dir="auto">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. <p dir="auto">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.<br /> <img src="https://images.hive.blog/DQmaDbAx6KZV8YRU1hkvsT1rKbw5GMyUJbbqmMqy8mia1fv/image.png" alt="image.png" /> <p dir="auto">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. <p dir="auto">Now we will initialize a Member function with name display this function will read the values from user , add then and then display them. <p dir="auto"><img src="https://images.hive.blog/DQmTjGbc6uhX6qkycbj8uSvBhneESJx6GZVPE572dZH5i4v/image.png" alt="image.png" /> <p dir="auto">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. <p dir="auto">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; <p dir="auto"><img src="https://images.hive.blog/DQmexRFpWeQgeoZYhLv1tx66zY2McvVbHQYbyyej7FjM5kk/image.png" alt="image.png" /><br /> Our object is created now let's use it to call the function. <p dir="auto"><img src="https://images.hive.blog/DQmPRPSHrJeqiVrFpJ1d2w4XSfwTiweXptU11Qw2jxZ85dS/image.png" alt="image.png" /> <p dir="auto">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. <p dir="auto"><img src="https://images.hive.blog/DQmfQ3xnyiMcsA8mmS9GpxpbgmxzjPWn2gP4GdnPweVSnf1/image.png" alt="image.png" /> <p dir="auto">In the above snap, we can see the proper syntax of object.<br /> Let's run the program. <p dir="auto"><img src="https://images.hive.blog/DQmdxK14Jeje9mbgwtppAPbDuP7SJv6J6f9HEnpRWygq53H/image.png" alt="image.png" /><br /> And our program is working and asking for values lets enter the values and check if it works. <p dir="auto"><img src="https://images.hive.blog/DQmPJnqSMEc82Jhm48qgE2ZT96W5FLLshSp96L49rBSaMwZ/image.png" alt="image.png" /> <p dir="auto">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. <p dir="auto">Take care till that and stay away from covid-19.