FP 201 Unit 2 - Part 2

Preview:

DESCRIPTION

 

Citation preview

Unit 2 :Data types

Objectives C++ data types, constant and

variable C++ keywords Input Output : cin, cout Hands On!

At the end of this presentation, students should be able to

• Identify identifier, variable, constant, data types and keywords use in C++ Programming

• Write C++ program

A data type determines the type of the data that will be stored, usually, in the computer memory (RAM).

Type statements in C++ are used to allow the compiler to: • reserve blocks of memory to store information• give the reserved blocks of memory a

symbolic name so that the data contained in this block of memory can be manipulated by referring to this name in future C++ statements.

C++ provides six fundamental data types:

However, qualifiers that can be put in front of the numerical data types to form their derivatives:

Can you tell the difference between those qualifiers?

char int float

double bool wchar_t

short long

signed unsigned

Fundamental data types and their size and ranges in the memory

Name Description Size* Range*

charCharacter or small integer.

1bytesigned: -128 to 127unsigned: 0 to 255

short int(short)

Short Integer. 2bytessigned: -32768 to 32767unsigned: 0 to 65535

int Integer. 4bytes

signed: -2147483648 to 2147483647unsigned: 0 to 4294967295

long int(long)

Long integer. 4bytes

signed: -2147483648 to 2147483647unsigned: 0 to 4294967295

boolBoolean value. It can take one of two values: true or false.

1byte true or false

float Floating point number. 4bytes+/- 3.4e +/- 38 (~7 digits)

doubleDouble precision floating point number.

8bytes+/- 1.7e +/- 308 (~15 digits)

long doubleLong double precision floating point number.

8bytes+/- 1.7e +/- 308 (~15 digits)

wchar_t Wide character.2 or 4 bytes

1 wide character

What is the suitable data type for the following?• number of student in your class• your matrix number• assignment marks for this subject• the distance to the moon (the distance to the

moon is over 200,000 miles) • last month's checking account balance • a counter used to count the number of lines in a

text file • Malaysian’s plate number• number of people living in Malaysia • the temperature used in a chemistry formula

Unit 2 : Identifier, variable, constant, keywords

KEYWORDS reserved words that have standard, predefined meanings

and must be used only for their intended purpose. it cannot be used as an identifier. keywords in C++ are as follow:asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while

refer to memory locations which can hold values.

it is used for naming variables, constants, functions, structures and classes.

Rules:• a valid identifier must begin with a letter or

underscore (_)• can consist only of letters, digits, and

underscores. • no blank space is allowed & the size must less

than 31 characters• an identifier should not match with any C++

reserved keywords.

The C++ language is a "case sensitive" language.

That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters.

Thus, for example, the NUMBER variable is not the same as the result variable or the number variable.

ValidValid InvalidInvalid

xx

sumx2sumx2

hourly_ratehourly_rate

namename

GROSS_PAYGROSS_PAY

““x”x”

2sumx2sumx

hourly-ratehourly-rate

name@name@

GROSS PAYGROSS PAY

Valid or not?• mass• c++• Force • 2ndBit • pos12• speed_of_light • yağmur• SpeedOfLight • float• isPrime• speed of light

variable• a valid identifier whose value can change during the

course of execution of a program general form of the declarations:

data-type variable_name; example:

int mass; double x, speed, dragForce;

when a variable is declared, you can initialize it in two alternative but equivalent ways

int mass = 22; or int mass(22);

Declaration of string variable•  include an additional header file <string> general

example:string name=“Mohamad”;

// Declaration of variables

#include <iostream> using namespace std;

int main () { short x = 22, y = 11, z; z = x - y; cout << "z = " << z << endl; int p = 3; int q = x * y * z – 2 * p; cout << "q = " << q << endl; return 0; }

variable can have either local or global scope

scope (visibility) of local variables is limited to the block enclosed in braces ({ }) where they are declared

global variables are declared outside of all blocks and their scope are the entire program, i.e. the main body of the source code.

Write appropriate declarations for the following:• Integer variable: index• Unsigned integer variable: customer_no• Double precision variables: gross, tax, net• Character variables: first, last

values that do not change during program execution.

they can be any type of integer, character or floating-point.

done by using const keyword as follows:const float PI = 3.1415926, TWOPI = 2.0*PI; const int EOF = -1;const num3 = 15;

by convension;• variable names use lowercase letters • constants use uppercase only

Correct the following errors• long Float x;• int code = three,• const int array_size;

Declare the following variable:Name Type Initial value

marks double None

grade char A

price float 10.0

num_1 int 5

msg string Hello World

result bool true

#include <iostream>using namespace std;int main() { const num2 = 10; const num3 = 15; cout << “Number =” << num2 + num3; return 0; }

Code Description Code Description

\a Audible Bell \t Horizontal tab

\b Backspace \\ Backslash character

\f Formfeed \’ Single quote character

\n Newline \” Double quote character

\r Carriage return

\0 Null ASCII 0

http://www.cplusplus.com/doc/tutorial/

In this presentation, you learnt the following: Data type defines the type of value to be stored

in the memory. Variable is the name given to the memory

location where the value is stored. A constant is a value that does not change

throughout program execution.

SUMMARY

1. List the data types of C++.2. What is the difference between variables

and constants?

EXERCISE

Recommended