C++ Types of Member Function or Methods in Class | Static vs Non-Static | Data Methods in C++ | OOP Tutorial in C++
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.
Types of Class Member Functions or Methods in OOP :
All member functions can be divided into the following categories:
1. Simple functions
2. Const functions
3. Static functions
4. Inline functions
5. Virtual functions
6. Non-static functions
Simple Member Functions in C++ :
These are the basic member function, which dont have any special keyword like static etc as prefix. All the general member functions, which are of below given form, are termed as simple and basic member functions.
return_type functionName(parameter_list) {
function body;
}
return_type functionName(parameter_list) {
function body;
}
Static Member Functions in C++ :
A function is made static by using static keyword with function name. These functions work for the class as whole rather than for a particular object of a class.
static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. In C++, static can be field, method, constructor, class, properties, operator and event.
It can be called using the object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.
Advantage of C++ static keyword :
Memory efficient: Now we don't need to create instance for accessing the static members, so it saves memory. Moreover, it belongs to the type, so it will not get memory each time when instance is created.
For example:
class X{ public: static void f() { // statement }
}; int main(){ X::f(); // calling member function directly with class name
}
These functions cannot access ordinary data members and member functions, but only static data members and static member functions can be called inside them.
It doesn't have any "this" keyword which is the reason it cannot access ordinary members.
Const Member functions in C++:
Const function is used to specify a “read-only” function. This type of function can’t modify any non-static data members or call any other non-const member functions. If you want to declare a constant function you have to add const keyword after parenthesis of parameter list:
string getAddress() const;
The const keyword must be used both in declaration and implementation of the member function.
string Person::getAddress() const { //some code here }
Inline functions in C++:
Inline functions are declared by using inline keyword
All the member functions defined inside the class definition are by default declared as Inline.
This copies the function to the location of the function call in compile-time and may make the program execution faster.
inline returnType functionName(parameters) { // code }
Friend Functions in C++:
If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function.
Friend functions are actually not class member function. Friend functions are made to give private access to non-class functions. You can declare a global function as friend, or a member function of other class as friend.
For example:
class WithFriend { int i; public: friend void fun(); // global function as friend
}; void fun() { WithFriend wf; wf.i=10; // access to private data member cout << wf.i;
} int main() { fun(); //Can be called directly
}
class Other { void fun(); }; class WithFriend { private: int i; public: void getdata(); // Member function of class WithFriend // making function of class Other as friend here friend void Other::fun(); // making the complete class as friend friend class Other; };
When we make a class as friend, all its member functions automatically become friend functions.
Friend Functions is a reason, why C++ is not called as a pure Object Oriented language. Because it violates the concept of Encapsulation.
Non-Static Member Functions in C++ :
A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier.
Differences between static and non static variables are:
Static variable | Non static variable |
Static variables can be accessed using class name | Non static variables can be accessed using instance of a class |
Static variables can be accessed by static and non static methods | Non static variables cannot be accessed inside a static method. |
Static variables reduce the amount of memory used by a program. | Non static variables do not reduce the amount of memory used by a program |
Static variables are shared among all instances of a class. | Non static variables are specific to that instance of a class. |
Static variable is like a global variable and is available to all methods. | Non static variable is like a local variable and they can be accessed through only instance of a class. |
0 Response to C++ Types of Member Function or Methods in Class | Static vs Non-Static | Data Methods in OOP
Post a Comment