gfxgfxFSnC Forumgfxgfx
gfx gfx
gfx
Welcome, Guest. Please login or register.
Did you miss your activation email?
February 12, 2012, 04:23:27 PM

Login with username, password and session length
TheDudeFromNowhere: "Can I get that art I requested forever ago now?"
/FSnC\ Fenril: = w ='
/FSnC\ Fenril: wait
/FSnC\ Fenril: which art
TheDudeFromNowhere: boktai one
/FSnC\ Fenril: what's a boktai
TheDudeFromNowhere: one with django and otenko
/FSnC\ Fenril: what's a django
/FSnC\ Fenril: it sounds like a black tap dancing legend
gfx
gfx
*
gfxgfx
gfxgfx gfxgfx
Search:     Advanced search
gfxgfx Home Help Calendar DownloadsChessFlashChat Login Register   gfxgfx
gfx gfx
gfx
Forte's Sprites n' Comics
Pages: 1 [2]   Go Down
Print
Author Topic: C++ Tutorial Topic R2  (Read 818 times)
0 Members and 1 Guest are viewing this topic.
Death Dark
Abaddon
Pig Mask Commander
*

Hats: 2
Offline Offline

Awards:
<3 Pink HairElder Scrolls FanPhantasy Star Online FanChosen by C++ to get a badge
Xbox Live Gamertag: The Death Dark
Steam ID: Darksteel_Colossus
Posts: 5942
13395.00 DP

View Inventory
Send Money to Death Dark

Master of the Unfinished.


« Reply #15 on: May 05, 2009, 03:53:03 AM »

=====Lesson 3:  More on Variables=====

===Characters===

In C++, the basic text storage is the char data type.  Although you can only store a single character, sometimes that all you need.

Code:
char gender = 'M';

It's important to remember to keep the character in single quotes, which indicates that it is not only a single character, but that it's also not a variable.

We'll do a little more with these in a bit, but keep them in mind; they're useful.


===Boolean===

A boolean value is a value of true or false.  This variable does a lot more than you may think at first.  Usually it's used to mark things, since it takes up nearly no memory (it could be stored in 1/8 of a byte if such a small section of memory were available).  As we move along, you'll recognize it.

To use a boolean variable, do something like this:

Code:
bool male = true;

The keywords true and false are both reserved in C++ for boolean operations.  Just like you can type out a 3 as a literal integer value, 'J' as a literal character value, and "BLAH" as a literal string value, you can type out true as a literal boolean value.

=Logical Operators=

Boolean variables have special operators that we can use to compare the values.

|| - OR - Is true when either operand is true
&& - AND - Is true when both operands are true
! - NOT - Is true when operand is true, and false when operand is false

These operators give different outputs based upon the inputs.  The || and && operators are like + and -, they need a value on both sides.  However, ! is specifically like a negative operator, and goes directly before the value.


=Comparison Operators=

There are some operators that give boolean values as a result, even though they may not have boolean values as operands.  These are comparison operators, and you've probably seen them before.

== - equal to
< - less than
> - greater than
<= - less than or equal to
>= - greater than or equal to
!= - not equal to

What happens here should be obvious.  You give the C++ a statement like 3 > 5, and it spits out a value of false.


===Type Casting and Automatic Conversion===

Both bool and char values have important properties of them that concern the C++ programmer.  Let's begin with bool.


=bool Conversion=

Boolean values are true and false by default, however bool also accepts integer values.  When interacting with int, bool has a value of 0 or 1.  Well, not just 0 or 1, but put simply, 0 is false, and everything else is true, though 1 is usually used.


=char Conversion=

Characters are stored in memory using ASCII values.  These values are numbers from 0 - 255 that signify what character is being used.  Since it is stored this way, an int with a value from 0 - 255 can be used to set a character, and vice versa.  For a full list of ASCII characters and their values, consult Wikipedia.


=Automatic Conversion=

As covered in the previous lesson, ints and doubles automatically switch between each other.  However, there are a few more notes to mention.

When dealing with two different types of variables in the same operator, C++ automatically converts the lower order into the higher order.  The order is pretty simple:

bool < char < short < int < long < float < double < long double

So, double + bool will convert bool all the way up to double, giving it the value of 0 or 1.  There are some cases, like when calling functions, that this doesn't happen, but you can count on it for the operators mentioned already.


=Type-Casting=

So, what do you do if you have a character that you want to be an integer for a second.  Well, you could:

Code:
char var1 = 'A';
int var2 = var1;

However, that takes up space in memory.  Instead, let's do this:

Code:
char var1 = 'A';
static_cast<int>(var1);

static_cast is a built-in function that takes the value given and temporarily turns it into the type given in angle brackets.  There are two other methods of doing this, but they may not be supported for long.  Going with static_cast is a good idea.


===Outputting Variables===

Variables and values can be output just like string literals.  Really, all you need to do is

Code:
char gender = 'M';
cout << "The gender is " << gender << endl;

Remember to include a << for each new thing you want to output.  In this case, the following will print to screen:

Quote
The gender is M


Each of the different variable types output differently to screen.

=Numeric Data Types=

All of the numeric data types display as you would think they do.  Their sign, if negative, is shown first, then their value, followed by a decimal point and extended values if needed (and only if a type that can store it).


=char=

char outputs as the character represented by the ASCII values.  Non-printing characters will print their results, and extended ASCII characters (non-keyboard characters) will print normally.

While we're on the subject on characters, in the previous code given, a variable endl was used.  That variable is defined in the IOStream library, and is set to the new-line value.  Pretty much, it's set to make a new line when it's displayed.


=bool=

bool outputs as either 0 or 1 as explained earlier.


=Operations=

Instead of storing values of operations, they can simply be displayed instead.  Take this example:

Code:
double pi = 3.14,
       r = 20;
cout << "Circumference:  " << 2 * pi * r;

In such situations, the operations will be resolved and the final value will be output to screen.


===For Next Time===

Play with variables.  Next time we'll discuss input.  Yay, input.
Logged

Quote from: lern
Quote
xuwenli200888
Guest
« Reply #16 on: November 04, 2010, 07:53:02 AM »

What.
« Last Edit: November 04, 2010, 08:10:20 AM by Forte » Logged
Pages: 1 [2]   Go Up
Print
Jump to:  

MegaMan Topsites

Our affiliates

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines
Cerberus design by Bloc
Valid XHTML 1.0! Valid CSS!
gfx
gfxgfx gfxgfx