Ticker

6/recent/ticker-posts

1. Introduction to OOP and C++

     Object Oriented Programming (OOP) is a programming paradigm that provides a way of modularizing the programs by creating a partitioned memory area for both data and functions so that they can be used as a template for creating the copies of search modules on demand.
     OOP eliminates some of the flaws encountered in Procedure Oriented Programming (POP). OOP treats data as a critical element in program development and does not allow it to flow freely around the system. It ties data more closely to the functions that  operate on it and protects it from accidental modification by outside functions.


Features/Characteristics of OOP
i. It focuses data rather than function.
ii. Programs are divided into objects.
iii. Data are hidden and cannot be accessed by external functions.
iv. New data and functions can be easily added whenever required.
v. Follows bottom-up approach of program design.


Structured Programming and OOP (Difference)

i.(Structured Programming): Less secured, as it does not have any proper way for hiding data.
  (OOP): It is more secured as it provides data hiding.

ii. (Structured Programming): Program is divided into small parts called functions.
    (OOP): Program is divided into small parts called objects.

iii. (SP): It follows top-down approach.
     (OOP): It follows bottom-approach.

iv. (SP): It does not have any access specifier.
     (OOP): It has access specifier namely Public, Private and Protected.

v. (SP): There is no inheritance.
    (OOP): Inheritance is allowed.

vi. (SP): Less abstraction and Less flexibility.
     (OOP): More abstraction and More flexibility.

vii. (SP): It provides Less re-usability and More function dependency.
      (OOP): It provides More re-usability and Less function dependency.



Inline Function

The function which when called at any point of the program execution, the compiler places the copy of the code of that function, such a function is called as Inline function.

Example:
1.   #include<iostream>
2.   using namespace std;
3.   inline int max (int x, int y)
4.  {
5.   return (x>y) ? x : y ;
6.   }
7.   int main()
8.   {
9.    max_value = max(20,10);
10.  cout<<"The greater number is: "<<max_value<<endl;
11.   return 0;
12.  }

     Now, let's figure out what the program's logic is. Line 1 and 2 are common to all the C++ programs. We have declared a function as a inline by using a keyword 'inline'. Two arguments are passed in the function of integer type. Line 5 is the conditional statement which returns the maximum value by comparing x and y. Main function starts from Line 7. The function max() is called in Line 9 and the maximum value is assigned to the variable 'max_value'. Line 10 is just the printing statement of our maximum value.


Advantages of Inline Function
i. It helps to reduce code.
ii.Manage complex coding.
iii. It does not require functions calling overhead.


Disadvantage of Inline Function
It may cause compilation overhead as if somebody changes code inside inline function then all calling location will also be compiled.


Inline function and Normal Function (Difference)

i. (Inline Function): The compiler replaces the function call statement with the function code itself.
(Normal Function): When a function call instruction is encountered, the compiler stores the memory address of instruction.

ii. (I F): STACK memory frame is not allocated during function call as it replaces the function call code with the function code.
(N F): If a normal function is called then the function call is stored on the STACK and when the function execution is over then the STACK is cleared.


Function Overloading
It is a method of writing functions to do conceptually the same thing on different data types without changing the name but assigning different logic inside the function.


Advantages of Function Overloading

i. It saves memory and space.
ii. Same function name is used, user don't need to remember the name of the multiple functions.
iii. Consistency and Readability.


Example:
1.   #include<iostream>
2.   using namespace std;
3.   void sum():
4.   void sum(int, int);
5.
6.   int main()
7.   {
8.    sum();
9.    sum(2,3);
10.  void sum()
11.  {
12.     a = 5, b = 6;
13.     cout<<"Sum is: "<<a+b<<endl;
14.    }
15.    void sum(int x, int y)
16.   {
17.     cout<<"Sum is: "<<x+y<<endl;
18.    }
19.   }

         Line 3 and 4 is the declaration of two functions. Both the functions has same name 'sum' but one has no arguments and the other has two arguments. Line 8 and 9 are the calling statement of the functions. We have passed the value 2 and 3 in the function of Line 9 whereas the Line 8 has no arguments passed. Line 10 to 14 is the definition of function sum() in which we have assigned the value of a and b and add it. Line 15 to 18 is the definition of function sum(int, int). There is no need to assign the value as it is already assigned in x and y with the help of function call in Line 9.



Rules for Function Overloading

i. Number of arguments must be changed.
ii. Types of arguments must be changed.
iii. Sequence of arguments must be changed.

Note: Return type has no significance in function overloading.


Function Overloading and Function Overriding (Difference)

i. (Function Overloading): There is a relationship between methods available in the same class.
(Function Overriding): There is a relationship between sub class method and super class method.

ii. (Overloading): It does not block inheritance from superclass.
    (Overriding):   It blocks inheritance from superclass.

iii. (Overloading): Separate methods share the same name.
     (Overriding): Subclass method replaces superclass method.

iv. (Overloading): It must have different method signature.
     (Overriding): It must have same signature.















Reactions

Post a Comment

0 Comments