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

Write const print and intersection Member Functions

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
  1. static and
  2. const member functions

use the this pointer.
Apply, Filter, Sort


//Calculate salary using static members.
class salary {
public:
   init(int b) { b_sal = b; }
   void  calc_bonus(double perc)
     { your_bonus = b_sal * perc; }
   static void  reset_all(int p)
     { all_bonus = p; }
   int  comp_tot() const
     { return (b_sal + your_bonus + all_bonus); }
private:
   int         b_sal;
   int         your_bonus;
   static int  all_bonus;     //declaration
};

//declaration and definition
int salary::all_bonus = 100;

int main(){
   salary  w1, w2;
    
   w1.init(1000);
   w2.init(2000);
   w1.calc_bonus(0.2);
   w2.calc_bonus(0.15);
   salary::reset_all(400);
   cout << " w1 " << w1.comp_tot() << "   w2 "
    << w2.comp_tot() << endl;
}

class  salary {
. . .
private:
   int        b_sal;
   int        your_bonus;
   static int all_bonus;     //declaration
};

The relevant text outside of the code block in the image:
  • "calculate salary using static members."
  • "//declaration and definition"

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.
init(int b) { b_sal = b; }

This statement assigns the value of b to the member b_sal. This member function initializes the base salary.
static void  reset_all(int p) { all_bonus = p; }

The modifier static must come before the function return type.
int  comp_tot() const{ 
 return (b_sal + your_bonus + all_bonus); 
}

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.
salary::reset_all(400);

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. 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. 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.
  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.

Orthogonal Class Design

An orthogonal class design is an attempt to build a minimal set of operations that gives a user a complete interface to the class. For example, you would not normally have two volume controls on a radio. Also, on a radio with multiple bands, you are given a control to switch between bands and a separate control to position within a band. This is an orthogonal design, and it would be possible to have separate controls for each band, but that would proliferate the number of controls unnecessarily. Prefer a design that favors the client over the class implementor. The client must be sold on the class. If a client of a class needs a highly efficient runtime class, then that requires a complex implementation. The class implementor must meet that requirement or the class will not be used.
  • Orthogonal Data Object: In computer terminology, a programming language or a data object is orthogonal if it can be used without consideration as to how its use will affect something else. A programming language is orthogonal if its features can be used without thinking about how that usage will affect other features. C++ is considered to be a non-orthogonal language.
  • Orthogonality Programming Language: Orthogonality in a programming language means that a relatively small set of primitive constructs can be combined in a relatively small number of ways to build the control and data structures of the language. The term is most-frequently used regarding assembly instruction sets, as orthogonal instruction set. Features of a program that is compatible with its own earlier versions - this is called backward compatible, have an orthogonal relationship with the features of the earlier version, because they are mutually independent; you don't have to worry about how the use of one version's features will cause an unintended effect because of an interaction with those of the other version. Both the features and the programs can be said to be mutually orthogonal.

Orthogonal Persistence

The length of time data is kept in storage in a computer system is known as its persistence. Orthogonal persistence is the quality of a programming system that allows a programmer to treat data similarly without regard to the length of time the data is kept in storage. Data is stored for varying lengths of time; some is stored very briefly and some is stored relatively permanently. Frequently, a programmer must use different approaches and separate coding to access data depending on whether it is stored for a long time or a short time. Using a programming system with orthogonal data persistence allows the programmer to treat data the same way regardless of its persistence characteristic, saving programming time and making it easier to enforce referential integrity (a type of constraint applied to ensure correct data validity).
  • Class Hierarchy of a Rectangular Box:
    The defining properties of a Box object are just the three orthogonal dimensions. You can apply this basic definition to the many different kinds of rectangular boxes that you find in the real world: cardboard cartons, wooden crates, candy boxes, and cereal boxes. All these have three orthogonal dimensions, and in this way they are just like generic Box objects. However, each of them has other properties such as the things they are designed to hold, or the material from which they are made. You could describe them as specialized kinds of Box objects. For example, a Carton class could have the same properties as a Box object(namely three dimensions) plus the additional property of its composite material. You could then specialize even further by using the Carton definition to describe a FoodCarton class, which is a special kind of Carton that is designed to hold food. A FoodCarton object will have all the properties of a Carton object and an additional member to model the contents. A Carton object has the properties of a Box object so a FoodCarton object will have those as well.

Class Carton inherits from class Box, class FoodCarton inherits from ClassCarton.
Class Carton inherits from class Box, class FoodCarton inherits from ClassCarton.
The connections between classes that express these relationships are shown in Figure 3-12. The Carton class is an extension of the Box class. You might say that the Carton class is derived from the Box class. In a similar way, the FoodCarton class has been derived from the Carton class. It is common to indicate this relationship diagrammatically by using an arrow pointing toward the more general class in the hierarchy. This notation is called UML (Universal Modelling Language) and which is used in Figure 3-12.

C++ Programming Language

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

SEMrush Software