Int, Long, Short, Char (Signed, Integer Division)
Int, Long, Short, Char (Signed, Integer Division)
Int, Long, Short, Char (Signed, Integer Division)
C++ guarantees a char is one byte in size Sizes of other types are platform dependent Can determine using sizeof() , <climits> INT_MAX
bool type
Logic type, takes on values true, false
Struct members are public by default Class members are private by default Both can have
Constructors Destructors Member variables Member functions
Common practice:
use structs for data use classes for objects with non-trivial methods
References
alias for an object or variable its type constrains what types it can refer to (more later) cannot be 0 (always references something else)
Scopes in C++
Each symbol is associated with a scope
The entire program (global scope) A namespace (namespace scope) Members of a class (class scope) A function (function scope) A block (block scope)
What if we want to describe more loosely related collections of state and behavior? Could use a class or struct
But that dilutes their design intent
CSE 332: C++ data types, input, and output
Namespaces
C++ offers an appropriate scoping mechanism for loosely related aggregates: Namespaces
Good for large function collections
E.g., a set of related algorithms and function objects
Declarative region
Where a variable/function can be declared
Potential scope
Where a variable/function can be used From where declared to end of declarative region
CSE 332: C++ data types, input, and output
Namespace Properties
Declared/(re)opened with namespace keyword
namespace Foo {int baz_;} namespace Foo {int fxn() {return baz_;}}
Everything not declared in another namespace is in the global (program-wide) namespace Can nest namespace declarations
namespace Foo {namespace Err {}}
Using Namespaces
The using keyword makes elements visible
Only applies to the current scope
C++ string class (template) provides a rich set of overloaded operators Often C++ strings do what you would expect as a programmer Often C-style strings do what you would expect as a machine designer Suggestion: use C++ style strings any time you need to change, concatenate, etc.
Overloaded operators
<< ostream insertion operator >> istream extraction operator
Other methods
ostream: write, put istream: get, eof, good, clear
Stream manipulators
ostream: flush, endl, setwidth, setprecision, hex, boolalpha
Other methods
open, is_open, close getline seekg, seekp
File modes
in, out, ate, app, trunc, binary
Program gets arguments as C-style strings But lets say we wanted to input floating point values from the command line Formatting is tedious and error-prone in C-style strings (sprintf etc.) iostream formatting is friendly Can we get there from here?
0 1 2 3 4
X
Goals
Give you a good basic data structure to use for now Cover its correct usage Start understanding why Less to manage/remember Harder to get things wrong (but still need to be careful)