The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science
Programming Languages and Tools:
CS:3210:0001
Lecture/Lab #4
Programming with C++
Variables and types (cont.), fundamental types, initialization
Warm-up
void hello( std::string name );
int main() {
hello( "Bob" );
}
-
What are some differences between entity declaration vs definition?
-
Does line 1 declare or define a function `hello`?
-
Is the above a valid C++ program?
-
Can function parameter names vary between a declaration and the definition?
Variables and types (cont.)
n = 42
n = False
n = "Hi there"
n = 3.14
int n = 42;
bool b = false;
std::string s = "Hi there";
float f = 3.14;
Python
C++
-
Can't change the type of the variable once it's been declared
-
Can't declare/define two variables with the same name in the same scope
-
There are multiple types for representing numeric variables and values
Fundamental Types
void
bool
int
type with an empty set of values; objects of type void are disallowed
Modifiers:
basic integer type. The keyword int may be omitted if any of the modifiers listed below are used
boolean type, possible values are true or false
signed / unsigned
short / long / long long
float, double, long double
floating point types; IEEE-754 32 bit, IEEE-754 64 bit, implementation defined "big" floating point type correspondingly
char, signed char, unsigned char, wchar_t
character types for representing string's individual characters
Deciding which Type to Use
-
Use an unsigned type when you know that the values cannot be negative.
-
Use int as a default type for integer arithmetic. short is usually too small and, in practice, long often has the same size as int. If your data values are larger than the minimum guaranteed size of an int, then use long long.
-
Do not use plain char or bool in arithmetic expressions. Use them only to hold characters or truth values. Computations using char are especially problematic because char is signed on some machines and unsigned on others. If you need a tiny integer, explicitly specify either signed char or unsigned char.
-
Use double for floating-point computations; float usually does not have enough precision, and the cost of double-precision calculations versus single-precision is negligible. In fact, on some machines, double-precision operations are faster than single. The precision offered by long double usually is unnecessary and often entails considerable run-time cost.
Exercise 1
Finish the program by providing definitions of functions called from `main`
-
Open the exercise template
-
Write your code, press Run to test
-
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
-
Click on the corresponding option in the "Lab4 exercises" poll in #general
Initialization
int i = 42;
int i( 42 ); // more or less the same as above
int i = { 42 }; // disallows narrowing conversions
int i{ 42 }; // same as above
int i({ 42 }); // compile error, this is
// something entirely different
Multiple ways to initialize a variable (mostly due to backward compatibility):
Recommendation: use the assignment form (=) for built-in types, compile your program with flags that make narrowing conversions a compile-time error
Programming with C++, Spring 2020, Lecture #4
By Aleksey Gurtovoy
Programming with C++, Spring 2020, Lecture #4
Variables and types (cont.), fundamental types, initialization
- 558