29
Learners Support Publications www.lsp4you.com Data Types Data Types

Learners Support Publications Data Types Data Types

Embed Size (px)

Citation preview

Page 1: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Data TypesData Types

Page 2: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Objectives of this sessionObjectives of this session

Basic Data TypesBasic Data Typesbool & wchar_tbool & wchar_tBuilt-in Data TypesBuilt-in Data TypesUser-defined Data TypesUser-defined Data TypesDerived Data TypesDerived Data TypesSymbolic ConstantsSymbolic ConstantsDynamic Initialization of VariablesDynamic Initialization of VariablesReference VariablesReference Variables

Page 3: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Variables and Data TypesVariables and Data TypesTokensTokens

The smallest individual units in a The smallest individual units in a program are known as tokens.program are known as tokens.

o KeywordsKeywordso IdentifiersIdentifierso ConstantsConstantso StringsStringso OperatorsOperators

Page 4: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

KeywordsKeywords

Page 5: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

IdentifiersIdentifiersA valid identifier is a sequence of one A valid identifier is a sequence of one

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

Page 6: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

IdentifiersIdentifiersThe name of a variable:The name of a variable: Starts with an underscore “_” or a letter, Starts with an underscore “_” or a letter,

lowercase or uppercase, such as a letter from lowercase or uppercase, such as a letter from a to z or from A to Z. a to z or from A to Z. Examples are Name, gender, _Students, pRice. Examples are Name, gender, _Students, pRice.

Can include letters, underscore, or digits. Can include letters, underscore, or digits. Examples are: keyboard, Master, Junction, Examples are: keyboard, Master, Junction, Player1, total_grade, _ScoreSide1. Player1, total_grade, _ScoreSide1.

Cannot include special characters such as !, Cannot include special characters such as !, %, ], or $. %, ], or $.

Cannot include an empty space. Cannot include an empty space. Cannot be any of the reserved words. Cannot be any of the reserved words. Should not be longer than 32 characters Should not be longer than 32 characters

(although allowed). (although allowed).

continue…

Page 7: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Basic Data TypesBasic Data TypesC++ Data Types

User-defined Type Built-in Type Derived Type

Integral Type Void Floating Type

structureunionclass

enumeration

arrayfunctionpointer

reference

int char float double

Page 8: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Basic Data TypesBasic Data Types

ANSI C++ added two more data typesANSI C++ added two more data types boolbool wchar_twchar_t

continue…

Page 9: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Data Type - boolData Type - bool

A variable with bool type can hold a Boolean A variable with bool type can hold a Boolean value true or false. value true or false. Declaration:Declaration:bool b1; // declare b1 as bool typebool b1; // declare b1 as bool typeb1 = true; // assign true value to b1 b1 = true; // assign true value to b1 bool b2 = false; // declare and initializebool b2 = false; // declare and initialize

The default numeric value of The default numeric value of true is 1 and true is 1 and false is 0.false is 0.

Page 10: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Data Type – wchar_tData Type – wchar_t

The character type wchar_t has been The character type wchar_t has been defined to hold 16-bit wide characters.defined to hold 16-bit wide characters.

wide_character uses two bytes of wide_character uses two bytes of memory.memory.

wide_character literal in C++ wide_character literal in C++ begin with the letter Lbegin with the letter LL‘xy’ // wide_character literalL‘xy’ // wide_character literal

Page 11: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Built-in Data TypesBuilt-in Data Types

int, char, float, double are known as int, char, float, double are known as basicbasic or or fundamentalfundamental data types. data types.

Signed, unsigned, long, shortSigned, unsigned, long, short modifier modifier for integer and character basic data for integer and character basic data types. types.

LongLong modifier for double. modifier for double.

Page 12: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Built-in Data TypesBuilt-in Data Types

Type void was introduced in ANSI C.Type void was introduced in ANSI C.

Two normal use of void:Two normal use of void:o To specify the return type of a function To specify the return type of a function

when it is not returning any value.when it is not returning any value.o To indicate an empty argument list to a To indicate an empty argument list to a

