Tutorial 8

Let's Get Classy

Okay you either skipped the last page or you've read through it and now you are here trying to find out what the trickier stuff is that I mentioned. Well we are now going to talk about classes and Abstract Data Types (ADT's). Take the C++ language as it stands and have a good look at it and then try to model anything from the real world with it's current objects. Provided you have an imagination I presume that you came up with something that has much more than one attribute. You could model this multi-attribute object in a program by using many variables but wouldn't it be much easier if you had one object to cover all the variables needed?


It is possible to do this. There are two ways, Classes and structs. A struct is a lesser form of a class and we are not going to talk about it on this page. By designing your own class and giving it all the member variables that it needs to describe its associated real world object we can declare many of these real world objects in a program without confusion. We can also write member functions of the class that allow us to manipulate the variables of the class and process other information too.


How??

Okay you've learned how to write functions and that's the implementation side of your class looked after, more or less. What you now have to learn is how to create your own class and what better way than to give an example of a class for you to examine. Below is a class that models a square. You'll also learn how to modularise your code so as it make it easier to maintain and easier to read.



		class Square {
			public:
				// constructors
				Square();
				Square(int);
			
				// member functions
				int area();
				int sideLength();
				void resize(int);
				
			private:
				int side;
		};

Okay, what do you see? First of all this unusual structure has two sections a public one and a private one. All things in the public section of the class can be accessed directly through the class but anything declared in the private section cannot. What??? Basically what this means is as follows, the syntax for accessing items through a class is as follows classname.itemname, if an item is public then you can do this, if it is not then you cannot! Simple as that. Private members can only be manipulated through public functions and not directly.


That might sound a bit odd if this is your first introduction to objects but as usual things are made better by the use of examples and if you continue on to the next tutorial then you'll see how we can create and use a class to model a real world situation in a program.


Next we learn how to modularise your code. This is really very simple and only involves the good coding structure that I'm sure you've adopted at this stage :-) and splitting your code into several different files that contain sections of code that can easily be separated in this fashion. When writing object-orientated code such as that with classes the norm is to split your code up into two different files. When you write a fully implemented class you will have two sections of code, the class definition itself and also the implementation code behind the class. These two sections should appear in two different files. Put the class definition in a .h file named by the name of the class e.g. sqaure.h and put the implementation code in a .cpp file again named by the name of the class e.g. square.cpp, this will separate out your code. When you have these two files you'll need to link them somehow. In your square.cpp file make a #include for the square.h file, this will be clearly shown in the following tutorial, and in the main.cpp file where you create objects of the type of your class #include the square.h file and off you go. Remember to include these files into your project if your using some sort of ide like Borland or Microsoft Visual C++.


Previous Index Next