C++ Tutorials



Tutorials > C++ > Classes

View Full Source


Welcome to the future. Almost everything these days is being created using classes. You have seen in the past how structs can be used to create a seperate data type consisting of a number of variables and functions. Classes are similar to this except that they offer many more features.

Classes allow you to encapsulate data into one object. Functions contained in classes are known as methods and whenever you create a variable using a class, it is known as an object. This is the core of object orientated programming.

Classes are created in the following way :

class class_name { methods and variables };

Notice that there is a ; at the end of the class definition.

Instead of trying to explain how classes work, we will move onto an example which will hopefully make things easier.

Contents of main.cpp :

#include <iostream>
#include <stdlib.h>

using namespace std;

First, we create a Player class.

class Player

Next, we have the public keyword followed by a :. For now, all you need to know is that you need this keyword as it specifies that any data that follows can be accessed via an object once created. This will be explained in greater depth in a future tutorial.

public :

We can then create some variables and methods as per usual. Notice that we give the body for the first method but we do not give the body for the second. This is similar to tutorial 20 where we declared the function at the top and gave the body further down.

The first method is known as an inline method. This means that whenever this method is called, the code is directly replaced internally allowing quicker execution. This is however only used for small methods consisting of 1 or 2 lines. Any larger methods should have their bodies placed in a separate location.

	int health;
	int mana;

	void attack()
		cout << "Player Attacks" << endl;

	void die();

The next step is to give the body of the die method declared above. To indicate that the method belongs to the Player class, a Player:: must be placed before the name of the method.

void Player::die()
	cout << "Player dies" << endl;

Creating a Player object works in a similar fashion to that of a struct variable.

int main()
	Player fred;

The methods and variables can now be accessed if they are in the public section of the class. We have put everything in the public section. = 100;
	cout << "Health : " << << endl;

If we have a pointer to a Player object, we need to first dereference the pointer to be able to access its methods eg.

	Player *sally = new Player;

This is a pain to type and things can look quite complex with a lot of these statements. Another operator is available to make this simpler. The -> operator can be used. This dereferences the pointer and accesses the object. Th


	delete sally;


	return 0;

Well done. You have begun your journey into object orientated programming. Classes form the foundation of OO and will see a lot of them in the future.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++ Unix / Linux

< Tutorial 45 - Function Templates Tutorial 47 - Constructors >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer