FSnC Forum
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
May 23, 2012, 04:39:07 PM
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
[2:58:23 AM] Moonie: alien truck drivers running pawn shops
[2:58:31 AM] Forte: ...
[2:58:36 AM] Forte: this sums up the history channel
[2:58:38 AM] Forte: perfectly
Search:
Advanced search
Forte's Sprites n' Comics
FSnC Forum
Normal People Things
General Notspam
C++ Tutorial Topic R2
Pages:
1
[
2
]
Go Down
« previous
next »
Author
Topic: C++ Tutorial Topic R2 (Read 873 times)
0 Members and 1 Guest are viewing this topic.
Death Dark
Abaddon
Pig Mask Commander
Hats: 3
Offline
Awards:
Xbox Live Gamertag: The Death Dark
Steam ID: Darksteel_Colossus
Posts: 5972
Master of the Unfinished.
Re: C++ Tutorial Topic R2
«
Reply #15 on:
May 04, 2009, 04:53:03 PM »
=====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
"Leave it to you to troll a porn star." -- Sepuku
xuwenli200888
Guest
Re: C++ Tutorial Topic R2
«
Reply #16 on:
November 03, 2010, 08:53:02 PM »
What.
«
Last Edit: November 03, 2010, 09:10:20 PM by Forte
»
Logged
Pages:
1
[
2
]
Go Up
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Issues of National Importance
-----------------------------
=> Site Announcements
=> Forum Announcements
-----------------------------
Normal People Things
-----------------------------
=> General Notspam
=> Technical Support
=> Badge Based Posting Simulator
=> Introductions
-----------------------------
Text Based Adventuring
-----------------------------
=> General Role Play
-----------------------------
Vidya Gaems
-----------------------------
=> Nintendo
===> Brawl+
===> Nintendo WFC
===> Animal Crossing: City Folk
===> Pokemon
=> Microsoft
=> SOPA lite
===> PSP
=> Everything else
===> Map Development
-----------------------------
Artsy People's Arts n' Crafts
-----------------------------
=> Sprites
===> Sprite Comics
=> Backgrounds
=> Artwork
===> Sigs and Avatars
=> Requests
=> Stories/FanFics
=> Coding
=> Fan Games
-----------------------------
Mainstream
-----------------------------
=> Movies
=> Music
=> TV
-----------------------------
What About the Website?
-----------------------------
=> Site Discussion
=> Advertise and Affiliate
Our affiliates
Loading...