function.function.o eg:- eg:- void function-name ( void )void function-name ( void )

continue…

Page 13: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Built-in Data TypesBuilt-in Data Types

Type void can also used for declaring generic Type void can also used for declaring generic pointer.pointer.

A generic pointer can be assigned a pointer value A generic pointer can be assigned a pointer value of any basic data type, but it may not be de-of any basic data type, but it may not be de-referenced.referenced.

void *gp; // gp becomes generic pointervoid *gp; // gp becomes generic pointer

int *ip; // int pointerint *ip; // int pointer

gp = ip; // assign int pointer to void pointergp = ip; // assign int pointer to void pointer

Assigning any pointer type to a void pointer is Assigning any pointer type to a void pointer is allowed in C & C++.allowed in C & C++.

continue…

Page 14: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Built-in Data TypesBuilt-in Data Types

void *gp; // gp becomes generic pointervoid *gp; // gp becomes generic pointerint *ip; // int pointerint *ip; // int pointerip = gp; // assign void pointer to int pointerip = gp; // assign void pointer to int pointerThis is allowed in C. But in C++ we need to use This is allowed in C. But in C++ we need to use a cast operator to assign a void pointer to other a cast operator to assign a void pointer to other type pointers.type pointers.ip = ( int * ) gp; // assign void pointer to int ip = ( int * ) gp; // assign void pointer to int pointerpointer // using cast operator// using cast operator

*ip = *gp; *ip = *gp; is illegal is illegal

continue…

Page 15: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

User-Defined Data TypesUser-Defined Data Types

Structures & Classes:Structures & Classes:structstruct

unionunion

classclass

Legal data types in C++.

Like any other basic data type to declare variables.

The class variables are known as objects.

Page 16: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

User-Defined Data TypesUser-Defined Data Types

Enumerated Data Type:Enumerated Data Type:Enumerated data type provides a way for Enumerated data type provides a way for attaching names to numbers.attaching names to numbers.enum keyword automatically enumerates a enum keyword automatically enumerates a list of words by assigning them values 0, 1, list of words by assigning them values 0, 1, 2, and so on.2, and so on.

enum shape {circle, square, triangle};enum shape {circle, square, triangle};enum colour {red, blue, green, yellow};enum colour {red, blue, green, yellow};enum position {off, on};enum position {off, on};

continue…

Page 17: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

User-Defined Data TypesUser-Defined Data Types

Enumerated Data Type:Enumerated Data Type:enum colour {red, blue, green, yellow};enum colour {red, blue, green, yellow};

In C++ the tag names can be used to In C++ the tag names can be used to declare new variables.declare new variables.

colour background;colour background;

In C++ each enumerated data type retains In C++ each enumerated data type retains its own separate type. C++ does not its own separate type. C++ does not permit an permit an intint value to be automatically value to be automatically converted to an converted to an enumenum value. value.

continue…

Page 18: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

User-Defined Data TypesUser-Defined Data Types

Enumerated Data Type:Enumerated Data Type:colour background = blue; colour background = blue; // allowed// allowed

colour background = 3; colour background = 3; // error in // error in C++C++

colour background = (colour) 3; colour background = (colour) 3; // // OKOK

int c = red;int c = red; // valid // valid

continue…

Page 19: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

User-Defined Data TypesUser-Defined Data Types

Enumerated Data Type:Enumerated Data Type:By default, the enumerators are assigned By default, the enumerators are assigned integer values starting with 0. We can integer values starting with 0. We can override the default value by explicitly override the default value by explicitly assigning integer values to the assigning integer values to the enumerators.enumerators.

enum colour { red, blue=4, green =8};enum colour { red, blue=4, green =8};

enum colour {red=5, blue, green};enum colour {red=5, blue, green};

continue…

Page 20: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

User-Defined Data TypesUser-Defined Data Types

Enumerated Data Type:Enumerated Data Type:C++ also permits the creation of C++ also permits the creation of anonymous enum (i.e., enum with out tag anonymous enum (i.e., enum with out tag name).name).

enum {off, on}; enum {off, on}; here off here off 0 and on 0 and on 1 1

