C++ OOP Example Code and Explanation.

            In this post, we are going to be discussing an example of object orientation in actual code and we'll try to get along with syntax and few new things. I assume you are introduced to the concept of OOP(if not check my previous post on Introduction to OOP).
            
            Prerequisites:
  1.   Should possess experience basic programming concepts.
  2.  Should know the concepts(Object, Class, Attributes, Method) in OOP theoretically.                        
        Things we'll learn
  1. Syntax 
  2. Access specifier 
  3. Constructor and Destructor
  4. Methods
  5. Quick Code Explanation
            The following code is a basic representation of Object orientation in a code. We'll proceed explanation considering RP (Reference Points) in code. (open the image in a new tab to get clear experience.)
  
  • Syntax:
            To declare a class you simply need to type the keyword class following with the name of your class. As you can see at RP1, The first letter of the name of the class is capitalized. It's not mandatory to do that but its a good programming habit since, it differentiates the name of the class with other variables.
        
  • Access Specifiers:
            Now proceeding further you'll see two keywords "public" and "private". These are called Access specifiers. The thing to remember about them is, Whatever code you write inside the "private" keyword is accessible only to the members of the class. and the code that is written inside the public keyword is accessible within the whole code. you can call all the member functions, member variables in the main function as well as in user-defined function; basically anywhere in code. Now you might come up with a question that, what if you want to call something which has private access? To access the members declared privately we call them in public with the help of an extra variable or function and then we give a call to that variable or function that called the private member. it's like when a student wants to contact the head of some department he first needs to contact intermediate faculty and then get the work done.
  • Constructor:
            Now we move to RP4 and you might get confused here. The statement that you see on line 12 is called a Constructor function. The constructor is just a function with the few changes. As you can see the Construction Function name and the class name are Identical... Well, that's what makes it A Constructor, It always has the same name as the class name. You might also observe that there is no return-type written. That's because the constructor never returns any value. 
            Now, what does a Constructor DO? A constructor is a function that gets called automatically when you declare an Object. That means when the compiler executes line no. 33 it immediately executes constructor. In our case, it will initialize a,b member variables with p and q and will finally print "Automatically Executed!".
            Again a question! Can we only have one constructor in a class? The answer is NO! You can have as many Constructor you want. depending on your number of arguments and data types of arguments compiler will call the appropriate constructor. 
  • Destructor:
            The function that you see on RP9 is called Destructor which is similar to Constructor but it gets called when an object has done all its work. Technically saying, when the object gets out of scope. Remember that Destructor neither takes any argument nor returns any value. Destructor has '~' this symbol to differentiate from Constructor since they possess the same name. There can only be one destructor, unlike constructors. 
  • Method:
            Like we discussed in the previous post Method is just the function inside the class. If it is public it can directly be called on an Object. Remember method can only be called on an object, unlike other functions. As you can see in the following code at RP6 and RP7 I have created two methods product and greater. which when called gives product and a greater number in between two given by the user respectively.
            In lines 35 and 36, you can see how the method is called on object "Obj" using the dot  ' . ' operator. Notice the parentheses"( ) " written in a call. Always Remember that method will always have parentheses. when we access an attribute value(line no. 34, str1 is the attribute), we don't write parentheses but in the case of method, we need to write them, That's the difference between accessing attribute value and method call.4
  • Quick Code Explanation:
  1. In the following Code, "Mynumbers" is the name of the class, which has 'x' and 'y' private integers 'a' and 'b' public integers, str1 private string, and str2  public string. which all are the attributes of the object.
  2. It has a constructor that takes two integer arguments and assigns them to private variables x and y and prints Automatically executed
  3. It has two methods, "product" and "greater" and a destructor that prints "Everything ended!" 
  4. when execution starts at line 31, the object of type Mynumbers gets declared with two integer arguments, which calls the constructor which initializes values and prints the content
  5. At line 34 str1 public attribute value is accessed and printed.(Notice NO parentheses)
  6. next two lines call methods on object Obj which give respective outputs.
  7. At line no. 37, A public method that has access to all the private attributes, is called on object Obj hence, ultimately private members are called in the main function with help of the intermediate public method.
You can see the output Below!


So this covers like most Basic things that are needed in object orientation in C++, I hope this post was informative to you and you learned something from it. let me know what you think about OOP in the comments down below, Share doubts, thoughts, suggestions, anything you want there.

We'll catch-up in the next post!

Thank you for reading!😄

Peace✌

Everything You need to know to get started with Object Oriented programming.|| INTRODUCTION TO OOP

         In this post, We are going to be talking about Object Orientation in a programming language and we'll also be talking about a bunch of stuff that's in it. Clean up your mind, keep it real empty for now, if you have searched or read something about it from somewhere.

    Prerequisites:

  1. You should know basic programming(Loops, Conditional statements, Functions, Pointers) and have a little experience of it.                 
        Firstly I would tell you that the object orientation in a programming language is just another concept like Functions and Pointer so, there's nothing extraordinary about it as you may feel about it after reading the word "OBJECT-ORIENTED PROGRAMMING!".

  • Attributes and Classes.

        Imagine a scenario, Think about a Car and try to point out what characteristic does a car have. You might come up with a few like, Name of the Car, Name of the brand, Color, type of Engine it has, and category of car. Alright, now I would ask you a question, Have you ever dreamed about having your own DATA TYPE just like Int, Float, String where you can put whatever you want like a Car and all its characteristics...... are you getting what I am trying to point out here? Yes! with the help of OOP, you can actually store characteristics of an "OBJECT" which are ultimately called "ATTRIBUTES" in the language of programming. that means "color", "name", "engine type", are the "ATTRIBUTES" of an " OBJECT" named as "car". Now the question is how do we create this DATA TYPE?. The answer is with the help of "CLASS". Now you might say What is class?. Class is like a Universal kind of data type which after naming becomes our own data type. for eg. if there is a bar of normal chocolate, then we call it a chocolate bar, and when it's wrapped with harshey's wrapper we say its Hershey's chocolate bar. it's exactly similar, We name our class with any name we want, and that name now becomes our brand new custom data type, that we build for ourselves. For eg., if I declare "class Vehicles" then "Vehicles" is now my own data type.

  • Methods

        Now let's get to another point. We all are familiar with a term called "Functions". Functions are the distinct pieces of code that we can call as many times as we want to perform desired operations. When we write a function inside of the Class it becomes "METHOD". Methods are the functions that can access the attributes and the variables declared inside the class and it can perform any operation you like as long as you are able to program it. for eg. I can create a function "tellme()" which prints all the values of attributes of my object when I call it on my object. ("car.tellme()") You just have to act smart to get your work done with methods.

  • What is actually an Object?

now let's talk about "OBJECTS". What are objects? to answer this question we need to first understand that all the arrangement that we made inside the class is the kind of like a blueprint. what do I mean by that? let's take an example. You may have noticed that many times Houses in outside Countries are Identical in every manner. I mean like in each and every aspect all houses are similar. So there must be a blueprint of the structure of the house. and that blueprint is used to create all the houses. that is why they are identical. this blueprint is what we consider our class. and when we create a house in accordance with the blueprint then that house is what we call "OBJECT" in programming. i,e when we declare a "Variable" of our "CLASS" then it's actually an "OBJECT". An object is also called as an "instance of a class" that is straightforward to understand. 

If you read it carefully:

  1. Vehicle=Class
  2. car=Object
  3. name,brand,engine,category=Attributes
  4. tellme()=Method


So these were the most basic 3-4 things, that you needed to get started with learning object orientation in a programming language.


Now let's try and implement all this in code and let's dig into it a little bit better and let's get technical.

I have written the Code with Easiest Explanation in C++ and Python. You can basically read anyone you like. Here are the links to those posts.

Click Here For C++


That's all for this post. I hope You liked it. If you feel this post can help others as well then share it with friends. lets spread knowledge and support community.

Leave comments if you have any kind of doubt or suggestions. Kindly follow my blog if you liked my content. and react to the post about how you felt about it.


Thank You for Reading...!

PEACE✌

Everything You need to know to get started with Object Oriented programming.|| INTRODUCTION TO OOP

            In this post, We are going to be talking about Object Orientation in a programming language and we'll also be talking about ...