Welcome guest! Click here to register or login.
logo

<< Tutorial 11
Tutorial 13 >>

Classes & Objects I

***Please register FREE to rate this tutorial***


Topics
Basics
Constructors
Accessors
Mutators
Examples
Example 1
Example 2

Basics

In any programming language, there exists something called a class. By definition, a class is a collection of objects which can include functions, assessors, data variables, operator overloads etc...

A simple class structure is declared in C++ as follows:

class Name{ //where name is the name of the class
private:
	//data variables here

public:
	//all else here

};

To end a class, there must be a semicolon after the closing brace.

Constructors

All classes have something called constructors. Constructors must match the name of the class EXACTLY. They also have no return type (not even void. See example below for this demonstration).

Without a constructor(s) the class you are making will not exist. Think of a blueprint for a building. You are designing it carefully and when it is done, you need to construct the building. In a similar way, the class by itself is the blueprint containing all the appropriate information. Once you are ready to use it in the program, you need to construct the class, otherwise it does not exist.

Example 1:
Simple class I

Download source code here (Right click - Save Tagret As...)

#include <iostream>
using namespace std;

class Rect{

private:
	//representing the 2 sides of the rectangle.
	int s1, s2;

public:

	//constructors:
	//default constructor:
	Rect(){
		s1 = 1;
		s2 = 1;
	}

	//non default constructor:
	Rect(int a, int b){
		s1 = a;
		s2 = b;
	}

        //member functions:
	int Area(int s1, int s2){
		return(s1*s2);
	}

	int Perimeter(int s1, int s2){
		return(s1*s1 + s2*s2);
	}
};

int main(){

	Rect r(3,5);

	cout << "Area of sides 3, 5 is: "
             << r.Area(3,5) <<  endl;

	cout << "Perimeter of sides 3, 5 is: "
             << r.Perimeter(3,5) << endl;

	return 0;
}

As you will observe, the class is representing a simple rectangle which will calculate the area and perimeter of it. This is just a sample so you can see what is going on here relating to the constructors.

Notice in the main() function we declared the object r of type Rect. This uses one of the two constructos available (here, the non default constructor). Any constructor that takes more than zero arguments is considered an additional constructor and is not default. So according to the class, the two sides we give it are 3 and 5 (representing s1 and s2 respectively).

Had the other constructor been used (the default constructor), the 2 sides would have both been set to 1 (logically sound since a rectangle cannot have any side equal to zero). The reason it is called default is that the constructor does not take any arguments and simply sets the data variables of the class to logical initial values.

The Area() and Perimeter() functions are called member functions. See the next section for more details. Member functions were discussed in the functions tutorial (tutorial 5). Also note that the member functions are accessed with the dot operator ( . )

The output of the above program is:

Area of sides 3, 5 is: 15
Perimeter of sides 3, 5 is: 34

Accessors

Since we declare our data variables as private, they can only be accessed from within the class itself, not outside (say in the main() function). When you attempt to use the dot operator on a private variable you will get an error.

This problem can be solved with what is called an accessor. An accessor is a member function with the appropriate return type of the variable you are trying to access which will simply return the value of that variable. Let's add on to the previous example to see what an accessor is:

int getS1(){ return s1; }
int getS2(){ return s2; }

Notice that the variables are simply being returned and nothing is changing. This will allow the program to access the data variables from anywhere in the program.

Example 2:
Simple class II

Download source code here (Right click - Save Tagret As...)

#include <iostream>
using namespace std;

class Rect{

private:
	//representing the 2 sides of the rectangle.
	int s1, s2;

public:

	//constructors:
	//default constructor:
	Rect(){
		s1 = 1;
		s2 = 1;
	}

	//non default constructor:
	Rect(int a, int b){
		s1 = a;
		s2 = b;
	}

        //accessors:
        int getS1(){ return s1; }
        int getS2(){ return s2; }

        //member functions:
	int Area(int s1, int s2){
		return(s1*s2);
	}

	int Perimeter(int s1, int s2){
		return(s1*s1 + s2*s2);
	}
};

int main(){

	Rect r(3,5);

	int s1 = r.getS1();
	int s2 = r.getS2();

	cout << "Area of sides 3, 5 is: "
             << r.Area(s1,s2) <<  endl;

	cout << "Perimeter of sides 3, 5 is: "
             << r.Perimeter(s1,s2) << endl;

	return 0;
}

Notice that the only changes comes in the main() function. Here, we are declaring variables called s1 and s2 which will hold the values of the variables from that class. There is also no name conflicts between the variables since we are in the main() function.

The accessors make life a bit easier as we do not have to constantly hard code numbers each time when calling a member function.

Mutators

Mutators are opposite of accessors. Here, they are going to change the data variables in the class and not get them. All mutators are of type void since they do not return values. Also, they take one argument since that will be the new value of the variable.

Now, with mutators, you need to do some error checking. For the purpose of this class, a side of a rectangle cannot be zero or negative. So an if statement is appropriate checking for that. Let's see the two mutators:

//mutators:
void setS1(int s){
	if(s <= 0)
		return;

	s1 = s;
}
void setS2(int s){
	if(s <= 0)
		return;

	s2 = s;
}

These are going to be placed in the class right after the accessors. Here, we do the appropriate error checking with the if statement. You could also tell the program to exit(1) instead of return but then the entire program will terminate.

See the next tutorial for the continuation of the series. That will feature member functions and operator overloads.


<< Tutorial 11
Tutorial 13 >>