Sunday, January 22, 2023

Defining member functions in C++

 Defining member functions in C++

In C++ when we create a function inside a class it is known as member function. A member function in C++ can be defined in two different ways:

  • We can define function inside the class i.e. body of the member function will be provided inside class definition.

  • We can declare function inside the class i.e. its prototype will be declared inside the class definition but its definition i.e. its body will be defined outside the class.
If we know that our function definition is small in length, it does not contain any loops or any other complex statements then we may define member function inside the class. If a member function is defined inside a class then compiler may try it treat it as inline function. As a result all the advantages and drawbacks of inline functions will come along.

Example:


Output:

a = 10

b = 20

Click Here to Download Source Code

Another way is to give prototype of function inside the class i.e. function can be declared inside the class and its definition is provided outside the class. If we define a member function outside the class then we have to use scope resolution operator to differentiate it from functions of other classes.

Example:


Output:

a = 10

b = 20

Click Here to Download Source Code

In case we declare member function inside the class then location of its declaration will decide its visibility i.e. it is public, private or protected. In our case both member functions are declared in public section so they are public in nature.



#c++ #cpp #scoperesolution #memberfunctions #cplusplus #functions #definition #coding #codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net

Thursday, January 12, 2023

Scope Resolution (::) Operator in C++

 Scope Resolution (::) Operator in C++

Scope Resolution Operator in C++ is denoted by two consecutive colon (::) symbols. The common uses of Scope Resolution Operator:

  • It is used to access global variable in a program if it has same name as of a local variable in a function.
  • It is used to define static variables outside class.
  • It is used to access static member functions and static data members outside class.
  • It is used to define a member function outside the class.
  • It is used to resolve ambiguity in case of multiple inheritance.
  • It is used to refer data members and member functions of a nested class.
  • It is used to access classes and objects from a namespace.

Now we will discuss each of the above uses one by one with suitable examples.
  • If we declare a global variable in a C++ program and it has same name as of some local variable in a function then by default local variable is accessed & it hides the visibility of global variable.
Example:


The above code will display "a = 50".
Now to make global variable visible in main function we will use scope resolution operator:


The above code will display "a = 50" and "a = 100" in next line.



  • Static variables declared within a class must also be defined outside the class, for this scope resolution operator is used.
  • Scope resolution operator is also used to access static members i.e. static data members (class variables) and static member functions outside the class. Example:
In this example we have declared static variable "a" inside class and it also has been defined outside the class using scope resolution operator with initial value 10.




In main function scope resolution operator is also used to access member function "display()" as well as class variable (static variable) "a".  Program will display "a = 10" and "a = 11" as output.


  • In C++ we can define member functions inside as well as outside the class. If member function is to defined outside the class then its declaration must be provided inside the class and definition must begin with identity label using class name and scope resolution operator like:
return-type class-name::function-name(parameter list)
{
//body
}
I will explain this with help of example given below:


In class abc show function is declared inside but its definition is given outside the class using scope resolution operator. class-name::(abc::) is identity label which used to differentiate same name methods of different classes.


  • In case of multiple inheritance if more than one base class has same name member function, then the the derived class will have definition of both the functions but when one of the function is called then an error will be there. (Ambiguity in multiple inheritance)
To resolve ambiguity we use scope resolution operator. Example:


Output:
    Class A
    Class B
    Class C

In above example classes A,B & C has function fxn(). Class C is derived class which inherit both A &B. So, in class C we have three definitions of fxn() will cause ambiguity when called by the object of Class C i.e. ob, to resolve this we have used scope resolution operator as:
    ob.A::fxn();// it will call fxn() of Class A
    ob.B::fxn();// it will call fxn() of Class B
    ob.C::fxn();// it will call fxn() of Class C

  • Sometime we define a class within another class which is known as nested class, to declare object of the nested class we use scope resolution operator.
Example:

Output:
Outer Class A
Inner Class B

We have used scope resolution operator to declare object of class B which is nested inside class A.

  • We can use objects from a namespace without using namespace declaration. It can be achieved with help of scope resolution operator.





#scoperesolutionoperator #nestedclass #staticclass #cpp #multipleinheritance #inheritance #local #global
#auto #break #case #char
#const #continue #default #do
#double #else #enum #extern
#float #for #goto #if
#int #long #register #return
#short #signed #sizeof #static
#struct #switch #typedef #union
#unsigned #void #volatile #while

#Keywords  #Identifier
#Variables  #Constants
#datatypes
#Input #output #functions
#Operators 

Tuesday, August 30, 2022

Fastest Method to Convert Binary Number into Decimal Number

 




















#codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net #binarytodecimal #numbersystem







Sunday, August 21, 2022

