Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Loose ends from last time. Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10);

Similar presentations


Presentation on theme: "C++ Loose ends from last time. Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10);"— Presentation transcript:

1 C++ Loose ends from last time

2 Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10); –While this looks like a call to a constructor, it isn’t. –This strange thing is necessary under certain situations (inheritance) which we will see later Uninitialized variables are just that, uninitialized! –Remember how Java deals with this?

3 Primitive data types Table on page 58 of the text book

4 Literals Table on page 59 of the text book Gotchas 1.42934 is a double 1.42934F is a float ‘a’ is a char L‘a’ is a wide char (unicode) – some API functions may want this 27 is an int (native architecture word length) 27U is an unsigned int 0x9FC is a hexidecimal int

5 Synonyms When the names of primitive data types aren’t good enough for you long int x; –can be replaced by typedef long int LONGINT; LONGINT x; Most of the time this is cosmetic but there are times when you must use it (some parameter passage mechanisms)

6 User defined data types (enum) enum (Java has this too) enum Week {Mon, Tues, Wed, Thurs, Fri, Sat, Sun}; –Defines a datatype called Week Week thisWeek; –Instantiates a variable of type Week thisWeek = Fri ; Assigns a value to the variable Only values Mon, Tues, Wed, Thurs, Fri, Sat, Sun are legal assignments

7 User defined data types (enum) Values in an enum are ordered as specified in the list with the first element equal to 0 –e.g. Mon=0, Tues=1, Wed=2, … Sun=6 But, values can be changed enum Week {Mon = 1, Tues = 7, Wed = 7…}; and don’t even have to be unique

8 Constants You can define constant values within your program using the const type modifier const double PI = 3.1415926; Remember how it’s done in Java?

9 Operator precedence Pretty much as expected Table on page 77 of the text book USE PARENTHESIS!!!

10 Type casting Narrowing (e.g. assigning a double to a float) is only a compiler warning, not an error as in Java This is bad… int i = 257; unsigned char uc = i; std::cout << (int)uc << std::endl; Prints out 1 (not 257)

11 Type casting int x = static_cast (47.3); Performs the cast at compile time – no runtime safety checks are performed There are other, runtime safe casts to be discussed sometime in the future dynamic_cast<> const_cast<> reinterpret_cast<> The old way (like Java uses) works too int x = (int)(47.3); As does this abomination (under some cases) int x = int(47.3);

12 Static variables Similar to Java when in a class definition Dissimilar (totally different) than Java when –defined at the global (no class) level – Java doesn’t even allow this Makes it global within the file only –Defined within a function – Java doesn’t allow this either Makes the variable hold it’s value for the life of the program, even when the function exits!

13 Namespace A way of creating separate spaces within a program to avoid naming clashes namespace mySpace { int x = 27; } namespace yourSpace { int x = 32; } –There is no conflict if these exist in the same program (or even the same file) To access the values use the namespace operator mySpace::x yourSpace::x

14 Assignment check Read chapter 2 Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell you what the operator is, then tells the user if they were correct or incorrect and, if incorrect, gives the correct answer. The program should loop asking the user if they want another problem to solve and stop when they don’t. It should also keep track of the number of correct and incorrect answers and report the score when the user is finished. Example on next page

15 Example execution Expression: 12 ? 4 = 3 Your answer: / You are correct! Again? Y Expression 3 ? 3 = 9 Your answer: + You are incorrect! Correct answer is * Again? N 1 correct, 1 incorrect Bye C:>

16 To generate a random number // -- read the seed for the random number generator int seed; std::cout << "random seed: "; std::cin >> seed; // -- only seed one time per program run if you don’t change the // seed then the program will generate the same random numbers // each time you run it (pseudo random number generator) srand(seed); // -- rand() generates an integer between 0 and RAND_MAX // (Microsoft defined constant) so we need to scale it from // 0.0 to 1.0 (by dividing) then multiple by 100.0 and truncate // to get a value from 0 to 100 float y = (float)rand() / RAND_MAX; int rn = (int)(y * 100.0); // -- print it out (just for this example) std::cout << rn << std::endl;


Download ppt "C++ Loose ends from last time. Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10);"

Similar presentations


Ads by Google