B.TECH CSE BT ECE(oop's): Member Function
Types of Member Functions
We already know what member functions are and what they do. Now lets study some special member functins present in the class. Following are different types of Member functions,- Simple functions
- Static functions
- Const functions
- Inline functions
- Friend functions
a) Simple Member functions
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;
}
b) Static member functions/ Static Data member
When we declare a normal variable (data member) in a class, different copies of those data members create with the associated objects.
In some cases when we need a common data member that should be same for all objects, we cannot do this using normal data members. To fulfill such cases, we need static data members.
In this post, we are going to learn about the static data members and static member functions, how they declare, how they access with and without member functions?
Any changes in the static data member through one member function will reflect in all other object’s member functions.
Declaration
It should be defined outside of the class following this syntax:
We can access a static member function with class name, by using following syntax:
class_name:: function_name(perameter);
When we declare a normal variable (data member) in a class, different copies of those data members create with the associated objects.
In some cases when we need a common data member that should be same for all objects, we cannot do this using normal data members. To fulfill such cases, we need static data members.
In this post, we are going to learn about the static data members and static member functions, how they declare, how they access with and without member functions?
- C++ static data member
Any changes in the static data member through one member function will reflect in all other object’s member functions.
Declaration
static data_type member_name;
Defining the static data memberIt should be defined outside of the class following this syntax:
data_type class_name :: member_name =value;
static member function
We can access a static member function with class name, by using following syntax:
class_name:: function_name(perameter);
For Example
#include <iostream> using namespace std; class Demo { private: //static data members static int X; static int Y; public: //static member function static void Print() { cout <<"Value of X: " << X << endl; cout <<"Value of Y: " << Y << endl; } }; //static data members initializations int Demo :: X =10; int Demo :: Y =20; int main() { Demo OB; //accessing class name with object name cout<<"Printing through object name:"<<endl; OB.Print(); //accessing class name with class name cout<<"Printing through class name:"<<endl; Demo::Print(); return 0; }
c) Const Member functions
We will study Const keyword in detail later, but as an introduction, Const keyword makes variables constant, that means once defined, there values can't be changed.When used with member function, such member functions can never modify the object or its related data members.
//Basic Syntax of const Member Function void fun() const {}
d) Inline Function
One of the advantage of using function is to save memory space by making
common block for the code we need to execute many times. When compiler invoke / call
a function, it takes extra time to execute such as jumping to the function definition, saving registers, passing value
to argument and returning value to calling function. This extra time can be avoidable for
large functions but for small functions we use inline function to save extra time.When we make an inline function, compiler will replace all the calling statements with the function definition at run-time.
#include<iostream.h> inline int Add(int x,int y) { return x+y; } void main() { cout<<"\n\tThe Sum is : " << Add(10,20); cout<<"\n\tThe Sum is : " << Add(45,83); cout<<"\n\tThe Sum is : " << Add(27,48); }
d) Friend Function/ Friend Class
Private members are accessed only within the class they are declared.
Friend function is used to access the private and protected members of different classes.
It works as bridge between classes.- Friend function must be declared with friend keyword.
- Friend function must be declare in all the classes from which we need to access private or protected members.
- Friend function will be defined outside the class without specifying the class name.
- Friend function will be invoked like normal function, without any object.
#include <iostream>
class
A {
private
:
int
a;
public
:
A() { a=0; }
friend
class
B;
// Friend Class
};
class
B {
private
:
int
b;
public
:
void
showA(A& x) {
// Since B is friend of A, it can access
// private members of A
std::cout <<
"A::a="
<< x.a;
}
};
int
main() {
A a;
B b;
b.showA(a);
return
0;
}
Comments
Post a Comment