សាកលវិទ្យាល័យជាតិគ្រប់គ្រង national university...

26
សសសសសសសសសសសសសសសសសសសសសសសសសស National University of Management Variable and data type Lectured by :SRENG VICHET Tel : 090 333 538 E-mail : [email protected] Prepared by : CHEA VICHET 077 657 007 EAV DARAPISAL 090 441 666 CHONG KIMKEANG 067 666 144 SRENG VICHET 090 333 538

Upload: bert

Post on 22-Feb-2016

35 views

Category:

Documents


0 download

DESCRIPTION

Variable and data type. សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management. Lectured by :SRENG VICHET Tel :090 333 538 E-mail : [email protected] Prepared by : CHEA VICHET 077 657 007 EAV DARAPISAL 090 441 666 CHONG KIMKEANG 067 666 144 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

សាកលវទិ្យាល័យជាតិគ្ប់គ្ងNational University of Management

Variable and data typeLectured by : SRENG VICHET

Tel : 090 333 538 E-mail : [email protected]

Prepared by : CHEA VICHET 077 657 007 EAV DARAPISAL 090 441 666CHONG KIMKEANG 067 666 144SRENG VICHET 090 333 538

Page 2: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

2

Objectives• Identifier• Variable and data type• Declared Variable• initialization Variable• Scope of Variable• Introduction to string

Page 3: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

3

Objective (con’t)• string concatenate• Constant variable• Literal variable• Symbolic Constant• typedef• Enumerated Constants

Page 4: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

4

Let’s think!• If I want you remember number 5 then number 2

more, so your brain remember number 5 and number 2.

• Now I suggest you to add 1 in the first number. You ready answer 6(5+1), then take number one minus number two. The result is 6 -2 =4.

first_num = 5;second_num = 2;first_num+=1;int result = first_num –second_num;

Page 5: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

5

Identifier• Name of variable or function

– Must begin with letter.– No space.– Must not be matched any keywords.first_number = 5; // validSecond number = 2 // invalid identifer

• Case sensitive– Result = 7;– result = 7;

Page 6: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

6

Variable & Data Type• Variable:

– Identifier that is used to store any different data type.

– mySkill =“information technology”;– myAge = 28;– myHeight =1.70;

• Data Type– It is the type of data that variable will be stored:

Page 7: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

7

Fundamental Data Type

Page 8: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

8

Declared Variable• Syntax: <data type> <identifier>;

– int numberOfEmployee;– float salary;– char letter;– string name;

• Multiple variable with the same type:– int a,b,c;– float salary, income;

Page 9: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

9

Initialization of variable• When declaring a regular variables, its value

is by default undermined. In order to initialize the variable, we have two ways to to do this in C++:

• Syntax:type identifier = initial_value; //c-like

int a = 0;type identifier(initial_value); // constructor initialization

int b(0);

Page 10: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

10

singed and unsigned• char, short, long and int can be signed and

unsigned too.– singed : your variables are stored the

positive(+) and negative(-) values;– unsigned : your variable are stored the

positive(+) and value of 0;• unsigned short int myNumberOfSisiters;• signed int myAccountBalance;

Page 11: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

11

Example#include <iostream>int main(){

unsigned short ageP=30;int ageN=-30;std::cout << ageP<<std::endl;std::cout << ageN<<std::endl;std::cin.get();return 0;

}

Page 12: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

12

Scope of Variable• local variable may be accessed within the

block of code in which a variable may be accessed. It can be used in function or a block of code({}).

• global variable may be accessed within every scope of the program. It can be used everywhere even though in function.

Page 13: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management 13

#include <iostream>using namespace std;

int a; // global variableint b; // global variableint main () {

int result; // local variable a = 5; // you use b = 2; // global result = a + b ; // variable here cout << "a + b = " ; cout << result ;

cin.get();return 0;

}

Page 14: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management 14

#include <iostream>using namespace std;

int a, b; //global variableint main(){ a = 5; b = 10; cout << "a = " << a << endl; cout << "b = " << b << endl; { int a, b; //local variable to its block a = 2; b = 3; cout << "a = " << a << endl; cout << "b = " << b << endl; } cout << "a = " << a << endl; cout << "b = " << b << endl; cin.get(); return 0;}

a = 5b = 10a = 2b = 3a = 5b = 10

Page 15: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

15

Introduction to string• Variables that can store non-numberical

values that are longer than one single character are known as string.

• In order to used string type we need to include a additional header file in our source code: <string>.

#include <string>…..string num=“National University of Management”;

Page 16: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management 16

#include <iostream> #include <string>using namespace std; int main () {

string mystring = “This is a string.”cout << mystring << endl;

string yourstring(“This is another string.”); cout << yourstring ;

return 0;}

This is a string.This is another string.

Page 17: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

17

string concatenatestring firstName = "Sabay"; string middleName = "Sok"; string lastName = "Sen"; string fullName = lastName + " "+ middleName + " "+ firstName;

cout << fullName; cout << endl;

Page 18: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

18

Constant Variable• A constant, like a variable, is a memory

location where a value can be stored. Unlike variables, constants never change in value(read-only).

• C++ has two type of constants:– Literal constants.– Symbolic constants.

Page 19: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

19

Literal Constant• Is a value typed directly into your program

wherever it is needed.

• Integer 75 // int75u // unsigned int75l // long75ul // unsigned long

Page 20: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

20

Literal Constant (con’t)• Floating Point

3.14159 // 3.14159

6.02e23 // 6.02 x 1023

3.14159L // long double

6.02e23f // float

• Character and String'z' // 'z' character“Hello World” // “Hello World” String

• BooleanBoolean has only two value : true (1) និង false (0)

Page 21: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

21

Symbolic Constant• Is a constant represented by a name, just

like a variable. The const keyword precedes the type, name, and initialization.– const int bouns = 300;– const float pi = 3.14159;

Page 22: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

22

typedef (synonym of type definition)

• typedef int integer; //integer is synonym int

• typedef double salary; //salary is synonym double

• typedef salary mysalary; //incorrect

• Why we used typedef:– In order to hide data type.– Easy to know, understand and use data type.

Page 23: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

23

Enumerated Constants• It create a set of constants with a single

statement. They are defined with the keyword enum followed by a series of comma-separated names surrounded by braces:– enum COLOR {RED, BLUE,GREEN,};

• The values of enumerated constants begin with 0 for the first in the set and count upwards by 1.

Page 24: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

24

Example

int Mon=1;int Tue=2;int Wed=3;int Thu=4;int Fri=5;int Sat=6;int Sun=7;

enum DAYOFWEEK { Mon=1, Tue, Wed, Thu, Fri, Sat, Sun,}; DAYOFWEEK dayOfWeek= Sun;cout << dayOfWeek; //7

Page 25: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

25

Quiz1. Please list all data types that you know, at least 5

data types.2. What is distinguish between singed and unsigned?3. What is the difference between an integer variable

and a floating variable.4. Which of the following variable names good, bad,

invalid?– Age– !Ex– R98– _Invalid

Page 26: សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

National University of Management

26

Thank You!