Increment & Decrement Operators

  • Increment (++) operator is used to increase the value of a variable by 1.

  • Decrement (--) operator is used to decrease the value of a variable by 1.

  • Both the operators are unary i.e. they only require one operand.

  • There are two forms in which ++ and - can be used:
    1.  Prefix
    2. Postfix

  • Prefix: In this form operator is written before the operand: ++a, --b.
  • Postfix: In this form operator is written after the operand: at+, b-.
  • If there is no assignment then prefix and postfix will give same result.
    Eg:




















In both cases a = 11 and b = 19


  • If there is assignment then prefix and postfix may give different result.





    x = 11 a = 11
    y = 21 b = 21

    x = 10 a = 11
    y = 20 b = 21



    Prefix


    int a =10, x;

    x=++a;

    It is calculated as a two step process:

    Step 1:a=a+1:// a will become 11

    Step 2:x=a;// x will become 11

    In case of prefix rule is:

    First increment/decrement is performed then assignment is performed.


    Postfix

    int a=10, x;

    x=a++;

    It is calculated as a two step process:


    Step 1:x=a;//
    x will become 10

    Step 2:a=a+1;//
    a will become 11

    In case of postfix rule is:

    First assignment is performed then 
    increment/decrement is performed.

    In case of following for loops both postfix & prefix will give same result:




     
    Both loops will print 1 to 5


    In case of printf functions result may vary:


    It will print a = 11
    It will print a = 10
    Finally a will be 11
    in both case.



    #codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net

Sunday, August 14, 2022

Arithmetic Operators in C Programming

  • Arithmetic operators are used to perform basic arithmetic operations.

  • There are five Arithmetic Operators used in C Language:

    Addition(+), Subtraction(-), Multiplication(*), Division(/) & Modulus(%).

  • Modulus gives us remainder of two operands and can only be applied on integer data.

  • Addition, subtraction, multiplication and division can be applied on both integer as well as real data.

  • Arithmetic operators are binary in nature i.e. they require at least two operands to perform operation.

  • Arithmetic Operators can be Applied on both +ve as well as -ve numbers.

  • Symbols used for Arithmetic operators are:




  • Arithmetic Operation Eg:






  • Applying '/' & '%' on -ve numbers :





  • On applying '%' to -ve numbers, sign of numerator will be assigned to the result.





Program


Output















#codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net

Saturday, August 13, 2022

Selection Sort Technique, Complexity & Program

  •  Suppose we want to arrange numbers of a list in ascending order.


  • We will divide our list in two parts: Sorted List & Unsorted List.

  • Initially all the elements are considered as Unsorted.


  • Now we will find minimum element from the unsorted list & swap it with the first element of unsorted list.








  • Now "1" will become part of Sorted List & we will repeat our process for remaining unsorted part.









  • Again we will find minimum element from unsorted list & swap it with the first element of unsorted list.











  • "2" will become part of sorted list & process will be repeated for remaining unsorted list.










  • Again we will find minimum element from unsorted list & swap it with first element of unsorted list.










  • "3" will become part of sorted list & we will repeat our process for remaining unsorted list.










  • Again we will find minimum element from unsorted list & swap it with first element of unsorted list.











  • As "5" is minimum element & it is already at its correct position so, no swap is required.



  • Similarly the remaining elements of unsorted list will become part of sorted list.







    Program

    //Selection Sort
    #include<iostream.h>
    #include<conio.h>
    int selection_sort(int a[],int n)
    {
    int i,j,t,u;
    for(i=0;i<n-1;i++)
    {
    u=i;//index of first element of unsorted list
    for(j=i+1;j<n;j++)
    {
    if(a[j]<a[u])
    u=j;//updated when
       //element less than minimum is found
    }
    if(i!=u)//no need to swap
    {       //if element is already at correct position
    t=a[i];
    a[i]=a[u];
    a[u]=t;
    }
    }
    }
    void main()
    {
    int a[]={12,5,8,2,1,7,3},i;
    clrscr();
    selection_sort(a,7);
    cout<<"Sorted Array"<<endl;
    for(i=0;i<7;i++)
    {
    cout<<a[i]<<endl;
    }

    getch();
    }








    Selection Sort technique, complexity & program with minimum number of swaps. https://www.instagram.com/reel/Cg6So4Uhwza/?igshid=YmMyMTA2M2Y= #datastructure #sorting #searching #minimumelement #bestcoding #goodcode #leetcode #aws #python #cprogram #cprogramming #bca #mca #it #cs #complexity #clanguage #java #javascripSot #DeveloperMode Selection Sort Program Algorithm to sort integers Sorting of Integer numbers

Monday, August 1, 2022

Binary Search : Technique, Program & Complexity

