Sherman3D
Google  
Web Sherman3D    
2. Introduction to C++
Your premier to the C++ programming language.
on Wednesday 06 October 2004
by Sherman Chin author list print the content item
in article > C++ Programming

In this tutorial, we will be taking a look at the basic concepts of C++, which includes objects and classes, and even delve into our first C++ program.

In today's tutorial, we will be taking a look at the basic concepts of C++ and even delve into our first C++ program. We would not be covering each and every boring syntax of C++. Instead, we would be taking the less painful way of learning C++. It is my intention that this tutorial be fun to read so if you would rather understand every single aspect of C++, then go get any renowned C ++ textbook.

There are no prerequisites to this tutorial but a firm grasp of computer usage will be very useful. I have designed this tutorial so that a complete newbie should be able to get along fine. Nevertheless, if you want to master the language, please buy a C++ reference book.

Let us work with a program as our hands on exercise throughout this article. We begin our C++ program by typing the '#include' C++ keyword followed by the header file to include. Make sure the header file name is enclosed in '<' and '>'.

#include < iostream.h>


Header files provide our C++ program with the basic commands that we can work with such as the command to display something on the screen. Here, we have included the iostream.h file that is provided along with your C++ compiler. This header file that we link to our program enables us to use commands that work in console text mode (such as displaying those basic white text on the black DOS background).

It is obligatory that I mention C++ is the successor of the C language and thus, is the superset of it. What this means is that any well-known C++ compiler will also compile C code. One important concept that C++ implements to differ itself from its predecessor is Object Oriented Programming (OOP). If you have programmed in procedural languages such as C before, then OOP might seem quite a different paradigm to grasp but this tutorial will try to make it as interesting as possible.

At this moment, two elements that we must bother with OOP are class and object. A class is a template or blueprint of which we use to create an object. Think of a class as a cookie cutter that we use to shape our shapeless dough to make nice-looking yummy cookies. An object is an instance of our class (what we create with the use of our class). Think of an object as the cookie we make with our cookie cutter.

It is important to remember to think in terms of classes when designing a program in OOP. Classes are identifiable as English "nouns" when we think of our program. Named instances of our classes are objects. This might seem confusing but continue on...


Now, assume we want to create a role-playing game (RPG) with at least one character. Thinking in terms of OOP, we can easily identify our classes by looking at the nouns in the previous sentence. The words "RPG" and "Character" are both nouns. But we want to create an object that will actually exist in our game so "Character" is our chosen candidate, as our game will surely consist of at least a single character. Let us start coding our generic Character class. Later on, we can declare objects using our Character class i.e. an actual character named Adam.

#include < iostream.h>
class Character
{
};


Notice that we start with the C++ keyword 'class' followed by our user-defined (that means we can name it almost anything we want) name. Follow that with a set of braces and a semicolon.

We use the Character class to represent generic characters who are of not much importance in the game and also to serve as a parent class to our more specialized classes such as our Fighter and Wizard class. So, all characters in our game should have a name, a level, hit points and experience points. We can write the name with a string of characters. Level, hit points and experience points, which are all numbers, can be written as integers.

#include < iostream.h> 

class Character
{
private:
    char name[25];
    int level;
    int hitPoints;
    int experience;
};


Okay, let us take a quick look at the syntax of our program. We have added the keyword "private:" to indicate that these are private data members of our class. This simply means that the following data members are not directly accessible from outside the class. Do not worry if you are a little confused, just type the keyword "private:" and continue on with the program.

Let us jump to the "int level;" first. In C++, when we want to declare a data type, we use the syntax "dataType userDefinedName; ", thus we have a variable that can store integers. We use the keyword "int" to declare that we want the variable we named "level" to hold integer data type. A variable is simply a space located in the computer's memory to hold different types of data depending on how it is declared. It is named a variable because the data held in the memory location that we declared could vary throughout the program as long as it remains the same data type. For example, the level of the characters can increase as they gain more experience. However, the allocated space in memory will still be for integer data type and as the integers will not mysteriously change to characters. Since the variables "hitPoints" and "experience" are declared as int data types, they contain varying integers throughout the program.

Now, let us look at the "name" variable. It is declared as a "char" - character variable data type. However, we notice the [25] following the variable name. The data-type "char" can only store a single character (please do not mix this up with our Character class.) such as, either 'a', 'b', '1', '2', '$' or '@'. Nonetheless, we need a string of characters such as the name 'Adam' which has 4 alphabets. Thus we allocate a space for a maximum of 25 characters for our name using the declaration "char name[25];". Those familiar with programming can think of this as an array of characters. Sorry, but C++ does not have a standard "string" data-type as of yet. However, veterans may want to check on the String class in C++'s STL (Standard Template Library) which works like JAVA's String data-type.


Subsequently, we are going to assume that all our game characters can walk and talk. So add to the code the member functions of the class needed to implement these operations:
#include < iostream.h>

