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✌

2 comments:

Atharva said...

Very nice efforts

Unknown said...

You are working hard dude. Accurate and perfect explanation..🙌🙌

Post a Comment

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 ...