Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Variables

One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a named location that stores a value. Just as there are different types of values (string, floating-point, integer, character, etc.), there are different types of variables.

When you create a new variable, you have to declare what type it is. For example, the string type in C++ is called string . The following statement creates a new variable named fred that has type string:

string fred;

This kind of statement is called a declaration. The type of a variable determines what kind of values it can store. A string variable can contain strings, and it should come as no surprise that int variables can store integers. To create an integer variable, the syntax is:

int bob;

In C++, there are two floating-point types, called float and double. In this book we will use doubles exclusively. The syntax for a floating-point variable is:

double mike;

To declare a character variable you suppose to use keyword int:

char sam;

W here fred, bob, mike and sam are just arbitrary names you made up for the variable to identify it. In general, you will want to make up variable names that indicate what you plan to do with the variable. For example, if you saw these variable declarations:

string sFirstName;

char cFirstLetter;

int iHour, iMinute;

double dSeconds;

you could probably make a good guess at what values would be stored in them. In this example the first small letter (s, c, i or d) indicate a variable type and the remaining part says something about variable meaning (note: this is only a convention for this book, not a general feature of C++). This example also demonstrates the syntax for declaring multiple variables with the same type: iHour and iMinute are both integers (int type).

You have also seen values like true and false . These are actually boolean values and there is a special type of variables called bool for them. We will learn about this type later.

C++ is generally considered a “loosely typed” language, unlike languages such as Ada which has much stricter type syntax. However, even though general C++ is somewhat forgiving with regard to variable types, and the WCM C++ implementation even more so, if your intent is to progress to general purpose programming, it is very important that you understand the concept and implications of the “type” concept.


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll