C++ Member Functions or Methods, Method vs Function | OOP Tutorial in C++

C++ Member Functions or Methods in OOP, Difference Between Methods and Functions | OOP Tutorial in C++

In this tutorial, we are going to learn about the Member Functions or Methods in C++ and differences between methods and functions.

What is Member Functions or Methods in C++?

Methods are functions that belongs to the class which helps to eliminate the need for writing the same code again and again.

Member functions are the functions, which have their declaration inside the class definition and works on the data members of the class.

How many ways we can define member function in class?

There are two ways to define functions that belongs to a class in Object 0riented Programming (OOP).

What are the two ways of defining member functions?

· Inside class definition --------- (.)

· Outside class definition ------- (::)


How do you create a member function? 

or, 

How can you define member function inside and outside class?

You can access methods just like you access attributes; by creating an object of the class and by using the dot syntax (.) but if its defined outside the class, then we have to use the scope resolution :: operator along with class name alng with function name.


Member function inside and outside the class Example:

Inside Example:

class Blog {        // The class
  public:              // Access specifier
    void myMethod() {  // Method/function defined inside the class
      cout << "Learn OOP in C++";
    }
};

int main() {
  Blog myObj;     // Create an object of MyClass
  myObj.myMethod();  // Call the method
  return 0;
}

 Output:

Learn OOP in C++

 

Outside Example:

class Blog {        // The class
  public:              // Access specifier
    void myMethod();   // Method/function declaration
};

// Method/function definition outside the class
void Blog::myMethod() {
  cout << "Learn OOP in C++";
}

int main() {
  Blog myObj;     // Create an object of MyClass
  myObj.myMethod();  // Call the method
  return 0;
} 

 Output:

Learn OOP in C++


What are the characteristics of member function outside a class?

The member functions declared outside a class has the following characteristics: 

  1. Data type and number of argument in member function must be same as data types and number of data declared in class definition. 
  2. Type scope resolution operator (::) helps in defining the member function outside the class.
  3. The different classes can use same member function names. 
  4. Membership label will resolve their scope limiting a particular class. 
  5. Member function can access private and protected data of a class, but not a non-member function

What is the difference between a method and a function?

Function is a set of instructions that perform a task. 
Method is a set of instructions that are associated with an object.


The main difference between methods and functions is the methods are defined under a class but the functions are defined outside a class i.e the functions do not belong to a particular class.


                    Methods vs. Functions in C++


Methods in C++ :

1. A method also works the same as that of function.

2. A method is defined inside a class.

3. A method can be private, public, or protected.

4. The method is invoked by its reference/object only. For Example: If class has obj as an object name, then the method is called by: obj.method();

5. A method is able to operate on data that is contained within the class
Each object has it’s own method which is present in the class.


Functions in C++ :


  1. A function is a block of statements that takes specific input, does some computations, and finally produces the output.
  2. A function is defined independently. For Example: main() in C++
  3. By default a function is public.
  4. It can be accessed anywhere in the entire program.
  5. It is called by its name itself.
  6. It has the ability to return values if needed.
  7. If a function is defined, it will be the same for every object that has been created.

What is the difference between a member function and a non-member function?

There are two major differences  between a member function and a non-member function:

1. A non-member function always appears outside of a class. The member function can appear outside of the class body (for instance, in the implementation file). But, when you do this, the member function must be qualified by the name of its class. This is to identify that that function is a member of a particular class.

2. Another difference between member functions and non-member functions is how they are called (or invoked) in the main routine. 



0 Response to C++ Member Functions or Methods, Method vs Function | OOP Tutorial in C++

Post a Comment