Anda di halaman 1dari 18

Data Types

Declaring variables
TYPE SIZE IN BYTES RANGE OF VA!ES
Bool (Boolean) 1 True or False
Char 1 By default, the same as type
signed char: -128 to 12!
"ptionally, you can ma#e
char the same range as type
unsigned char!
$igned char 1 -128 to 12
unsigned char 1 % to 2&&
'char(t 2 % to )&,&*&
short 2 -*2,)8 to *2,)
+nsigned short 2 % to )&,&*&
int , -2,1,,,8*,),8 to
2,1,,,8*,),
unsigned int , % to ,,2-,,-),2-&
long , -2,1,,,8*,),8 to
2,1,,,8*,),
unsigned long , % to ,,2-,,-),2-&
long long 8 --,22*,*2,%*),8&,,&,8%8
to
-,22*,*2,%*),8&,,&,8%
unsigned long long 8 % to
18,,,),,,,%*,%-,&&1,)1&
float , .*!,(1%.*8 'ith
appro/imately -digits
accuracy
dou0le 8 .1!(1%.*%8 'ith
appro/imately 1&-digits
accuracy
long dou0le 8 .1!(1%.*%8 'ith
appro/imately 1&-digits
accuracy
1eneral set up:
The set up for declaring 2aria0les is li#e so:
34aria0le types5 32aria0le name that 'e 'ant to ta#e that type5
i!e int 6ge: 'e created a 2aria0le called 6ge, under the type integer
setting up the initial 2alues of 2aria0les in t'o methods
1! 7ethod one ( e8ual method)
9nt 2alue : 1%;
2! 7ethod t'o (0rac#et method)
9nt 2alue(1%);
6dditional
1! <efining a long 2aria0le and setting it,
For long, you and an =, to the end of the num0er
For long long, you add an ==, to the end of the num0er
Character data type:
1! Char letter : >6?
2! Char letter(>6?)
@ou ha2e defined the char, as character and ha2e defined as letter
Ahen defining a letter, you need the 8uotes for the test
*! Char, can also ta#e num0ers
+sed for code for a single 6$C99 character
1! Achar(t: 'ide characters,
This allo's you to ha2e 'ider characters, i!e ta#e characters for Bapanese and other
characters!
1! +nsigned, 'hen you are setting the 2aria0le at the 0eginning you add a u
unsigned long mileage : %+=;
1! The Boolean type
Bool Color9sCed : true
B%ol Color9sCed (true)
1! Float to ha2e a certain amount of decimal points cur0ed
- 9ncluding e/ponentials
1!12&D2 : 1!12& E 1%F2
- Bloc# on the end
Float pi : *!1,1,-f;
<efining synonyms for data tyoes
@ou can ma#e a data type and ha2e it mean an e/isting data type to ma#e things easier
+se type"e#
i!e
typedef long int BigGum0er;
no' 'e ha2e
BigGum0er calculation1 : >num0er?=
=ong int calculation1 : >num0er?=
$e8uential input in cHH
$td::cin II num1 II num2
This is the se8uential input of data into the num0er!
9n command prompt or the data entry method, 'e ha2e num0er 1, only 'hen 'e enter the
space 0ar, and then enter the second num0er
Code is read from left to right
$e8uential output in cHH
$td::coutJJ num1 JJ num2
Code is read from left to right,
$o output is num1 and num2
+sing lines to add space
$td::coutJJ std::endl; ( this is a method of ha2e the output s#ip line)
e!g! 2ia e/ample
Coding so that you Koin data together, ha2e data on one line and ha2e data on separate lines
i) 6ll on one line
Main()
{
int num1,num2;
std::cout<<"enter the two numbers you would like to be joined toether";
std::cin!!num1!!num2;
std::cout<< std::endl;
std::cout<<num1<<" "<<num2 <<std::endl;
std::cout<< std::endl;
return ";
#
ii) La2e data on one line 'ith space
Main()
{
int num1,num2;
std::cout<<"enter the two numbers you would like to be joined toether";
std::cin!!num1!!num2;
std::cout<< std::endl;
std::cout<<num1<<" "<<num2 <<std::endl;
std::cout<< std::endl;
return ";
#
iii) La2e data on multiple lines
Main()
{
int num1,num2;
std::cout<<"enter the two numbers you would like to be joined toether";
std::cin!!num1!!num2;
std::cout<< std::endl;
std::cout<<num1 <<std::endl;
std::cout<<num2 <<std::endl;
std::cout<< std::endl;
return ";
#
+sing function:
Ahen using standard output procedures you can: call them 'ithout ha2ing to e/plicitly define
all the time!
i!e
Minlcude JiostreamI
!sing std::cout;
!sing std::endl;
!sing std::cin;
int main()
N
9nt Gum0er1!
CinIIGum0er1;
CoutJJnum0er1JJendl
Ceturn %;
O
O$tP$t F%r&atting
Ae use manipulators to format the output and ho' it comes out!
i!e
Ae ha2e a manipulator that creates a changed output!
i!e
using manipulators: come 0ac# page &2-&* i2or
Escape Se'$ence
This are special characters that 'e can input into the te/t so that they respond to an output,
i!e li#e ascii characters so that 'e can display #ey0oard functions online
EE Gote on use:
$he esca%e se&uence must be used like a strin, and be %laced within &uotation
marks'
i!e using the 0ee0 command
(include "stda)*'h"
(include <iostream!
(include <strin!
usin std::cin;
usin std::cout;
usin std::endl;
int +tmain(int arc, +$,-./0 ar123)
{
cout<< "time to beeb baby" << endl;
cout<< "beeb 1" << endl;
cout<< "4a" << endl;
cout<< "double beeb" << endl;
cout<< "4a 4a"<< endl;
return ";
#
i'e usin new line
(include "stda)*'h"
(include <iostream!
(include <strin!
usin std::cin;
int +tmain(int arc, +$,-./0 ar123)
{
cout<< "word 4n word 2 4n word5 "<< endl;
return ";
#
(alc$lati%ns
1! 6ssignment
Ae can ma#e li#e in 40a assign a 2aria0le to e8ual another 2aria0le
2! 6rithmetic "perations
Ae can use: E (multiple), - (su0tract), H (addition), P (di2ide)
*! 6ssignment and sign change
Ae can assign a 2aria0le to another
9nt a;
9nt 0;
6 : -B
Therefore if 6 : -1, then B :1
4ia e/ample: calculating 'hat you need for 'all paper
(include "stda)*'h"
(include <iostream!
(include <strin!
usin std::cin;
usin std::cout;
usin std::endl;
int +tmain(int arc, +$,-./0 ar123)
{
66 7e)ined 8ariables
66 de)inin the double and settin the 1ariable lenth to one d%
double heiht 9 "'", width 9 "'", lenth 9 "'"; 66 /oom dimensions
double %erimeter 9 "'"; 66 /oom %erimeter
const double roll:idth 9 21'"; 66 ;tandard roll width
const double roll<enth 9 12'"055'"; 66 ;tandard roll lenth(55)t')
66 de)inin the inteer, we will be indirectly con1ertin the double into an
inteer
int stri%s+%er+roll 9 "; 66 =umber o) stri%s in a roll
int stri%s+re&d 9 "; 66 =umber o) stri%s needed
int nrolls 9 "; 66 $otal number o) rolls
cout << endl 66 ;tart a new line
<< ">nter the heiht o) the room in inches: ";
cin !! heiht;
cout << endl 66 ;tart a new line
<< "=ow enter the lenth and width in inches: ";
cin !! lenth !! width;
stri%s+%er+roll 9 roll<enth 6 heiht; 66 ?et number o) stri%s %er roll
%erimeter 9 2'"0(lenth @ width); 66 ,alculate room %erimeter
stri%s+re&d 9 %erimeter 6 roll:idth; 66 ?et total stri%s re&uired
nrolls 9 stri%s+re&d 6 stri%s+%er+roll; 66 ,alculate number o) rolls
cout << endl
<< "Aor your room you need " << nrolls << " rolls o) wall%a%er'"
<< endl;
return ";
modi)yied 1ersion
66 ,onsole.%%lication2'c%% : 7e)ines the entry %oint )or the console
a%%lication'
66
(include "stda)*'h"
(include <iostream!
(include <strin!
66usin std::cin;
66usin std::cout;
66usin std::endl;
usin std::cin;
usin std::cout;
usin std::endl;
int +tmain(int arc, +$,-./0 ar123)
{
66 7e)ined 8ariables
66 de)inin the double and settin the 1ariable lenth to one d%
double heiht 9 "'", width 9 "'", lenth 9 "'"; 66 /oom dimensions
double %erimeter 9 "'"; 66 /oom %erimeter
const double roll:idth 9 21'"; 66 ;tandard roll width
const double roll<enth 9 12'"055'"; 66 ;tandard roll lenth(55)t')
66 de)inin the inteer, we will be indirectly con1ertin the double into an
inteer
int stri%s+%er+roll 9 "; 66 =umber o) stri%s in a roll
int stri%s+re&d 9 "; 66 =umber o) stri%s needed
int nrolls 9 "; 66 $otal number o) rolls
cout << endl 66 ;tart a new line
<< ">nter the heiht o) the room in inches: ";
cin !! heiht;
66Mod section, here we ha1e s%lit the lines and written the 1ariables
cout <<endl;
cout<< "Blease enter the lenth: ";
cin!! lenth;
cout <<endl;
cout<< "Blease enter the width: ";
cin!! width;
stri%s+%er+roll 9 roll<enth 6 heiht; 66 ?et number o) stri%s %er roll
%erimeter 9 2'"0(lenth @ width); 66 ,alculate room %erimeter
stri%s+re&d 9 %erimeter 6 roll:idth; 66 ?et total stri%s re&uired
nrolls 9 stri%s+re&d 6 stri%s+%er+roll; 66 ,alculate number o) rolls
cout << endl
<< "Aor your room you need " << nrolls << " rolls o) wall%a%er'"
<< endl;
cin!! heiht;
return ";
8ariable choice e*am%le
,alculatin a /eminder:
:e ha1e a %roblem where we ha1e calculate the amount o) items that can be
reasonably s%lit'
?eneral code
cout << "Blease enter the number o) hunry children: ";
cin!! children;
cout << endl;
cout << "Blease enter the number o) cookies a1ailable: ";
cin!! cookies;
cout << endl;
residue 9 cookies 6 children;
cout<< " the number o) cookies that are a1ialble )or each child is " <<
residue;
cin !!cookies;
return ";
1' /ounded to %roduce an inteer
int residue , cookies, children;
2' .n e*act calculation
double residue , cookies, children;
Modi)yin a 1ariable
i'e )or counters
i'e when e1er an e1ent occurs the number chanes by
lhs: left hand side 2aria0le
rhs: right hand 2aria0le
op: operations
i!e H, -, P, E
1! 1eneral rule 1
=hs op: rhs
8ia e*am%le
a) ,ount 9 ,ount @ 2
,ount @9 2
b) ,ount 9 count02
,ount 09 2
c) More .d1anced
. 9 .6(C @ ,)
.69 (C@,)
2' Dncrement and 7ecrement E%erators
;et u%:
,ount 9 ,ount @ 1; count @9 1; @@count
a) E% o% 8ariabe
Dteration )irst, then arument ne*t
i'e @@count
F total 9 @@count @ G
on the loo% )or increment this )irst adds the increment to count,
then adds G to )orm the total 1ariable
there)ore:
interation 1: count 9 H
on internation 2: count 9G , H @1
total 9 G @ G
b) 8ariable o% o%
.rument 1
st
then interation ne*t
- $otal 9 G @ count@@
>&ui1alent to
- $otal 9 G @ count;
@@count
:ith this we are addin the count 1alue to the total, then we
increment a)ter
,omma o%erator
$his is in res%ect to a 1ariable e&uallin a set a 1ariables
se%arated by comma'
i'e
numI 9 (num1 9 1"<, num2 9 2"<, num5 9 5"<)
numI 9 num5 9 5"<, the last 1alue in the se&uence
eneral note on assinin 1ariable 1alues
int 1ariable1(1"") : D de)ined a 1ariable as 1ariable1 and assined it with
the 1alue o) 1""
>&ually the same )or incrementin
<on num1(1"<), num2(2"<), num5(1""<), numI("<)
>&ui1alent to
<on num1, num2,num5, numI
=um1 9 1"<, num2 9 2"<, num5 9 1""<, numI 9 "
=umI 9 (@@num1, @@num2, @@num5);
>*am%le:
int chicken(J), %ork(H), answer(");
answer 9 chicken @ %ork;
cout << answer ;
cin !! answer ;
return ";
,astin o1er 1ariables
$his is the con1ertin o) one 1ariable to another
:orth notin
Dnt 1ariable(1"'KH) 9 1",
$akin an Dnteer o) an decimal at the outside, doesnLt round, but takes the
inteer 1alue o) it
double 1alue1 9 1"'KH, 1alue2 9 1H'J;
int added 9 static+cast<int!(1alue1) @ static+cast<int!(1alue2);
.uto keywords
$hese are e&ui1alent to 1ariant in a way, the auto )unction means that the c@@
com%iler %icks u% the 1alue o) the 1ariable and chooses the most suitable ty%e
that it should take'
auto n 9 1G; 66 $y%e is int
auto %i 9 5'1I1HM; 66 $y%e is double
auto * 9 5'H); 66 $y%e is )loat
auto )ound 9 )alse; 66 $y%e is bool
const auto e 9 2'K1J2J<; 66 $y%e is const lon double
const auto doNen(12); 66 $y%e is const int
auto )actor(n0%i0%i); 66 $y%e is double
7isco1erin ty%es
$y%eid(8ariable+Dn+&uestion)'name()
6utomatic 2aria0le, Declaring Variables )it*in Bl%c+s
Bloc#s defined as N O
Code e/ample:
(include "stda)*'h"
(include <iostream!
(include <strin!
usin std::cin;
usin std::cout;
usin std::endl;
int +tmain(int arc, +$,-./0 ar123)
{
int count1 9 1";
int count5 9 H";
cout << endl
<< "8alue o) outer count1 9 " << count1
<< endl;
{ 66 =ew
sco%e starts here'''
int count1 9 2"; 66 $his hides the
outer count1
int count2 9 5";
cout << "8alue o) inner count1 9 " << count1
<< endl;
count1 @9 5; 66 $his a))ects the
inner count1
count5 @9 count2;
# 66 '''and
ends here
cout << "8alue o) outer count1 9 " << count1
<< endl
<< "8alue o) outer count5 9 " << count5
<< endl;
66 cout << count2 << endl; 66 uncomment to et an error
cin!! count1;
return ";
# 66 Aunction sco%e ends here
=ist of outputs and e/plained:
2alues returned 0ased on code:
output 1: 1%
output 2: 2%
output *: 1%
output ,: 8%
"utput 1: is self e/planatory, this is an output in the outter loop and 0efore any
manipulation has 0een made
"utput 1: is again self e/planatory this is an ouput that in the inner loop and hasent
0een manipulated!
The inner loop, the 2aria0le has 0een defined deferently, 'e can loo# as the intter
0rac#et as an alternati2e
"utput *, 'e return to the outter 0rac#et, and as in the outterloop, the original su0
procedure nothing has happened to the 2aria0le count1, so the 2alue remains 1%
"utput ,: is 8%, this is 0ased on the calculations in loop and outer loop
The 2aria0les inside and outside the 0races can interact if the 2aria0le isn?t redefined
and set in the innter loop,
Lere on the outter 0race, the count * : &%,
6s count * in the inner 0race is not defined init,
The relationship stated is count * : count * H count 2
Ahere count 2 'as redefined as *%,
$o the 2aria0le solutions passes through 0oth 0races and the is output
EEE loo# into: a stac# is a portion of the code in 0races, so here 'e ha2e t'o stac#s, the
innter and outter 0races! Lere 'e can defined 2aria0es
The stac# siQe at ma/ can contain 1m0 data!
This can 0e e/tended 0y Pstac#!!
(%"ing practise, p%siti%ning variable "eclarati%ns
Rositioning 2aria0le declare close to use so ppl reading the code #no' 'ats going on
+se 1:
To use glo0al 2aria0les 'e define out of the main N O 0races
9n the same place as Minclude JiostreamI
+se 2:
Ae can declare local 2aria0les, 2aria0les 'ithin the main N,!!O, 'ith the same name as the
glo0al, this 'ill o2erride the glo0al 2aria0le,
To output and use the glo0al 2aria0le in the case 'here it is o2er ridden internally 'e use ::
D/ample output
(include "stda)*'h"
(include <iostream!
usin std::cin;
usin std::cout;
usin std::endl;
int count1 9 1""; 66 ?lobal 1ersion o) count1
int +tmain(int arc, +$,-./0 ar123)
{ 66 Aunction sco%e starts here
int count1 9 1";
int count5 9 H";
cout << endl
<< "8alue o) outer count1 9 " << count1
<< endl;
cout << "8alue o) lobal count1 9 " << ::count1 66 Arom outer
block
<< endl;
{ 66 =ew sco%e starts here'''
int count1 9 2"; 66 $his hides the outer count1
int count2 9 5";
cout << "8alue o) inner count1 9 " << count1
<< endl;
cout << "8alue o) lobal count1 9 " << ::count1 66 Arom inner
block
<< endl;
count1 @9 5; 66 $his a))ects the inner
count1
count5 @9 count2;
# 66 '''and ends here'
cout << "8alue o) outer count1 9 " << count1
<< endl
<< "8alue o) outer count5 9 " << count5
<< endl;
cin!! count1;
66cout << count2 << endl; 66 uncomment to et an error
return ";
# 66 Aunction sco%e ends here
D/ample Calculation
(include "stda)*'h"
(include <iostream!
usin std::cin;
usin std::cout;
usin std::endl;
int count1 9 1""; 66 ?lobal 1ersion o) count1
int +tmain(int arc, +$,-./0 ar123)
{ 66 Aunction sco%e starts here
int count1 9 1";
int count2 9 5";
int count5;
count5 9 count1 @ count2;
cout << endl;
cout<<" calculation one local 1ariable "
<< count5 << endl;
count5 9 ::count1 @ count2;
cout<<" calculation one lobal 1ariable "
<< count5;
cin!! count1;
return ";
#
En$&erati%ns
Dnumerations are set 2aria0les that cHH recognises, that are assigned num0ers
C"7D B6CS T"
Na&eSpaces
Gamespace: This is a region that you define 2aria0les 'ith a particular components of your
code,
@ou do this 0ecause it means that the code is less confusing it many
programmers are coding on a large proKect!
@ou can then call these 2aria0les from this uni8ue namespace!
Characteristics:
1! The namespace is defined outside the main procedure, li#e 'ith glo0al 2aria0les
2! Aho to form
Na&espace -Na&e %# na&espace .
N
=ist of declared 2aria0les
O
*! 9n namespaces you can only define 2aria0les, it is affecti2ely a 2aria0le defining
Qone,
Therefore you cant do
4aria0le* : 2aria0le 1 H 2aria0le 2
6nd ha2e that output in the main function!
Lo' to use:
i) To use the 2aria0le in the namespace in the main, e/ecution code, 'e ha2e to :
-na&e %# na&e space.,,-variable na&e.
ii) 9f you 'ant to mainipulate these 2aria0les in the name space and calculate
4aria0le* : 2aria0le 2 H 2aria0le1
Ae need to define a local 2aria0le * and e8uate it to the name space 2aria0les
EE remem0er cHH reads code se8uentially, therefore to code and reads it
se8uencially, therefore define 2aria0les 0ut 0efore this ha2e the input feed into
the 2aria0le first!
iii/ !sing Na&espace -Na&e %# na&espace .
<efining this 0efore the main is run, it allo's you to ha2e the 2aria0les defined in the
name space and ha2e the 2aria0les used in the main function, 'ithout ha2ing to use
D/plicit: 3name of name space5::32aria0le name5, any more
Therefore if 'e ha2e multiple namespaces, and use the using, as long as 2aria0le
names don?t conflict e2erything is good!
Ae can also e/plicityly define some 'ith e/plicit
D/ample
(include "stda)*'h"
(include <iostream!
(include <strin!
usin std::cin;
usin std::cout;
usin std::endl;
names%ace ,alculation7etails
{
int .eCe)oreMarriae 9";
int .e=ow9";
int OearsDn/elationshi% 9";
const int .1erae<i)e;%an 9 J";
const int Oear 9 2"15;
66double Oears$other 9 OearF(Oear F .eCe)oreMarriae);
double BercentaeE)<i)e$oether9(.1erae<i)e;%an F
.eCe)oreMarriae)61"";
#
names%ace ,alculation7etails2
{
int .e.tMarriae 9";
int OearsDnMarriae 9";
const int .1erae<i)e;%an 9 J";
double BercentaeE)<i)e$oether9(.1erae<i)e;%an F .e.tMarriae)61"";
#
int +tmain(int arc, +$,-./0 ar123)
{
int Oears$other;
66de)inin the 1ariables to be used in the sub%rocedure
cout<< " :hat ae did you )irst et toether ";
cin!! ,alculation7etails::.eCe)oreMarriae;
cout<<endl;
cout<< " :hat ae are you now ";
cin!! ,alculation7etails::.e=ow;
cout<<endl;
Oears$other 9 (,alculation7etails::.e=ow F
,alculation7etails::.eCe)oreMarriae);
cout<< " Oou ha1e been toether )or " << Oears$other << " years";
cin!! ,alculation7etails::.eCe)oreMarriae;
cout<<endl;
cout<< ,alculation7etails::BercentaeE)<i)e$oether <<endl;
return ";
#
T*ings t% searc* int,
- <irecti2es
- 7ore escape se8uences
- "perator precendence page )&
- Come 0ac# scii
- $tatic 2aria0le : pg 8,

Anda mungkin juga menyukai