1 / 26

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

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

bert
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variable and data type សាកលវិទ្យាល័យជាតិគ្រប់គ្រងNational University of Management Lectured by : SRENG VICHET Tel : 090 333 538 E-mail : srengvichet@num.edu.kh Prepared by : CHEA VICHET 077 657 007 EAV DARAPISAL 090 441 666 CHONG KIMKEANG 067 666 144 SRENG VICHET 090 333 538

  2. Objectives Identifier Variable and data type Declared Variable initialization Variable Scope of Variable Introduction to string National University of Management

  3. Objective (con’t) string concatenate Constant variable Literal variable Symbolic Constant typedef Enumerated Constants National University of Management

  4. Let’s think! first_num = 5; second_num = 2; first_num+=1; int result = first_num –second_num; • 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. 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; // valid Second number = 2 // invalid identifer • Case sensitive • Result = 7; • result = 7; 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: National University of Management

  7. Fundamental Data Type National University of Management

  8. Declared Variable • Syntax:<data type> <identifier>; • intnumberOfEmployee; • float salary; • char letter; • string name; • Multiple variable with the same type: • inta,b,c; • float salary, income; 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); 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 intmyNumberOfSisiters; • signed intmyAccountBalance; National University of Management

  11. Example #include <iostream> int main() { unsigned short ageP=30; intageN=-30; std::cout<< ageP<<std::endl; std::cout << ageN<<std::endl; std::cin.get(); return 0; } 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. National University of Management

  13. #include <iostream> using namespace std; int a; // global variable int b; // global variable int 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; } National University of Management

  14. #include <iostream> using namespace std; int a, b; //global variable int 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 = 5 b = 10 a = 2 b = 3 a = 5 b = 10 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”; National University of Management

  16. This is a string. This is another string. #include <iostream> #include <string>using namespace std;int main () { stringmystring = “This is a string.”cout<< mystring << endl; stringyourstring(“This is another string.”);cout<< yourstring;return 0;} National University of Management

  17. string concatenate string firstName = "Sabay"; string middleName = "Sok"; string lastName = "Sen"; string fullName = lastName + " "+ middleName + " "+ firstName; cout << fullName; cout << endl; 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. National University of Management

  19. Literal Constant Is a value typed directly into your program wherever it is needed. Integer 75 // int 75u // unsigned int 75l // long 75ul // unsigned long 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 Boolean Boolean has only two value :true (1) និង false (0) National University of Management

  21. Symbolic Constant • Is a constant represented by a name, just like a variable. The constkeyword precedes the type, name, and initialization. • constintbouns = 300; • const float pi = 3.14159; National University of Management

  22. typedef(synonym of type definition) • typedefint 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. 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. National University of Management

  24. Example National University of Management

  25. Quiz • Please list all data types that you know, at least 5 data types. • What is distinguish between singed and unsigned? • What is the difference between an integer variable and a floating variable. • Which of the following variable names good, bad, invalid? • Age • !Ex • R98 • _Invalid National University of Management

  26. Thank You! National University of Management

More Related