Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Class Member Functions

C++ Class Member Functions

C++ Classes & Objects C++ Classes & Objects

A class member function is a function whose definition and prototype are written inside the class definition, just like other variables in the class definition. A class member function is a member of the class; it can operate on any object of the class and can access all members within the object.

Let's look at the previously defined class Box. Now we will use member functions to access the class's members, rather than accessing those members directly:

class

Box

{

public

:

double

length

;

// length

double

breadth

;

// width

double

height

;

// height

double

getVolume

(

void

)

;

// Return volume

}

;

Member functions can be defined inside the class definition, or separately using thescope resolution operator :: to define them. A member function defined within the class definition is declared asinline, even without using the inline identifier. So you can define getVolume() functions as follows:

class

Box

{

public

:

double

length

;

// length

double

breadth

;

// width

double

height

;

// height

double

getVolume

(

void

)

{

return

length

breadth

height

;

}

}

;

You can also usescope resolution operator :: outside the class to define the function, as shown below:

double

Box

::

getVolume

(

void

)

{

return

length

breadth

height

;

}

Here, one point must be emphasized: the class name must be used before the :: operator. Calling a member function uses the dot operator on the object (.), so it can operate on the data associated with that object, as shown below:

Box

myBox

;

// Create an object

myBox

.

getVolume

(

)

;

// Call the object's member function

Let's use the concepts mentioned above to set and get the values of different members in a class:

Example

#include

<

iostream

using

namespace

std

;

class

Box

{

public

:

double

length

;

// length

double

breadth

;

// width

double

height

;

// height

// Member function declaration

double

getVolume

(

void

)

;

void

setLength

(

double

len

)

;

void

setBreadth

(

double

bre

)

;

void

setHeight

(

double

hei

)

;

}

;

// Member function definition

double

Box

::

getVolume

(

void

)

{

return

length

breadth

height

;

}

void

Box

::

setLength

(

double

len

)

{

length

=

len

;

}

void

Box

::

setBreadth

(

double

bre

)

{

breadth

=

bre

;

}

void

Box

::

setHeight

(

double

hei

)

{

height

=

hei

;

}

// main function of the program

int

main

(

)

{

Box

Box1

;

// declare Box1 of type Box

Box

Box2

;

// declare Box2 of type Box

double

volume

=

0

.0

;

// Used to store volume

// box 1 details

Box1

.

setLength

(

6

.0

)

;

Box1

.

setBreadth

(

7

.0

)

;

Box1

.

setHeight

(

5

.0

)

;

// box 2 details

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

;

return

0

;

}

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



Box1 的体积: 210

Box2 的体积: 1560

C++ Classes & Objects C++ Classes & Objects