programming declarations. comp102 prog. fundamentals i: declarations/ slide 2 general form of a c++...

14
Programming Declarations

Post on 19-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

Programming

Declarations

Page 2: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 2

General form of a C++ program// Program description #include directives int main(){

constant declarations

variable declarations

executable statements

return 0; }

Page 3: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 3

C++ Data Type

A type defines a set of values and a set of operations that can be applied on those values. The set of values for each type is known as the domain for the type.

C++ contains 5 standard types:

vo id in t ch ar flo a t b o o l

S tan da rdD a ta T yp es

Page 4: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 4

void

The void type has no values and no operations. In other words, both the set of values and the set of operations are empty. Although this might seem unusual, we will see later that it is a very useful data type.

Page 5: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 5

Integer

An integer type is a number without a fractional part. It is also known as an integral number. C++ supports three different sizes of the integer data type: short int, int and long int.

sizeof(short int)<= sizeof(int)<= sizeof(long int)

Short int

int

long int

Page 6: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 6

Integer

The type also defines the size of the field in which data can

be stored. In C++, even though the size is machine dependent, most PCs use the integer sizes shown below.

Type Sign Byte Size

Number of Bits

Min Value

Max Value

short int Signedunsigned

2 16 -32768 0

3276765535

int Signed

unsigned

4 32 -2,147,483,648

0

2,147,483,647

4,294,967,295

long int Signed

unsigned

4 32 -2,147,483,648

0

2,147,483,647

4,294,967,295

Page 7: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 7

Floating Point

A floating-point type is a number with a fractional part, such as 43.32. The C++ language supports three different sizes of floating-point: float, double and long double.

sizeof(float)<= sizeof(double)<= sizeof(long double)

float

double

long double

Page 8: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 8

Floating Point

type Byte size Number of Bits

float 4 32

double 8 64

long double 10 80

Although the physical size of floating-point types is machine dependent, many computers support the sizes shown below.

Page 9: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 9

Declarations Constants and variables must be declared before they can

be used. A constant declaration specifies the type, the name and

the value of the constant. A variable declaration specifies the type, the name and

possibly the initial value of the variable. When you declare a constant or a variable, the compiler:

1. Reserves a memory location in which to store the value of the constant or variable.

2. Associates the name of the constant or variable with the memory location. (You will use this name for referring to the constant or variable.)

For more on declarations, see www.courseware.ust.hk and choose English--> C++ --> Declarations.

Page 10: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 10

Constant declarations Constants are used to store values that never change

during the program execution. Using constants makes programs more readable and

maintainable. Syntax: const <type> <identifier> = <expression>;

Examples: const double US2HK = 7.8;

//Exchange rate of US$ to HK$ const double HK2TW = 3.98;

//Exchange rate of HK$ to TW$ const double US2TW = US2HK * HK2TW;

//Exchange rate of US$ to TW$

Page 11: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 11

Variables are used to store values that can be changed during the program execution.

A variable is best thought of as a container for a value.

3445 y -3.14

Syntax: < type > < identifier >;

< type > < identifier > = < expression >;

Examples: int sum;

int total = 3445;

char answer = 'y';

double temperature = -3.14;

Variable declarations

Page 12: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 12

A variable has a type and it can contain only values of that type. For example, a variable of the type int can only hold integer values.

Variables are not automatically initialized. For example, after declaration

int sum;

the value of the variable sum can be anything (garbage). Thus, it is good practice to initialize variables when they

are declared. Once a value has been placed in a variable it stays

there until the program deliberately alters it.

Variable declarations

Page 13: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 13

Character data A variable or a constant of char type can hold an

ASCII character (see Appendix A of the textbook). When initializing a constant or a variable of char

type, or when changing the value of a variable of char type, the value is enclosed in single quotation marks.

Examples: const char star = '*';

char letter, one = '1';

Page 14: Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives

COMP102 Prog. Fundamentals I: Declarations/ Slide 14

Computers are Easy!

"Using a computer is just like riding a bike, except you don't have to wear the tight shorts and funny helmet. A water bottle is also a bad idea. Just about everyone agrees that computing is very simple, or will be in only a few more days.

– David Lubar, 1995