Class Member Functions

**+

Defining the Body of a Member Function

Example
The avg_price function has an empty parameter list and returns a value of type double. It is declared inside the class but defined elsewhere. The same_isbn function returns a bool and takes a single parameter of type reference to const Sales_item. It is defined inside the Sales_item class.
A member function that is defined inside the class is implicitly treated as an inline function.

class Sales_item 
{
     public:
         // operations on Sales_item objects
         double avg_price() const;
         bool same_isbn(const Sales_item &rhs) const
              { return isbn == rhs.isbn; }
     // private members as before
     private:
         std::string isbn;
         unsigned units_sold;
         double revenue;
};

Introducing this

Each member function has an extra, implicit parameter named this. When a member function is called, the this parameter is initialized with the address of the object on which the function was invoked.**

heading level 2@@raw text

insert the code here

@@

Introducing const Member Functions

const modifies the type of the implicit this parameter.
A function that uses const in this way is called a const member function. Because this is a pointer to const, a const member function cannot change the object on whose behalf the function is called.

Defining a Member Function Outside the Class

Member functions defined outside the class definition must indicate that they are members of the class.

Example
The function name Sales_item::avg_price uses the scope operator to say that we are defining the function named avg_price that is defined in the scope of the Sales_item class. The const that follows the parameter list reflects the way we declared the member funcion inside the Sales_item header. In any definition, the return type and parameter list must match the declaration, if any, of the function. In the case of a member function, the declaration is as it appears in the class definition. If the function is declared to be a const member function, then the const after the parameter list must be included in the definition as well.

double Sales_item::avg_price() const
{
         if (units_sold)
             return revenue/units_sold;
         else
             return 0;
}

Constructors

Class data members are not initialized when the class is defined. Instead, data members are initialized through a constructor.

Defining a Constructor

Example

class Sales_item 
{
     public:
         // operations on Sales_item objects
         double avg_price() const;
         bool same_isbn(const Sales_item &rhs) const
             { return isbn == rhs.isbn; }
         // default constructor needed to initialize members of built-in type
         Sales_item(): units_sold(0), revenue(0.0) { }
     // private members as before
     private:
         std::string isbn;
         unsigned units_sold;
         double revenue;
     };

Constructor Initialization List

heading level 6

++

heading level 2
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.