C++ Class Member Functions

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 any object of the class of which it is a member, and has access to all the members of a class for that object.

Let us take previously defined class to access the members of the class using a member function instead of directly accessing them −

classBox{public:double length;// Length of a boxdouble breadth;// Breadth of a boxdouble height;// Height of a boxdoublegetVolume(void);// Returns box volume};

Defining Class Member Functions

Member functions can be defined within the class definition or separately using scope resolution operator, : −. Defining a member function within the class definition declares the function inline, even if you do not use the inline specifier. So either you can define Volume() function as below −

Defining Member Function inside the Class

classBox{public:double length;// Length of a boxdouble breadth;// Breadth of a boxdouble height;// Height of a boxdoublegetVolume(void){return length * breadth * height;}};

Defining Member Function outside of the Class

If you like, you can define the same function outside the class using the scope resolution operator (::) as follows −

doubleBox::getVolume(void){return length * breadth * height;}

Here, only important point is that you would have to use class name just before :: operator.

Calling (Accessing) Member Functions

A member function will be called using a dot operator (.) on a object where it will manipulate data related to that object only as follows −

Box myBox;// Create an object

myBox.getVolume();// Call member function for the object

Example

Let us put above concepts to set and get the value of different class members in a class −

#include <iostream>usingnamespace std;classBox{public:double length;// Length of a boxdouble breadth;// Breadth of a boxdouble height;// Height of a box// Member functions declarationdoublegetVolume(void);voidsetLength(double len );voidsetBreadth(double bre );voidsetHeight(double hei );};// Member functions definitionsdoubleBox::getVolume(void){return length * breadth * height;}voidBox::setLength(double len ){
   length = len;}voidBox::setBreadth(double bre ){
   breadth = bre;}voidBox::setHeight(double hei ){
   height = hei;}// Main function for the programintmain(){
   Box Box1;// Declare Box1 of type Box
   Box Box2;// Declare Box2 of type Boxdouble volume =0.0;// Store the volume of a box here// box 1 specification
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);// box 2 specification
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);// volume of box 1
   volume = Box1.getVolume();
   cout <<"Volume of Box1 : "<< volume <<endl;// volume of box 2
   volume = Box2.getVolume();
   cout <<"Volume of Box2 : "<< volume <<endl;return0;}

When the above code is compiled and executed, it produces the following result −

Volume of Box1 : 210
Volume of Box2 : 1560

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *