Defining member functions, nesting of member functions, Private Member function, making outside function inline
c++ – Defining member functions inside or outside the class definition
Member functions of a class can be defined either outside the class definition or inside the class definition. In both the cases, the function body remains the same, however, the function header is different.
Outside the Class: Defining a member function outside a class requires the function declaration (function prototype) to be provided inside the class definition. The member function is declared inside the class like a normal function. This declaration informs the compiler that the function is a member of the class and that it has been defined outside the class. After a member function is declared inside the class, it must be defined (outside the class) in the program.
The definition of member function outside the class differs from normal function definition, as the function name in the function header is preceded by the class name and the scope resolution operator (: :). The scope resolution operator informs the compiler what class the member belongs to. The syntax for defining a member function outside the class is
1
2
3
|
Return_type class_name :: function_name (parameter_list) { // body of the member function
} |
To understand the concept of defining a member function outside a class, consider this example.
Example : Definition of member function outside the class
1
2
3
4
5
6
7
8
9
10
11
12
|
class book {
// body of the class
}; void book :: getdata( char a[], float b) {
// defining member function outside the class
strcpy (title,a):
price = b:
} void book :: putdata () {
cout<< "\nTitle of Book: " <<title;
cout<< "\nPrice of Book: " <<price;
} |
Note that the member functions of the class can access all the data members and other member functions of the same class (private, public or protected) directly by using their names. In addition, different classes can use the same function name.
Inside the Class: A member function of a class can also be defined inside the class. However, when a member function is defined inside the class, the class name and the scope resolution operator are not specified in the function header. Moreover, the member functions defined inside a class definition are by default inline functions.
To understand the concept of defining a member function inside a class, consider this example.
Example : Definition of a member function inside a class
1
2
3
4
5
6
7
8
9
|
class book {
char title[30];
float price;
public :
void getdata( char [], float ); II declaration
void putdata() //definition inside the class {
cout<< "\nTitle of Book: " <<title;
cout<< "\nPrice of Book: " <<price;
} ; |
In this example, the member function putdata() is defined inside the class book. Hence, putdata() is by default an inline function.
Note that the functions defined outside the class can be explicitly made inline by prefixing the keyword inline before the return type of the function in the function header. For example, consider the definition of the function getdata().
1
2
3
|
inline void book ::getdata ( char a [], float b) {
body of the function
} |
Nesting of member functions:
A member function can be called by using its name inside another member function of the same class is known as nesting of member functions.
program:
20) #include<iostream.h>
//nested member function
class squarenumber
{
int number;
public:
long square();
void getnumber();
void display();
};
void squarenumber::getnumber()
{
cout<<"Enter an integer number:";
cin>>number;
}
long squarenumber::square()
{
number=number*number;
return (number);
}
void squarenumber::display()
{
cout<<"Square of the number:"<<square();
}
int main()
{
squarenumber sqr;
sqr.getnumber();
sqr.display();
return 0;
}
output:
enter an integer number:12
square of the number:144
21) #include<iostream.h>
//nested member function
class set
{
int m, n;
public:
void input();
void display();
int largest();
};
void set::input(void)
{
cout<<"Input values of m and n"<<"\n";
cin>>m>>n;
}
int set::largest(void)
{
if(m>=n)
return(m);
else
return(n);
}
void set::display(void)
{
cout<<"Largest value="<<largest()<<"\n";
}
int main()
{
set a;
a.input();
a.display();
return 0;
}
output:
Input values of m and n
20
10
Largest value is 20
Example of private member function in C++
Private Member Function
A function declared inside the class's private section is known as "private member function". A private member function is accessible through the only public member function. (Read more: data members and member functions in C++ (Links to an external site.)).
Example:
In this example, there is a class named "Student", which has following data members and member functions:
-
Private
-
Data members
- rNo - to store roll number
- perc - to store percentage
-
Member functions
- inputOn() - to print a message "Input start..." before reading the roll number and percentage using public member function.
- inputOff() - to print a message "Input end..." after reading the roll number and percentage using public member function.
-
Data members
-
Public
-
Member functions
- read() - to read roll number and percentage of the student
- print() - to print roll number and percentage of the student
-
Member functions
Here, inputOn() and inputOff() are the private member functions which are calling inside public member function read().
Program:
#include <iostream>
using namespace std;
class Student
{
private:
int rNo;
float perc;
//private member functions
void inputOn(void)
{
cout<<"Input start..."<<endl;
}
void inputOff(void)
{
cout<<"Input end..."<<endl;
}
public:
//public member functions
void read(void)
{
//calling first member function
inputOn();
//read rNo and perc
cout<<"Enter roll number: ";
cin>>rNo;
cout<<"Enter percentage: ";
cin>>perc;
//calling second member function
inputOff();
}
void print(void)
{
cout<<endl;
cout<<"Roll Number: "<<rNo<<endl;
cout<<"Percentage: "<<perc<<"%"<<endl;
}
};
//Main code
int main()
{
//declaring object of class student
Student std;
//reading and printing details of a student
std.read();
std.print();
return 0;
}
Output
Input start...
Enter roll number: 101
Enter percentage: 84.02
Input end...
Roll Number: 101
Percentage: 84.02%
Error: When you try to call the private member function inside the main with the object name.
Example: Changing only main() part
//Main code
int main()
{
//declaring object of class student
Student std;
//trying to call private data member
std.inputOn(); //error - because it's a private member
//reading and printing details of a student
std.read();
std.print();
return 0;
}
Output
main.cpp: In function 'int main()':
main.cpp:10:8: error: 'void Student::inputOn()' is private
void inputOn(void)
^
main.cpp:47:14: error: within this context
std.inputOn();
Making an Outside Function Inline
We can define a member function outside the class definition and use make it inline using qualifier inline in the header line of function definition.
For example#include<iostream.h>
#include<conio.h>
class InlineDemo
{
int a;
public:
void getdata(int n);
void show();
};
inline void InlineDemo::getdata(int n)
{
a=n;
}
inline void InlineDemo::show()
{
cout<<”Value of a= “<<a;
}
void main()
{
InlineDemo obj;
obj.getdata(10);
obj.show();
getch();
}
OutputValue of a= 10