C++ Class Construct  «Prev  Next»
Lesson 12 Program dissection
ObjectiveWrite const print and intersection Member Functions

Write const print and intersection Member Functions

Orthogonal Class Design
Write const print and intersection member functions for a class that implements a mathematical set.
Let us examine an example program that calculates salaries and illustrates how static and const member functions use the this pointer.

Program dissection

Move your mouse cursor over the highlighted lines of code below for a pop-up discussion of their function.
Apply, Filter, Sort
  1. There are three private data members. The static member all_bonus requires a file scope declaration. It can exist independent of any specific variables of type salary being declared
  2. This statement assigns the value of b to the member b_sal. This member function initializes the base salary.
  3. The modifier static must come before|the function return type.
  4. The const modifier comes between the end of the argument list and the front of the code body. It|indicates that no data member will have its value changed. Thus, it makes the code more robust. In effect, it means that the self-referential pointer is passed|as const salary* const this.
  5. A static member function can be invoked using the scope resolution operator. It could also have been invoked as w1.reset_all(400); but this is misleading, since there is nothing special|about using the class variable w1.

Big C++
Dissection Information
The static keyword is used only in the class definition and needs to be omitted when the data or function member is defined outside the class.

Const Member Function - Exercise

Click the exercise link below to add const print and intersection member functions to a class that implements a mathematical set.
Const Member Function - Exercise