Classes

Classes are a special type like integers or characters, or user-defined types. Classes are used to represent objects in your programs; different objects allow different actions to be performed on them. For example, if you were to write a graphics program, you could have a Point class, which would define how points were represented in your program.

The definition of a class will generally include both data members and function members. Data members consist of the data which the object needs. In our above example, the Point class' data members would be the x and y coordinates of that point. The function members of a class allow other parts of your program to manipulate your object. Your Point class could consist of functions to set the x and y coordinates of that point.

Classes are an essential element of C++ (they are what makes it an object-oriented language). A good understanding of classes is crucial if you are to use C++ effectively. Classes are used for these reasons:

· to organize programs into self-contained units (or modules);

· to protect data by controlling the functions which have access to that data.

Syntax

· syntax for defining a class

class [name]{

[data-member_type] [data-member_name];

...

public:

[function_name] ( [argument_type(s)] );

...

};

· syntax for defining a function

[class_name]::[function_name]( [function_argument(s)] ){

//code for what the function does

//can access data members directly here

}

· syntax for creating an object

[class_name] [your_name_for_new_object];

· syntax for calling an object's function

[object_name].[function_name]( [function_argument(s)] );

Example showing use of syntax

· Here is the definition for a class that stores and sets personal info.

class person{

char *name;

int telephone;

public:

person(); //constructor

person( char*, int ); //constructor

int change_tel( int ); //setter method

int gettel(); //accessor method

};

· Here are the definitions of the 2 constructor methods (or functions).

person::person(){

name = new char[1]; //reserve space (one byte) for "name"

*name = '\0'; //set "name" to NULL

telephone = -99;

}

person::person(char *n, int t){

name = new char[ strlen(n) + 1 ]; //explained in Notes

strcpy( name, n ); //set "name" to argument 1

telephone = t; //set "telephone" to argument 2

}

· Here are the definitions of the 2 other methods (or functions).

int person::change_tel( int new_t ){

telephone = new_t; //when called, this function sets

return 1; //the telephone number of the object

}

int person::gettel(){ //when called, this function just returns

return telephone; //the telephone number of current object

}

· Here is some code which creates a person object and then uses some of the person functions (this would appear in your main program).

person yori( "yori", 54543133); //this creates an object, "yori", of class "person"

yori.gettel(); //function call - will return "54543133"

yori.change_tel( 8222345 ); //function call - will reset "telephone" variable

//for object "yori"

Notes on syntax

· data and methods: Observe in the example the syntax for declaring data and methods of a class. The first declarations after the curly brackets are those of the data members (which we can also call the state variables). These are declared in the same way as any other variables in C++. Next comes the keyword public, and then the declarations of the methods (or functions). There is no keyword private in the example. C++ will automatically set the variables in this example to private (you may include the private keyword before the variables if you wish). It is possible also to declare private methods, but there are none in this example. A final note: it is possible to define methods at the point of declaration (which we have not done here). This is sometimes done with constructors if the code is very short.

· private and public: Class members may be declared "public" or "private". Data members should always be private. This means that the only way to access or alter a class's data is through the use of its methods - your main program cannot have a line such as telephone = 8222345 or yori.telephone = 8222345. Only code within the definition of the methods can access the private variables (as in the code for change_tel). This is known as information hiding. Because of this you will need to be sure that you define enough methods to access and set your data.

· constructors: Every class definition must include at least one constructor, and can include more than one. A constructor is a special method that is used to create an object (or instance) of a class. In the example there are 2 constructors: one that initializes the data members with null values, and one that accepts arguments and sets the data to the correct name and number. Note that the only way C++ distinguishes between the 2 constructors (which must always have the same name as the class) is through how many arguments are used in the line that creates the object. The appropriate constructor is automatically invoked by this line.

· objects: An object is defined as an instantiation of a class. Initializing a variable with the line int x instantiates x as an integer. In the example we can say that we have created "yori" as a "thing" following our specifications for "person". An object has some state (as defined by the data members), and an object can be manipulated through its class's methods.

· method calling: A class's methods can only be called via objects of that class. Your main program cannot call the change_tel function simply with the line change_tel. Note that one consequence of this is that you do not have to specify, in your call to change_tel, whose telephone number you wish to change. Since you call it through the object with the name "yori", it will be yori's number that gets changed.

· strlen, strcpy: See the Strings Tutorial. Here strlen is used in the line name = new char[strlen(n) + 1]. This line reserves space for the name variable of the current object. When using variables of type char* you must reserve space for them, using new, as soon as you know how much space you will need. In this case we use strlen to get the length of the argument n, adding one for the null character (which should be at the end of all string variables).

· file organization: Generally your main class definition (first point of example) should be in a header file (like, say, person.h). The code for your functions should be in a source file (person.C). It is preferable to then have your main program and any other class definitions in other files. So your creations of "person" objects and your calls to "person" methods will be elsewhere (like a file called program.C).