C++ member functions

C++ MEMBER FUNCTION

Member functions can be defined in two places, show below

1. outside the class Definition
2. inside the class Definition

It is obvious that irrespective of the place of definition, the function should do the same task. Therefore code for the function body would be identical in both the cases However, there is some difference in the way the function header is defined. but these approaches are discussed in detail in this tutorial

outside the class definition

member functions that are declared inside a class have to be defined separate outside the class. They should have function header and a function body, Their definitions are very much like the normal functions. Since C++ language does not support the old version of function definition, the ANSI prototype form must be used for defining the function header

An important difference between a member functions and a normal functions is that a member function incorporates a membership identical label in the header. This label tells to the compiler which class the function belongs to .

The general form of a member function definitions is

return-type class-name:: function-name(argument declaration)
{
function body
}

The membership label class-name:: tells to the compiler that the function function-name belongs to class class-name. That is the scope of the function is restricted to the class-name specified in the header line. The symbol :: is called the scope resolution operator

for instance consider the member functions getdata() and displaydata() as discussed above they may be coded as follows

void student:: getdata(int f, int r)
{
roll=f;
fees=r;
}
void student :: displaydata()
{
std::cout<<"roll number"<<roll<<"\n";
std::cout<<"fees" <<fees<<"\n";
}

since these functions do not return any value, their return type is void. function arguments are declared using the ANSI prototype

The member functions have some special characteristics that are often used in the program development. These characteristics are

1. several different classes can use the same function name. The membership label will resolve their scope.
2. member functions can access the private data of the class. A non member function can not do so.(How ever an exception to this rule is a friend function discussed later)
3. A member function can call another member function directly, without using the dot operator

for example, The function call statement s.getdata(200,89);
is valid and assigns the value 200 f and 89 to r of the object s by implementing the getdata() function. The assignment occur in the actual function
and similar the statement
s.displayData();

Inside the class Definition

Another method of defining a member function is to replace the function declaration by the actual function definition inside the class. for exmaple we could define the item class as follows

class item
{
int number;
float cost;
public:
void getdata(int f,int r); // it is declaration
// inline function
void displaydata() // definition inside the
class
{
std::cout <<roll<<"\n";
std::cout <<fees<<"\n";
}
};

when a function is defined inside a class, it is treated as an inline function. Therefore all the restrictions and limitations that apply to an inline function are also applicable here.Normally, only small functions are defined inside the class definition

C++ program with class

#include <iostream>
using namespace std;
class student
{
int roll;
int fees;
public:
void getdata(int f,int r); // prototype declaration to be defined
// function defined inside class
void displaydata()
{
std::cout<<"roll no" <<"\roll" <<"\n";
std::cout<<"fees"<<fees <<"\n";
}
};
void student:: getdata(int f, int r)
{
roll=r; // this is private variable
fees=f; // directly used
}
int main()
{
student s;
std::cout << "\n object s"<<"\n";
s.getdata(100,288); // call member function
s.displaydata(); // call member function
student t; // create another object
std::cout<<"\n object t"<<"\n";
t.getdata(300,500);
t.displaydata();
}

So explain this program. This program features the class student. This class contain two private variables and two public functions. The member function getdata() which has been defined outside the class supplies values to the variables.

Note the use of statements such as

roll=r;

in the function of getdata(). This shows that the member functions can have direct access to private data items

The member function displaydata() has been defined inside the class and therefore behaves like an inline function. This function displays the value the values of the private variables roll and fees.

The program creates two objects, X and Y in two different statements. This can be combined in one statements student s,t;
Output of the above program:

object s
roll no 1000
fees 288
object t
roll no 300
fees 500

MAKING AN OUTSIDE FUNCTION INLINE

one of the objectives of OOP is to separate the details of implementation from the class definition. it is therefore good practice to define the member functions outside the class

we can define a member function outside the class defination and skill make it inline by just using the qualifier inline in the header line of function definition Example

class Student
{
…….
…….
public:
void getdata(int f, int r); // declaration
};
inline void item:: getdata(int f,int r) // definition
{
fees=f;
roll=r;
}

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise