CS 250 Computer Programming and Problem Solving - FALL 1998 

[ Home | Syllabus | Notes | Glossary | CS250 WEB Version Home Page ]


Sections of chapter 3 to be deferred:


Chapter 3: Misc Topics


"const" modifier

Description const is used to tell the compiler that the thing being modified is constant (that is unchangeable) this information is used by the compiler to protect data from being changed.
It can be used to modify:
  • variables (means data cannot be changed)
  • parameters (means argument cannot be changed)
  • member functions in a class (means data members cannot be changed)
Syntax
  • const variableType VariableID = InitialValue;
  • functionType functionID(const parameterType...)
  • functionMemberType functionMemberID(parameterList) const;
Examples
  • const int MaxX = 1000;
  • searchArray(const int a, int sizeA, int item);
  • int getX( ) const;  // guaranteed not to change data members x,y, and display
Tips
  • const variables MUST have an initial value (it can't be changed later)
  • const parameters are most useful for pointer and reference parameters
  • const member function must use const in the function defintion as well
  • If you forget the const in either the prototype or defintion, the compiler will think they are different functions!

 


 

Friends

Description  
  • Private data members of a class can make life difficult sometimes.

  • This is especially true if you want them to be truly private and not even provide a "set" modifier or "get" access function to them.
    (Why would you have a private data member with no access function - because you may want to change your mind how you solved a problem and use a different implementation which does not require that data member).

  • When you define a class you can declare a function to be a friend function.
    This allows it to access the private data members of the class.

Syntax friend function-type function-ID(parameter-list)
Example
(full example here)
#include <math.h>
friend float distance(Coordinate p1, Coordinate p2) {
   float deltaX = p1.x - p2.x;
   float deltaY = p1.y - p2.y;
   return sqrt(deltaX*deltaX + deltaY*deltaY); // sqrt in math.h
}
Tips
  • Avoid using friends unnecessarily
  • One common use of friend functions is to operate on two or more objects of the same class
    (e.g. addition, concatenation of strings, distance (like in the above example)
  • Put the function prototype of the friend in the header file of the class with the modifier friend
  • Define the function in the classes function definition file (.cpp) without the friend modifier
  • Overloading the input output operators is a common use of friend functions

 

 


Go over complete example to show:


 

Overloading Functions

 

Description  
  • Overloading a function means using the same function name for two or more different function definitions
  • Common example is the "+" operator (which is an infix function) which can be used to add integers or floating points numbers
    Another common example is the I/O functions "<<" and ">>" which can handle a variety of data types.
  • So how does the compiler tell the difference? By the number and type of the parameters to the function. As long as the number or type of the parameters are different, there is no problem.
Syntax no special syntax required
Example
float add(float a, float b) {
   return a + b;
}
int add(int a, int b) {
   return a + b;
}
Tips
  • Overloading the c++ operators makes your objects easier to use (e.g. I/O operators)

 

Overloading I/O Stream Operators

 

 

Description  
  • Common and useful way to extend the usual extraction ">>" and insertion "<<" operators so that they can work with your new objects.
  • Operator function is a friend to the class
Syntax friend ostream & operator << (ostream & os, const objectType objectID);

friend istream & operator >> (istream & is, objectType objectID);

Example
// in the class definition in the header file "Coordinate.h"
....
friend ostream & operator << (ostream & oStream, const Coordinate & aPoint);
friend istream & operator >> (istream & iStream, Coordinate & aPoint);

// in the function definition file "Coordinate.cpp"
ostream & operator << (ostream & oStream, const Coordinate & aPoint) {
   oStream << aPoint.x << " " << aPoint.y << " " << aPoint.display;
   return oStream;
}

istream & operator >> (istream & iStream, Coordinate & aPoint) {
   iStream >> aPoint.x >> aPoint.y >> aPoint.display;
   return iStream;
}

// to use, just do the usual I/O statement (in main.cpp)
#include "Coordinate.h"
   Coordinate point1;
   cin >> point1;
   cout << point1;

Tips
  • You can also do error checking on input (like SetX, etc)
  • You can format the output any way you want (e.g. with descriptive text)

 

Overloading I/O Stream Operators (Safer Example)

 

Overload I/O Safer Example for Coordinate

Description  
  • Problem with previous example is that it does not guarantee consistency of x,y, display values
  • This version uses the public setX, setY and setDisplay functions to take care of this
  • Because it does not access the private data members, extraction need not be a friend
Example
(click here for full example)
// in the class defintion in the header file "Coordinate.h"
....
friend ostream & operator << (ostream & oStream, const Coordinate & aPoint);
//NOTE: doesn't need to be friend - see definition below
istream & operator >> (istream & iStream, Coordinate & aPoint);

// in the function definition file "Coordinate.cpp"
ostream & operator << (ostream & oStream, const Coordinate & aPoint) {
   oStream << aPoint.x << " " << aPoint.y << " " << aPoint.display;
   return oStream;
}

istream & operator >> (istream & iStream, Coordinate & aPoint) {
    int x,y,display;
    iStream >> x >> y >> display;
    aPoint.setX(x);
    aPoint.setY(y);
    aPoint.setDisplay(display);

   return iStream;
}

// to use, just do the usual I/O statement (in main.cpp)
#include "Coordinate.h"
   Coordinate point1;
   cin >> point1;
   cout << point1;

Tips
  • You can format the output any way you want (e.g. with descriptive text)

Copyright chris wild 1998.
For problems or questions regarding this website contact [Chris Wild (e-mail:wild@cs.odu.edu].
Last updated: September 14, 1998.