int switch_1 = off;int switch_1 = off;

int switch_2 = on;int switch_2 = on;

continue…

Page 21: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Derived Data TypesDerived Data Types

ArraysArraysThe application of arrays in C++ is The application of arrays in C++ is similar to that in C.similar to that in C.

FunctionsFunctionstop-down - structured programming ; top-down - structured programming ; to reduce length of the program ; to reduce length of the program ; reusability ; function over-loading.reusability ; function over-loading.

Page 22: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Derived Data TypesDerived Data Types

PointersPointersPointers can be declared and Pointers can be declared and initialized as in C.initialized as in C.

int * ip;int * ip; // int pointer // int pointer

ip = &x;ip = &x; // address of x assigned to // address of x assigned to ipip

*ip = 10;*ip = 10; // 10 assigned to x through // 10 assigned to x through indirectionindirection

continue…

Page 23: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Derived Data TypesDerived Data Types

PointersPointersC++ adds the concept of constant C++ adds the concept of constant pointer and pointer to a constant.pointer and pointer to a constant.char * const ptr1 = “GOODS”; // char * const ptr1 = “GOODS”; // constant pointerconstant pointer

int const * ptr2 = &m; // pointer to a int const * ptr2 = &m; // pointer to a constantconstant

continue…

Page 24: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Symbolic ConstantsSymbolic Constants

Two ways of creating symbolic constant in C+Two ways of creating symbolic constant in C++.+.

Using the qualifierUsing the qualifier constconst

Defining a set of integer constants usingDefining a set of integer constants using enumenum keyword.keyword.

Any value declared as Any value declared as constconst can not be modified by can not be modified by the program in any way. In C++, we can use the program in any way. In C++, we can use constconst in in a constant expression.a constant expression.

const int size = 10;const int size = 10;

char name[size]; char name[size]; // This is illegal in C. // This is illegal in C.

Page 25: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Symbolic ConstantsSymbolic Constants

constconst allows us to create typed constants. allows us to create typed constants.

#define#define - to create constants that have no type - to create constants that have no type information.information.

The named constants are just like variables The named constants are just like variables except that their values can not be changed. C+except that their values can not be changed. C++ requires a + requires a constconst to be initialized. to be initialized.

A const in C++ defaults, it is local to the file A const in C++ defaults, it is local to the file where it is declared. To make it global the where it is declared. To make it global the qualifier extern is used.qualifier extern is used.

continue…

Page 26: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Symbolic ConstantsSymbolic Constants

extern const int total = 100;extern const int total = 100;

enum { X, Y, Z };enum { X, Y, Z };

This is equivalent to This is equivalent to

const int X = 0;const int X = 0;

const int Y = 1;const int Y = 1;

const int Z = 2;const int Z = 2;

continue…

Page 27: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Reference VariablesReference VariablesA reference variable provides an alias for a A reference variable provides an alias for a previously defined variable.previously defined variable.

For eg., if we make the variable For eg., if we make the variable sumsum a a reference to the variable reference to the variable totaltotal, then , then sumsum and and totaltotal can be used interchangeably to can be used interchangeably to represent that variable.represent that variable.

data-type & reference-name = data-type & reference-name = variable-namevariable-name

float total = 100;float total = 100;

float &sum = total;float &sum = total;

Page 28: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Reference VariablesReference VariablesA reference variable must be A reference variable must be initialized at the time of declaration. initialized at the time of declaration. This establishes the correspondence This establishes the correspondence between the reference and the data between the reference and the data object which it names.object which it names.

int x ;int x ;

int *p = &x ;int *p = &x ;

int & m = *p ;int & m = *p ;

continue…

Page 29: Learners Support Publications  Data Types Data Types

Learners Support Publications www.lsp4you.com

Reference VariablesReference Variablesvoid f ( int & x )void f ( int & x )

{{

x = x + 10;x = x + 10;

}}

int main ( )int main ( )

{{

int m = 10;int m = 10;

f (m);f (m);

}}

continue…

When the function call f(m) is executed,

int & x = m;