Definition of a Class Member Function

computer science

Description

Introduction to Classes


The concept of a class is the same as that of a structure: it is a user-defined data type that is used to

create a group of variables that may have different data types.

 In addition to the data members, a member of a class can also be a function.

A member function is in general specified in the class definition by its function prototype with its

definition provided somewhere else in the program.

 You define a class as follows:

class <class-name>

{

<Data-members-and/or-member functions-declarations>

};


 A member of a class (data or function) can be public or private.

 In the definition of a class,

 you use the label public to specify its public members and

 the label private to specify its private members.


Example C1

class Demo1

{

public:

double getValue2(void); // to return the value of the private data member

void setValue2(double num); // to set the value of the private data member

double getAverage( ); // to compute the average of both values

double val1; // the public data member

private:

double computeSum( ); // to compute the sum of both values

double val2; // the private data member

};


©2011 Gilbert Ndjatou Page 208

Definition of a Class Member Function

 When you write the definition of a class member function, you must precede the name of the

function in the function header by the name of that class followed by the scope resolution operator

( :: ).

 A class data member (public or private) can be accessed in the body of a member function of that

class.

 A class member function (private or public) can be called in another member function of that class.


Example C2 Definitions of the member functions of class Demo1:

/*---------------------------------- function getValue2( ) --------------------------------------------------*/

/* returns the value of the private member variable */

double Demo1:: getValue2(void)

{

return (val2);

}

/*---------------------------------- function setValue2( ) --------------------------------------------------*/

/* set the value of the private member variable to the given value */

void Demo1:: setValue2(double num)

{

val2 = num;

}

/*---------------------------------- function getAverage( ) --------------------------------------------------*/

/* compute the average of both values and return it */

double Demo1:: getAverage(void)

{

double total;

total = computeSum( );

return(total / 2);

}

/*---------------------------------- function computeSum( ) ------------------------------------------------*/

/* compute the sum of both values and return it */

double Demo1:: computeSum(void)

{

return (val1 + val2 ) ;

}


©2011 Gilbert Ndjatou Page 209

Declaration of a Class Variable (or Object)

  •  A class variable is called an object.
  • You declare an object in the same way that you declare a structure variable.
  •  As with structure variables, when you declare an object, a member variable is created for each data

member of the class.

Instruction Files

Related Questions in computer science category


Disclaimer
The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.