data types declarations expressions data storage c++ basics
TRANSCRIPT
![Page 1: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/1.jpg)
•
• Data Types
• Declarations
• Expressions
• Data storage
C++ BasicsC++ Basics
![Page 2: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/2.jpg)
Data TypesData Types
Integers – Integers – ordinalordinal
numbersnumbers
Floating – Floating – realreal numbersnumbers
Address – Address – memorymemory
locationlocation
Structured - Structured - aggregatesaggregates
![Page 3: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/3.jpg)
Integer Data TypesInteger Data Types
ccharhar256 possibilities;256 possibilities;enclosed in single quotesenclosed in single quotes
intintno decimal pointsno decimal pointsno commasno commasno special signs ($, ¥, ‰)no special signs ($, ¥, ‰)
010100010010100010
Characters can be treated as integers -- a legacy from C
cout << ‘B’ – 1 << ‘\n’
011100010011100010
010000010010000010
011000010011000010
011101010011101010
![Page 4: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/4.jpg)
Int Data TypesInt Data Types
Int -- Signed vs. UnsignedInt -- Signed vs. Unsigned
shortshort 65,536 numbers 65,536 numbers-32,768 to 32,767-32,768 to 32,767
longlong 4,294,967,296 numbers 4,294,967,296 numbers
2,147,483,648 2,147,483,648
enum – a collection of symbolic related enum – a collection of symbolic related
constantsconstants
* *
![Page 5: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/5.jpg)
Real Number Data TypesReal Number Data Types Floating point numbers:Floating point numbers:
signed or unsigned with decimal pointsigned or unsigned with decimal point
Types: differ in amount of storage space; Types: differ in amount of storage space; varies with machine varies with machine
float (single precision)float (single precision) double double long double long double
Use Use sizeof ( )sizeof ( ) to find amount of storage to find amount of storage space used. See pages 42 - 43 in textspace used. See pages 42 - 43 in text
* * *
Floating pointFloating point numbers:
![Page 6: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/6.jpg)
Data TypesData Types AddressAddress
pointer value is an actual addresspointer value is an actual addressreference - reference - an alias for another an alias for another
variablevariable CONFUSING, don’t use CONFUSING, don’t use (yet)(yet)StructuredStructured
array - an ordered list of like itemsarray - an ordered list of like items strings are a type of an arraystrings are a type of an array
struct – unordered heterogeneous struct – unordered heterogeneous setset
class – the basis of OO in C++ class – the basis of OO in C++
![Page 7: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/7.jpg)
ConstantsConstantsLiteraltyped directly into the program as needed
ex. y = 23.4 pi = 3.1416 Symbolic (Named)Symbolic (Named)
similar to a variable, but cannot be changed similar to a variable, but cannot be changed after it is initializedafter it is initialized
Syntax:Syntax: constconst typetype VAR = value VAR = value Ex:Ex: constconst intint CLASS_SIZECLASS_SIZE = =
87;87; constconst doubledouble PI PI = 3.1416;= 3.1416;
* *
can change
can NOT change
![Page 8: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/8.jpg)
OperatorsOperators
An An operatoroperator is a symbol that is a symbol that
causes the compiler to take an causes the compiler to take an
action.action.
*
Categories:mathematicalassignment
boolean / logicaletc.
![Page 9: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/9.jpg)
Arithmetic OperatorsArithmetic Operators
additionaddition ++
subtractionsubtraction --
multiplicationmultiplication **
divisiondivision //
modulusmodulus ((remainderremainder)) %%
Unary (minus) operatorUnary (minus) operator
x = -3; x = -3;
y = -x;y = -x;
![Page 10: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/10.jpg)
Arithmetic ExpressionsArithmetic Expressions
Syntaxoperand operator operand
ExampleExample 77 ++ 1515 34 34 -- 189 189 92 92 ** 3131
345 345 // 6.02 6.02 86 86 %% 3 3
*
![Page 11: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/11.jpg)
ModulusModulus
12/3 14/3
12%3 14%3
The modulus operator yields the The modulus operator yields the remainder of integer division.remainder of integer division.
* *
43 12 12 0
43 14 12 2
![Page 12: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/12.jpg)
A Glimpse ofA Glimpse ofOperator OverloadingOperator Overloading
Operator overloadOperator overloadUsing the same symbol for more Using the same symbol for more
than than one operation.one operation.
type int / type int9 / 5
operator performs int division
type double / type double9.0 / 5.0
operator performs double division
*
![Page 13: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/13.jpg)
ModulusModulus
The modulus operator yields the The modulus operator yields the remainder of remainder of integerinteger division. division.
18 % 4 is 218 % 4 is 2 13 % 4 is 1 13 % 4 is 117 % 3 is 2 17 % 3 is 2 35 % 47 is 35 35 % 47 is 3524 % 6 is 024 % 6 is 0 24 % 4 is 0 24 % 4 is 0 4 % 4 % 18 is 4 18 is 4 0 % 7 is 0 0 % 7 is 0
12 % 2.5 12 % 2.5 errorerror 6.0 % 6 6.0 % 6 errorerror Requires floating point division, NOT Requires floating point division, NOT
the samethe same
* * *
![Page 14: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/14.jpg)
Mixed-Mode ExpressionsMixed-Mode ExpressionsOperator overloadOperator overload
Same operator will behave Same operator will behave differently differently depending upon the depending upon the operands.operands.
Operands of the same type give Operands of the same type give results of results of that type.that type.In mixed-mode, In mixed-mode, floating point floating point takes takes precedence precedence..
* *
![Page 15: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/15.jpg)
Integer DivisionInteger Division int a, b;int a, b;
a = 8;a = 8;b = 3;b = 3;cout << “The result is “ << a / b cout << “The result is “ << a / b
<< endl;<< endl;
The result is 2The result is 2
8 / 3 is 2 and not 2.6667The result must be an integer. The result is truncated to 2.
*
![Page 16: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/16.jpg)
Order of OperationsOrder of Operations
8 + 3 * 4 is 8 + 3 * 4 is ??
Show associativity to clarify.Show associativity to clarify.
( 8 + 3 ) * 4 is 44( 8 + 3 ) * 4 is 44
8 + ( 3 * 4 ) is 20 8 + ( 3 * 4 ) is 20Without parentheses,Without parentheses, 8 + 3 * 4 is 20 8 + 3 * 4 is 20
* *
P E M D A S from left to right
![Page 17: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/17.jpg)
Order of OperationsOrder of Operations
ExpressionExpression ValueValue
10 10 // 2 * 3 2 * 3
10 % 3 - 4 / 210 % 3 - 4 / 2
5.0 * 2.0 / 4.0 * 2.05.0 * 2.0 / 4.0 * 2.0
5.0 * 2.0 / (4.0 * 2.0)5.0 * 2.0 / (4.0 * 2.0)
5.0 5.0 ++ 2.0 / (4.0 * 2.0) 2.0 / (4.0 * 2.0)
15
-1
5.0
1.25
5.25
* * * * *
![Page 18: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/18.jpg)
Evaluation TreesEvaluation Trees
ii
10 / 2 * 3
* *
10 % 3 - 4 / 2
i
ii
5.0 * 2.0 / 4.0 * 2.0
r
r
r
5 * 2 / (4.0 * 2.0)
i
r
r
![Page 19: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/19.jpg)
Evaluation TreesEvaluation Trees
5.0 * 2.0 / 4.0 * 2.0
5.0 + 2.0 / (4.0 * 2.0)
(5 + 2) / (4.0 * 2.0)
i
r
rr
r
r
r
r
r
![Page 20: Data Types Declarations Expressions Data storage C++ Basics](https://reader036.vdocument.in/reader036/viewer/2022071806/56649f585503460f94c7dfbe/html5/thumbnails/20.jpg)
Those who Those who fail to plan, fail to plan, plan to failplan to fail