Tutorial 9

Class Example

Okay, let's get implementing our first class. First we pick something in the real world and then we will model it using a class that we design and write ourselves. For the sake of consistency let's use the shape class mentioned in the previous tutorial. We'll create a proper version of the class and also write the implementation behind the class and we'll even write a test program for the class.


First we must analyse the shape object that we wish to model. For our purposes let's make a square object. Now think about the different attributes and properties that a square can have. You'll notice that a square has a side length and that the length of each side in a sqaure is the same, this is good information. Also a square has a computable area, a diagonal and maybe even a way of resizing it (bigger or smaller). So let's write this class and it's implementation.


	// this code should appear in your square.h file
	class Square {
		public:
			// constructors
			Square();
			Square(int);
			
			// member functions
			int area();
			int sideLength();
			float diagonal();
			void resize(int);
				
		private:
			int side;
	};

There we have it the class itself and now we must write the implementation of this class so that we can use it in a program.



	// this code should appear in your square.cpp file
	
	#include <math.h>
	#include "square.h"

	Square::Square() {
		side = 0;
	}

	Square::Square(int sideLength) {
		side = sideLength;
	}
	
	int Square::area() {
		return side*side;
	}
	
	int Square::sideLength() {
		return side;
	}
	
	float Square::diagonal() {
		return sqrt(side*side*2);
	}
	
	void Square::resize(sideLength) {
		side = sideLength;
	}

There we have it, all the code that we need to implement our Square class, modularised and simply coded as a starting example. Every class you write from now on will have this basic layout and will consist of much the same structure only differing in what operations and data it contains. Now we want to use our class in a program to show that it works the way we want it to.


	// this code should appear in your main.cpp file
	
	#include <iostream.h>
	#include "square.h"
	
	void main(void) {
		// create two squares, 1 initialised the other not initialised
		Square	firstSquare(5);
		Square	secondSquare;
		
		// initialise the second square by resizing it
		secondSquare.resize(10);
		
		// print out some of the attributes of the squares
		cout << "The area of square 1 is: " << firstSquare.area() << endl;
		cout << "The area of square 2 is: " << secondSquare.area() << endl;
		cout << "The diagonal of sqaure 1 is: " << firstSquare.diagonal() << endl;
		// and so on for the rest of the functions
	}

Finished, we now have a complete implementation of our Square object that can be used to model real world squares. Also the benefit of modularising the code out in this fashion means that you can reuse the square code in other projects without having to hack it out of a single file. I don't really know what else I can tell you about classes that would be of any real advantage to you. If you have grasped this much then all you need do is practice with some examples of your own and get used to creating the classes and implementing them. There are other things involved in classes like protected sections, virtual functions, inheritance and other things. You'll get a good background about these with plenty of exmaples from a good C++ book but as for me, at the moment I'm planning a bit of a well earned break for myself having finished my degree. I'll be off and about for a while and maybe I'll take up where I left off on the tutorials in the Winter but as for now good luck with the programming and stick with it, it's fun!


Previous Index Tutorials