Member Functions in C++

Member Functions

Generally, A class contains data members and member functions

Definition: A member function of a class is a function that has its definition or its prototype within the class definition like any other variable.

It operates on an object of the class of which it is a member, and has access to all the members of a class for that object.

Member Functions in C++

Member Function inside the class

class class_name//class def
{
stmts;//private data
return_type func_name()//memfunc definition inside class
{
----
stmts;
----
}
};

Example:-

/*member function defination inside the class*/
#include<iostream>
using namespace std;
class stu//class definition
{
int id;//private data
char name[20];//private data
public:
void readstu()//memfunc def inside
{
cout<<"enter student id,name:";
cin>>id>>name;
cout<<"stu id:"<<id<<endl;
cout<<"name:"<<name<<endl;
}
};
main()
{
stu s;//obj creation
s.readstu();//memfunc call
cout<<"size of s:"<<sizeof(s);
}

O/P:-

enter student id,name:66 trishaank
stu id:66
name:trishaank
size of s:24

Problem with function definition inside:

If a function defined inside the class, it becomes inline and implicit expansion takes place, hence performance decreases and recommended to define the function outside the class.

Defining member function outside the class

Member functions are defined outside the class using scope resolution operator(::)

Syntax:

return type class_name::func(arg)//memfun def outside
{
-----
body
----
}

Example:-

/* Defining member function outside the class*/
#include
using namespace std;
class stu
{
int id;//private data
char name[20];//private data
public:
void showstu();//memfunction dec

};
void stu::showstu()//memfun defination outside
{
cout<<"enter student id,name:";
cin>>id>>name;
cout<<"stu id:"<<id<<endl;
cout<<"name:"<<name<<endl;
}
main()
{
stu s;
s.showstu();//memfunc call
cout<<"size of s:"<<sizeof(s);
}

O/P:-

enter student id,name:27 rishvik
stu id:27
name:rishvik
size of s:24

void stu::showstu()   indicates that function showstu() belongs to class stu

Accessing private member function of a class

  • When a member function is public we can access that member fun directly by using (.)membership operator anywhere
  • Default access in C++ is private, which is visible only within the class and cannot be accessed outside the class
  • Hence A private member function is accessed by only another public function of the same class within the body of the class

Example:-

In this example getemp() is a private member function, any attempts to call directly would result in an error. hence it is called indirectly by placing the private member function call inside a public function putemp().

/*Accessing private member function of a class*/
#include<iostream>
using namespace std;

class emp
{
int id;
char name[20];
float sal;
void getemp()//private memfun
{
cout<<"enter emp id,name,sal:";
cin>>id>>name>>sal;
}
public:
void putemp()//public memfun
{
getemp();//calling private memfun
cout<< id<< name<< sal;
}
};
int main()
{
emp e;
//e.getemp();error:-emp::getstu() not accesible
e.putemp();
}

O/P:-

enter emp id,name,sal:66 trishaank 15000
emp details:
66 trishaank 15000