1 / 30

Variables and Data Types

Variables and Data Types. Objectives of this session. Keywords Identifiers Basic Data Types bool & wchar_t Built-in Data Types User-defined Data Types Derived Data Types Symbolic Constants Dynamic Initialization of Variables Reference Variables. Variables and Data Types. Tokens

danil
Télécharger la présentation

Variables and Data Types

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. Variables and Data Types

  2. Objectives of this session • Keywords • Identifiers • Basic Data Types • bool & wchar_t • Built-in Data Types • User-defined Data Types • Derived Data Types • Symbolic Constants • Dynamic Initialization of Variables • Reference Variables

  3. Variables and Data Types Tokens The smallest individual units in a program are known as tokens. • Keywords • Identifiers • Constants • Strings • Operators

  4. Keywords

  5. Identifiers A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ).

  6. Identifiers continue… The name of a variable: • Starts with an underscore “_” or a letter, lowercase or uppercase, such as a letter from a to z or from A to Z. Examples are Name, gender, _Students, pRice. • Can include letters, underscore, or digits. Examples are: keyboard, Master, Junction, Player1, total_grade, _ScoreSide1. • Cannot include special characters such as !, %, ], or $. • Cannot include an empty space. • Cannot be any of the reserved words. • Should not be longer than 32 characters (although allowed).

  7. Basic Data Types C++ Data Types User-defined Type Built-in Type Derived Type structure union class enumeration array function pointer reference Integral Type Void Floating Type int char float double

  8. Basic Data Types continue… ANSI C++ added two more data types • bool • wchar_t

  9. Data Type - bool A variable with bool type can hold a Boolean value true or false. Declaration: bool b1; // declare b1 as bool type b1 = true; // assign true value to b1 bool b2 = false; // declare and initialize The default numeric value of true is 1 and false is 0.

  10. Data Type – wchar_t The character type wchar_t has been defined to hold 16-bit wide characters. wide_character uses two bytes of memory. wide_character literal in C++ begin with the letter L L‘xy’ // wide_character literal

  11. Built-in Data Types int, char, float, double are known as basic or fundamental data types. Signed, unsigned, long, shortmodifier for integer and character basic data types. Long modifier for double.

  12. Built-in Data Types continue… Type void was introduced in ANSI C. Two normal use of void: • To specify the return type of a function when it is not returning any value. • To indicate an empty argument list to a function. • eg:- void function-name ( void )

  13. Built-in Data Types continue… Type void can also used for declaring generic pointer. A generic pointer can be assigned a pointer value of any basic data type, but it may not be de-referenced. void *gp; // gp becomes generic pointer int *ip; // int pointer gp = ip; // assign int pointer to void pointer Assigning any pointer type to a void pointer is allowed in C & C++.

  14. Built-in Data Types continue… void *gp; // gp becomes generic pointer int *ip; // int pointer ip = gp; // assign void pointer to int pointer This is allowed in C. But in C++ we need to use a cast operator to assign a void pointer to other type pointers. ip = ( int * ) gp; // assign void pointer to int pointer // using cast operator *ip = *gp;  is illegal

  15. User-Defined Data Types Structures & Classes: struct union class Legal data types in C++. Like any other basic data type to declare variables. The class variables are known as objects.

  16. User-Defined Data Types continue… Enumerated Data Type: Enumerated data type provides a way for attaching names to numbers. enum keyword automatically enumerates a list of words by assigning them values 0, 1, 2, and so on. enum shape {circle, square, triangle}; enum colour {red, blue, green, yellow}; enum position {off, on};

  17. User-Defined Data Types continue… Enumerated Data Type: enum colour {red, blue, green, yellow}; In C++ the tag names can be used to declare new variables. colour background; In C++ each enumerated data type retains its own separate type. C++ does not permit an int value to be automatically converted to an enum value.

  18. User-Defined Data Types continue… Enumerated Data Type: colour background = blue; // allowed colour background = 3; // error in C++ colour background = (colour) 3; // OK int c = red; // valid

  19. User-Defined Data Types continue… Enumerated Data Type: By default, the enumerators are assigned integer values starting with 0. We can override the default value by explicitly assigning integer values to the enumerators. enum colour { red, blue=4, green =8}; enum colour {red=5, blue, green};

  20. User-Defined Data Types continue… Enumerated Data Type: C++ also permits the creation of anonymous enum (i.e., enum with out tag name). enum {off, on}; here off  0 and on  1 int switch_1 = off; int switch_2 = on;

  21. Derived Data Types Arrays The application of arrays in C++ is similar to that in C. Functions top-down - structured programming ; to reduce length of the program ; reusability ; function over-loading.

  22. Derived Data Types continue… Pointers Pointers can be declared and initialized as in C. int * ip; // int pointer ip = &x; // address of x assigned to ip *ip = 10; // 10 assigned to x through indirection

  23. Derived Data Types continue… Pointers C++ adds the concept of constant pointer and pointer to a constant. char * const ptr1 = “GOODS”; // constant pointer int const * ptr2 = &m; // pointer to a constant

  24. Symbolic Constants Two ways of creating symbolic constant in C++. Using the qualifierconst Defining a set of integer constants usingenumkeyword. Any value declared as const can not be modified by the program in any way. In C++, we can use const in a constant expression. const int size = 10; char name[size]; // This is illegal in C.

  25. Symbolic Constants continue… const allows us to create typed constants. #define - to create constants that have no type information. The named constants are just like variables except that their values can not be changed. C++ requires a const to be initialized. A const in C++ defaults, it is local to the file where it is declared. To make it global the qualifier extern is used.

  26. Symbolic Constants continue… extern const int total = 100; enum { X, Y, Z }; This is equivalent to const int X = 0; const int Y = 1; const int Z = 2;

  27. Reference Variables A reference variable provides an alias for a previously defined variable. For eg., if we make the variable sum a reference to the variable total, then sum and total can be used interchangeably to represent that variable. data-type & reference-name = variable-name float total = 100; float &sum = total;

  28. Reference Variables continue… A reference variable must be initialized at the time of declaration. This establishes the correspondence between the reference and the data object which it names. int x ; int *p = &x ; int & m = *p ;

  29. Reference Variables continue… void f ( int & x ) { x = x + 10; } int main ( ) { int m = 10; f (m); } When the function call f(m) is executed, int & x = m;

  30. Thank You

More Related