Pages

Sunday, 24 August 2014

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures
Tokens are the smallest individual units in a program.
  • Keywords
  • Identifiers : name to program elements (cannot start with digit)
  • Constants : fixed values that don’t change during execution
  • Strings Operators
3 uses of void: to specify return type, to indicate empty argument list and to declare generic pointers.
Generic pointers: void *gp;  can point to any basic data type but cannot be dereferenced. It is al so present in ASNSI C, but in C++ , to assign a void pointer to any other type pointer we have to use casting. Ptr=(char*) void_ptr ;

User Defined Data Types

Enumerated Data Type
enum dept {cse, mech, ise}; (values are assigned, 0,1,2 so on)
dept is the tag name, which can be used to declare new variables and cse, mech are the enumerated values
Dept my_dept= cse;
Dept my_dept=(Dept) 1;
Enumerated values can be used in place of int.
Subsequent initialized enumerators are larger by one than their predecessors.
Anonymous enums can be created without the tag name

Derived Data Types

Arrays: Difference between C and C++ strings >> in C++ the array dimension of the string should include the null character.

Pointers

Constant Pointer
Pointer to a Constant
Char * const ptr = “good”
Int const * ptr = &m
The address cannot be changed
Contents cannot be changed
const char * const cp=“xyz”
(Both the pointer and the variable is constant)

Symbolic Constants

Either use const or the keyword enum
const int size (named constant, the value can never be changed)
const size = 10 (perfectly legal, will be converted to int)
extern keyword is used to give external linkage to a const variable

enum {X};  enum declaration without a tag name
This is equivalent to const X=0;
In C++ char is not promoted to the size of int.

Reference Variables

Provides different names to the same variable. ALIAS (one location two names)
Data-type  &  reference-name  = variable-name   
Int & num = sum ;
int & means reference to a float variable

It must always be initialized when it is declared
Main application is passing arguments to functions.

Operators

::* Pointer to member declarator
->* Pointer to member operator
.* Pointer to member operator
:: scope resolution operator  (allows access to hidden global version of a variable)

New (Dynamic Allocation of memory>> allocation at run time)

Pointer-variable = new data-type (initial-value) ;
Pointer-variable = new data-type [size]  ; to allocate memory space for an array of data.
Advantage of new over malloc()  >>use of sizeof() and type casting not needed.
New and delete can be overloaded .

Manipulators (formatting the display) setw and endl (“\n”)
setw(size) creates a field of size and prints the output in a right-justified manner
cout<<setw(5)<<”hello”<<setw(5)<<”i” ;
Output:
Hello
      i
Type Cast Operator (Explicit conversion)
(type-name) expression   // C notation
Type-name (expression)  // c++

Chained expressions cannot be used at the time of the declaration

If data types are mixed then c++ will perform implicit or Automatic conversions
Follows waterfall model >> smaller type is converted to a wider type





Operator Precedence





No comments:

Post a Comment