Sunday 22 January 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

Wednesday 11 January 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