Data Type - lsp4you.com

Download Report

Transcript Data Type - lsp4you.com

Variables and Data
Types
Learners Support Publications
www.lsp4you.com
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
Learners Support Publications
www.lsp4you.com
Variables and Data Types
Tokens
The smallest individual units in a program are
known as tokens.
o Keywords
o Identifiers
o Constants
o Strings
o Operators
Learners Support Publications
www.lsp4you.com
Keywords
Learners Support Publications
www.lsp4you.com
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 (_ ).
Learners Support Publications
www.lsp4you.com
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).
Learners Support Publications
www.lsp4you.com
Basic Data Types
C++ Data Types
User-defined Type
Built-in Type
Derived Type
structure
union
class
enumeration
array
function
pointer
reference
Integral Type
int
Learners Support Publications
Void
char
www.lsp4you.com
Floating Type
float
double
Basic Data Types
ANSI C++ added two more data types
 bool
 wchar_t
Learners Support Publications
www.lsp4you.com
continue…
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.
Learners Support Publications
www.lsp4you.com
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
Learners Support Publications
www.lsp4you.com
Built-in Data Types
int, char, float, double are known as basic or
fundamental data types.
Signed, unsigned, long, short modifier for integer
and character basic data types.
Long modifier for double.
Learners Support Publications
www.lsp4you.com
Built-in Data Types
continue…
Type void was introduced in ANSI C.
Two normal use of void:
o
o
To specify the return type of a function when it is not
returning any value.
To indicate an empty argument list to a function.
o
eg:- void function-name ( void )
Learners Support Publications
www.lsp4you.com
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++.
Learners Support Publications
www.lsp4you.com
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
Learners Support Publications
www.lsp4you.com
User-Defined Data Types
Structures & Classes:
struct
union
Legal data types in C++.
class
Like any other basic data type to declare variables.
The class variables are known as objects.
Learners Support Publications
www.lsp4you.com
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};
Learners Support Publications
www.lsp4you.com
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.
Learners Support Publications
www.lsp4you.com
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
Learners Support Publications
www.lsp4you.com
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};
Learners Support Publications
www.lsp4you.com
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;
Learners Support Publications
www.lsp4you.com
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 overloading.
Learners Support Publications
www.lsp4you.com
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
Learners Support Publications
www.lsp4you.com
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
Learners Support Publications
www.lsp4you.com
Symbolic Constants
Two ways of creating symbolic constant in C++.
Using the qualifier const
Defining a set of integer constants using enum keyword.
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.
Learners Support Publications
www.lsp4you.com
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.
Learners Support Publications
www.lsp4you.com
Symbolic Constants
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;
Learners Support Publications
www.lsp4you.com
continue…
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;
Learners Support Publications
www.lsp4you.com
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 ;
Learners Support Publications
www.lsp4you.com
Reference Variables
void f ( int & x )
{
x = x + 10;
}
int main ( )
{
int m = 10;
f (m);
}
Learners Support Publications
www.lsp4you.com
continue…
When the function call f(m) is
executed,
int & x = m;
Thank You
Learners Support Publications
www.lsp4you.com