class Character
{
private:
char name[25];
int level;
int hitPoints;
int experience;
public:
void talk(void)
{
// We have to write the code to make the character talk here.
}
void walk(void)
{
// We have to write the code to make the character walk here.
}
};

The keyword "public:" is to indicate that these are public members of our class. It simply means that these member functions are directly accessible from outside the class. As before, do not worry if you are a little confused, just type the keyword "public:" and continue on with the program.

Our function syntax is as follows: "returnType userDefinedName(dataType userDefinedName, dataType userDefinedName, ...) { } ". The return-type is the data-type that we might want to return to the caller of this function. We will see how this works in future tutorials. For now, just type the keyword "void" as our return-type. "void" is a special data-type that means "nothing" so this function returns nothing. Our user-defined name of our function is followed by a set of parentheses. The parentheses contain the type of parameters that we want to pass to our program such as "int hitPoints". However, to keep our discussion simple, we simply pass the parameter "void" to our function. Next, the function statements are enclosed in braces. There is no ending semicolon for the function. I have also used the C++ keyword "//" to indicate the start of a single line comment. We can write anything on that line following the "//" to help others understand what we are trying to do with our code.

Okay, let us make our character actually say something that will be displayed on the screen:

#include < iostream.h> 

class Character
{
private:
char name[25];
int level;
int hitPoints;
int experience;
public:
void talk(void)
{
cout << "Hello! I am a character.";
}
void walk(void)
{
// We have to write the code to make the character walk here.
}
};

In our code, we have removed the comment and added the statement cout << "Hello! I am a character"; All statements in C++ must end with a semicolon if you noticed by now. The "cout" keyword and the "<<" steam operator are already declared in our include file, iostream.h. In short, the code to make them work has already been supplied with your compiler and we have made them available to our program by using the "#include " statement. By the way, the "#include" statement is a special statement in C and C++ that does not require the semicolon to end it. The statement is known as a preprocessor directive.


Back to our program, we have defined our class but have not yet defined an object as an instance of the class. In other words, we have a cookie cutter with no cookies. We should then add our objects. Prior to declaring our objects, we need a main function that is separate from our class:

#include < iostream.h>

class Character
{
private:
char name[25];
int level;
int hitPoints;
int experience;
public:
void talk(void)
{
cout << "Hello! I am a character.";
}
void walk(void)
{

// We have to write the code to make the character walk here.
}
};

void main(void)
{
}

The main function is just like any normal member function of the class. It is special in the sense that the main is not a user-defined name but a C++ keyword. Every program in C++, or C for that matter must have the main function. The main function is where you control the flow of the program. Program execution begins automatically from the main function. In short, the main function is where it all begins.


Let us create an object of the Character class in the "main" function. Name the object "adam". We will then call the member function talk() from the Character class using the object "adam".
#include < iostream.h>
class Character
{
private:
char name[25];
int level;
int hitPoints;
int experience;
public:
void talk(void)
{
cout<< "Hello! I am a character.";
}
void walk(void)
{
// We have to write the code to make the character walk here.
}
};

void main(void)
{
Character adam;
adam.talk();
}

To declare an object of type Character we use the class name followed by a user-defined name. This is very much like declaring a built-in variable type, such as "int hitPoints;" As you can see, it is quite simple declaring our objects. So now we have our cookie, so to speak. If you want to create more instances of the Character class, you can do something like this.

Character eve;


In the next C++ statement, we have the member function call preceded by a period and the variable name. The period is known as the dot operator or the access operator and is used to access the members of the class of which the variable preceding it belongs to. The adam.talk() statement will be the program statement that causes the talk() function in our Character class to be called and the output to be displayed on your computer screen. You will see "Hello! I am a character." being displayed on your monitor. You can also add:

eve.walk();


Of course the above statement would not make Eve walk because we have not added any code to the member function yet. Let me digress a little here and explain why we used keyword "public" before. Notice that we are calling the member functions "talk" and "walk", which are public member functions. We can do this even when we are calling the functions from the "main" function that is not a member function of the class. However, if we had declared the functions as private, we would only be able to call them in another member function of the class and not from outside the class as we are doing here.

We have learned quite a lot today. We have seen the basic structure of a C++ program and some C++ keywords. We have also learned some OOP concepts such as class and objects. You now know that classes have member data and member functions. Member data is usually kept private while member functions are usually public.

That is about it for this tutorial. Please key in the code above into any C++ compiler and compile as well as run it to see the result. You should see the words "Hello! I am a character." appearing on your command line. I know that this is a far cry from a full-fledge game but we will get there some day and this is just the beginning. In our next tutorial, we will be seeing how to code our Fighter and Wizard class so stay tuned.

Link to us
Link to us
Warning!
Stats Logging plugin has to be installed for this to work
Welcome
Username or Email:

Password:




[ ]


































Copyright © 2003-2012
Sherman3D

All intellectual property and graphics are strictly protected by Sherman3D.

Please do not redistribute without prior consent.

Thank you.