0% found this document useful (0 votes)
702 views4 pages

Just A Brief Introduction To C++ Data Types PDF

This document provides an introduction to C++ data types. It describes fundamental data types like integers, characters, floating-point numbers, Booleans, and enums. It also discusses user-defined types using classes. Each data type has specific characteristics like its range of possible values and the operations that can be performed on variables of that type. Common integer types include short, int, long, signed, and unsigned variants. Floating-point types include float, double, and long double in various precisions. Classes allow users to define their own complex data types.

Uploaded by

ariwangga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
702 views4 pages

Just A Brief Introduction To C++ Data Types PDF

This document provides an introduction to C++ data types. It describes fundamental data types like integers, characters, floating-point numbers, Booleans, and enums. It also discusses user-defined types using classes. Each data type has specific characteristics like its range of possible values and the operations that can be performed on variables of that type. Common integer types include short, int, long, signed, and unsigned variants. Floating-point types include float, double, and long double in various precisions. Classes allow users to define their own complex data types.

Uploaded by

ariwangga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Intro

ThistutorialprovidesabriefintroductiontoC++datatypes.
Whatisadatatype?
WhenwewishtostoredatainaC++program,suchasawholenumberoracharacter,wehaveto
tellthecompilerwhichtypeofdatawewanttostore.Thedatatypewillhavecharacteristicssuchas
therangeofvaluesthatcanbestoredandtheoperationsthatcanbeperformedonvariablesofthat
type.
Fundamentaltypes
C++providesthefollowingfundamentalbuiltindatatypes:Boolean,character,integerand
floatingpoint.Italsoenablesustocreateourownuserdefineddatatypesusingenumerationsand
classes.
Foreachofthefundamentaldatatypestherangeofvaluesandtheoperationsthatcanbeperformed
onvariablesofthatdatatypearedeterminedbythecompiler.Eachcompilershouldprovidethe
sameoperationsforaparticulardatatypebuttherangeofvaluesmayvarybetweendifferent
compilers.
BooleanType
TheBooleantypecanhavethevaluetrueorfalse.Forexample:
boolisEven=false;
boolkeyFound=true;
IfaBooleanvalueisconvertedtoanintegervaluetruebecomes1andfalsebecomes0.
IfanintegervalueisconvertedtoaBooleanvalue0becomesfalseandnonzerobecomestrue.
CharacterType
ThecharactertypeisusedtostorecharacterstypicallyASCIIcharactersbutnotalways.For
example:
charmenuSelection='q';
charuserInput='3';
Notehowacharacterisenclosedwithinsinglequotes.Wecanalsoassignnumericvaluesto
variablesofcharactertype:
charchNumber=26;
Wecandeclaresignedandunsignedcharacters,wheresignedcharacterscanhavepositiveand
negativevalues,andunsignedcharacterscanonlycontainpositivevalues.
signedcharmyChar=100;
signedcharnewChar=43;
unsignedcharyourChar=200;

Notethatifweuseaplainchar,neithersignednorunsigned:
chardataValue=27;
itmaydifferbetweencompilersastowhetheritbehavesasasignedorunsignedcharactertype.On
somecompilersitmayacceptpositiveandnegativevalues,onothersitmayonlyacceptpositive
values.Refertoyourcompilerdocumentationtoseewhichapplies.
Acharisguaranteedtobeatleast8bitsinsize.C++alsoprovidesthedatatypewchar_t,awide
charactertypetypicallyusedforlargecharactersets.
AnarrayofcharacterscanbeusedtocontainaCstylestringinC++.Forexample:
charaString[]="ThisisaCstylestring";
NotethatC++alsoprovidesastringclassthathasadvantagesovertheuseofcharacterarrays.
IntegerTypes
Theintegertypeisusedforstoringwholenumbers.Wecanusesigned,unsignedorplaininteger
valuesasfollows:
signedintindex=41982;
signedinttemperature=32;
unsignedintcount=0;
intheight=100;
intbalance=67;
Likecharacters,signedintegerscanholdpositiveornegativevalues,andunsignedintegerscanhold
onlypositivevalues.However,plainintegercanalwaysholdpositiveornegativevalues,they're
alwayssigned.
Youcandeclaresignedandunsignedintegervaluesinashortenedform,withouttheintkeyword:
signedindex=41982;
unsignedcount=0;
Integervaluescomeinthreesizes,plainint,shortintandlongint.
intnormal=1000;
shortintsmallValue=100;
longintbigValue=10000;
Therangeofvaluesforthesetypeswillbedefinedbyyourcompiler.Typicallyaplainintcanholda
greaterrangethanashortint,alongintcanholdagreaterrangethanaplainint,althoughthismay
notalwaysbetrue.Whatwecanbesureofisthatplainintwillbeatleastasbigasshortintand
maybegreater,andlongintwillbeatleastasbigasplainintandmaybegreater.Ashortintegeris
guaranteedtobeatleast16bitsandalongintegeratleast32bits.
Youcandeclareshortandlongintegervaluesinashortenedform,withouttheintkeyword:
shortsmallValue=100;
longbigValue=10000;
Youcanhavelongandshortsignedandunsignedintegers,forexample:

unsignedlongbigPositiveValue=12345;
signedshortsmallSignedValue=7;
FloatingPointTypes
Floatingpointtypescancontaindecimalnumbers,forexample1.23,.087.Therearethreesizes,
float(singleprecision),double(doubleprecision)andlongdouble(extendedprecision).Some
examples:
floatcelsius=37.623;
doublefahrenheit=98.415;
longdoubleaccountBalance=1897.23;
Therangeofvaluesthatcanbestoredineachoftheseisdefinedbyyourcompiler.Typicallydouble
willholdagreaterrangethanfloatandlongdoublewillholdagreaterrangethandoublebutthis
maynotalwaysbetrue.However,wecanbesurethatdoublewillbeatleastasgreatasfloatand
maybegreater,andlongdoublewillbeatleastasgreatasdoubleandmaybegreater.
EnumerationType
Anenumerationtypeisauserdefinedtypethatenablestheusertodefinetherangeofvaluesforthe
type.Namedconstantsareusedtorepresentthevaluesofanenumeration,forexample:
enumweekday{monday,tuesday,wednesday,thursday,friday,saturday,sunday};
weekdaycurrentDay=wednesday;
if(currentDay==tuesday){
//dosomething
}
Thedefaultvaluesassignedtotheenumerationconstantsarezerobased,soinourexampleabove
monday==0,tuesday==1,andsoon.
Theusercanassignadifferentvaluetooneormoreoftheenumerationconstants,andsubsequent
valuesthatarenotassignedavaluewillbeincremented.Forexample:
enumfruit{apple=3,banana=7,orange,kiwi};
Here,orangewillhavethevalue8andkiwi9.
ClassType
Theclasstypeenablesustocreatesophisticateduserdefinedtypes.Weprovidedataitemsforthe
classtypeandtheoperationsthatcanbeperformedonthedata.Forexample,tocreateasquare
classthathasadataitemforsize,andprovidesdrawandresizeoperations:
classsquare{
intsize;
public:
square();
~square();
voiddraw();
boolresize(intnewSize);
};

[Pleaserefertoatutorialonclassesandobjectsforamoredetailedexplanationoftheclasstype,
whichisoutsidethescopeofthistutorial].
Bob@http://www.daniweb.com/techtalkforums/thread1767.html

You might also like