Technique

  • Let us say we have a book of "600" pages and we want to read page number "338", one method is to apply linear search and turn pages one by one which is very time consuming and inefficient.




  • Another way is that we can randomly open a page from book which may be near to 338, suppose we opened page number 420. Now we will only search our page in the left part of the book i.e. between 1 to 420.




  • Again say we got to page number 330 so, now we will search between page 330 to 420.

  • This way we can reach our page in a much efficient way.

  • Binary Search will work in a similar manner, which can only be applied on sorted data.

  • In this method we will compare the item to be searched with the item at middle index of array.





  • Say "l" is lower bound & "r" is upper bound of array, so middle will be calculated as        mid = ( l + r ) / 2.

  • Then we will compare our item with "a[mid]" element of our Array as follows:

            if ( item == a[mid] )

            return mid;

            else if ( item > a[mid] ) 

            l = mid + 1;

            else 

            r = mid - 1;

  • If item == a[mid] i.e. item is found then, we will return "mid" and our search is successful.



  • If  item > a[mid] i.e. "item" is greater than "a[mid]" then we set "l" to the element on the right of "mid" & "r" remains at its position.

  • It means item exist between "mid + 1" & "r".




  • Now, we will again calculate "mid" & repeat the process.

  • If item < a[mid] i.e. "item" is less than "a[mid]" then we set "r" to the element on the left of "mid". "l" remains at its position.
  • It means item exist between "l" & "mid - 1".




  • We will stop our search when "l" becomes greater than "r". It means search is unsuccessful and we will return -1.

Program

Example

  • Suppose we want to find location of element "17" in Array given below:




  • We will take three variables "l" , "r", & "mid". Initially we will take l=0 & r=6. (Lower Bound=0 & Upper Bound=6)

  • Then we will calculate index of middle element of our Array as ( l + r ) / 2 and store the result in mid.
    mid=( l + r ) / 2 = ( 0 + 6 ) / 2 = 3.


  • Now we will compare our item "17" with a[mid] i.e. a[3], Both are not equal.




  • So, we will check whether our item "17" is greater than a[3] or not. As "17" is greater than "10". So, "17" might exist between "mid + 1" & "r".

  • It will reduce our search bracket. Now we will set "l = mid + 1" & r will remain unchanged, i.e. "l = 4" & "r = 6".


  • Now again we will calculate mid = ( l + r )/2 = ( 4 + 6 )/2 = 5.





  • Now we will compare our item "17" with a[mid] i.e. a[5], Both are equal and our search is successful so, our algorithm will return "mid" i.e. "5"

Let us take another example

  • Suppose we want to find location of item "2" in Array given below:


  • Again we will set l=0 & r=6. (Lower Bound=0 & Upper Bound=6)

  • Set mid = ( l + r ) / 2 = ( 0 + 6 ) / 2 = 3.



  • Now we will compare our item "2" with a[mid] i.e. a[3], Both are not equal.





  • So, we will check whether our item "2" is greater than a[3] or not. As "2" is not greater than "10". So, "2" is less than a[mid] and "2" might exist between "l" & "mid-1".

  • It will reduce our search bracket. Now we will set "r = mid - 1" & "l" will remain unchanged, i.e. "l = 0" & "r = 2".



  • Now again we will calculate mid=( l + r )/2 = ( 0 + 2 )/2 = 1.




  • We will compare our item "2" with a[mid] i.e. a[1]. Again both are not equal.

  • So, we will check whether our item "2" is greater than a[1] or not. Again "2" is not greater than "5". It means "2" is less than a[mid] and "2" might exist between "l" & "mid-1".



  • It will reduce our search bracket. Now we will set "r = mid - 1" & "l" will remain unchanged, i.e. "l = 0" & "r = 0".




  • We will calculate mid = ( l + r ) / 2 = ( 0 + 0 ) / 2 = 0.

We will compare our item "2" with a[mid] i.e. a[0]. As both are equal so, our algorithm will return mid i.e. 0.





Binary Search Complexity
  • In Best case Binary Search will find the location of item in given array in only 1 comparison so, it is O(1). .
  • In Worst case at each iteration of Binary Search the array size will be reduced to its half.
  • At iteration 1, size of array = n
  • At iteration 2, size of array = n/2
  • At iteration 3, size of array = n/4
  • At iteration k, size of array = 1 .i.e. n/2^k = 1 => n = 2^k => k=log(n)
  • So, in Worst Case Complexity of Binary Search is O(log(n).
  • In Average case of Binary Search item could be found in: 1 comparison or 2 comparisons or 3 comparisons.. or log(n) comparisons where log(n) = m.
  • So, Total comparisons (1+2+3+..+m)
  • Total no. of cases = m
  • Average Comparisons = (1+2+3+...+m)/m where m = log(n). = m ( m + 1 ) / 2m = ( m + 1 ) / 2 = ( log(n) + 1 ) / 2 .
  • So, Average Complexity of Binary Search is O(log(n)).

Coding Acceleration Program

🚀 CODING ACCELERATION PROGRAM (90 DAYS) Build Strong Foundations Learn to Think Like a Programmer Get Placement Ready 💡 Learn C Progra...