Overview

CLASSES

The C++ class is a development of the C structure:

struct now
{
  int hours;
  int mins;
  int secs;
  int frac;
};

struct now time;

time.hours = 19;
time.mins = 15;
time.secs = 0;
time.frac = 10;

Not exactly sure the point or even merit of having this in - suppose it's just another cast off from the old C this probably came from!

The C++ structure has been expanded from the structure designed for C and can now contain both data and function members.

We can make members of a C++ structure or class, function calls. The parts of a class or structure can be defined as being either private or public. The class default is private whereas with a C++ structure, the default it public. The C++ class is now used extensively. The C++ structure has been provided mainly for compatibility with existing C code.

And what has happened to protected? The way this is written makes it look like members can only be public or private!

class Clock
{
  private:
    int minutes;
    int hours;
  public:
    void AdvMins() { cout << "Inline function ";}
    void AdvHours();
};

Note the semicolon after the brace at the end of the block. This is used to signify the end of the class. Missing this out will cause compilation failure.


The 'AdvMins()' function has been defined inside the class. It is therefore an inline function. The 'AdvHours()' function has been defined outside of the class and therefore not inline:

AdvMins may not be an inline method function at all

void Clock::AdvHours()
{
  cout << "Notice the double colon in the function name ";
}

The scope resolution operator :: is used to show to which class a member belongs.

NB : The class defined is a data type (just like an int or char). To use this type that you have defined, you must declare an 'instance' of this type (an object). An instance of the class Clock would be defined like this:

class clock WallClock;

or

clock WallClock;

The main parts of an object are function members, data members and interfaces.

The private keyword of the class mean that the class members following it can only be accessed by other members of the same class. The public parts of the class can be called from elsewhere on the program. This would be written like this:

WallClock.AdvMins();


The data hiding which is enforced by the private part of the class means that the private members cannot be directly used by code other than that defined in the member functions of the same class.

All that is available to the outside code is the class's function call interface; the internal implementation of the class remains a 'black box'.

Class declarations are usually stored in header files. These declarations contain the class name and member function prototypes, but do not contain the class implementation code.

Any types of data including basic data types, user defined types, constants and even objects of other classes can be used as data member of a class.

A C++ program accomplishes it's work by passing messages between objects, through the co-operation of collections of objects, rather than by a flow of information around a system. An object responds to a message by executing the appropriate member function.

"Passing messages"? What on earth???

A class can have as many instances as required by the application.

EXERCISE 14

Initialise and use a static variable as a class member.


MEMBER FUNCTIONS AND THE const SPECIFIER

The keyword const placed after the argument list of a member function, tells the compiler that this member function can only read the data items, but cannot write them :-

Why are there two headers (both incorrect) being used here when nothing from either of them have been used? I fail to see why they need to be there at all!

#include <iostream.h>
#include <conio.h>

class game
{
  private:
    int player_number;
    int high_score;
  public:
    void setscore(int& value, int& player);
    void getstatus(int& bullets) const;
};

Notice how the member function getstatus obeys these rules.

This is not very clear as it is contradicted (seemingly) further down

We can now write data to the object's private data only by the use of the member function setscore.

Note however, that the getstatus is called with a reference to an integer variable called bullets which can and is changed in the function. It is the object's private data alone that is write protected.

Any attempt to place code in the getstatus function to change the values of the private data high_score or player_number will cause a compliation error informing the programmer that a constant cannot be changed.

The rest of the example follows.....


Bit of a pity here - it's not compilable, despite not being a bad example

void main()
{
  int value = 1000;
  int player = 1;
  int bullets = 25; 	// start value for bullets
  clrscr();
  game score;
  score.setscore(value,player);
  score.getstatus(bullets);
}

void game::setscore(int& value, int& player)
{
  high_score = value;
  player_numer = player;
}

void game::getstatus(int& bullets) const
{
  int score = 100;
  cout << "High score " << high_score << '\n';
  cout << "Player number " << player_number <<'\n';
  cout << "Bullets before last shot " << bullets <<'\n';
  bullets--;
  cout << "Bullets now left " << bullets <<'\n';
  // high_score = score;	Not allowed - object is a constant.
  getch();
}

EXERCISE 15

Write a function which uses the const keyword with a reference parameter to a data structure or class, so that the data in the data structure or class cannot be altered by the function.