c++ loose ends from last time. variable initialization you can do the usual things int x; x = 10;...

16
C++ Loose ends from last time

Upload: doreen-bell

Post on 04-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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);

C++

Loose ends from last time

Page 2: 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);

Variable initialization

• You can do the usual thingsint x;x = 10;int y = 20;

• And you can do an unusual thingint 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?

Page 3: 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);

Primitive data types

• Table on page 58 of the text book

Page 4: 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);

Literals

• Table on page 59 of the text book• Gotchas

1.42934 is a double1.42934F is a float‘a’ is a charL‘a’ is a wide char (unicode) – some API functions

may want this27 is an int (native architecture word length)27U is an unsigned int0x9FC is a hexidecimal int

Page 5: 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);

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)

Page 6: 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);

User defined data types (enum)

• enum (Java has this too)enum Week {Mon, Tues, Wed, Thurs, Fri, Sat, Sun};

– Defines a datatype called WeekWeek thisWeek;

– Instantiates a variable of type WeekthisWeek = Fri;

• Assigns a value to the variable• Only values Mon, Tues, Wed, Thurs, Fri, Sat,

Sun are legal assignments

Page 7: 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);

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 changedenum Week {Mon = 1, Tues = 7, Wed = 7…};

and don’t even have to be unique

Page 8: 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);

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?

Page 9: 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);

Operator precedence

• Pretty much as expected

• Table on page 77 of the text book

• USE PARENTHESIS!!!

Page 10: 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);

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)

Page 11: 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);

Type casting

int x = static_cast<int>(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 futuredynamic_cast<>const_cast<>reinterpret_cast<>

• The old way (like Java uses) works tooint x = (int)(47.3);

• As does this abomination (under some cases)int x = int(47.3);

Page 12: 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);

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!

Page 13: 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);

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 operatormySpace::xyourSpace::x

Page 14: 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);

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

Page 15: 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);

Example executionExpression: 12 ? 4 = 3Your answer: /You are correct!Again? YExpression 3 ? 3 = 9Your answer: +You are incorrect! Correct answer is *Again? N1 correct, 1 incorrectByeC:>

Page 16: 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);

To generate a random number

// -- read the seed for the random number generatorint 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 100float y = (float)rand() / RAND_MAX;int rn = (int)(y * 100.0);// -- print it out (just for this example)std::cout << rn << std::endl;