Thursday, August 23, 2018

Some expressions and Control Structures

ANSI C++, doesn't have character limit for variable names like in C.

new type introduced extra, wchar_t type , wide character literal type intended for character sets that cannot fit into a single byte.they begin with L.

Ex: L'ab'

C++ supports 2 types of string representation.
C-style string and string class type (recommended) introduced in C++.

The enums in C++, have one more extra feature than C.
can declare using the tag names.

enum shape{circle,square,triangle};
shape ellipse;

ANSI C, declares the enums to be int.Here, each enum datatype is its own separate type. i.e., C++ automatically doesn't convert enum to int.

shape b1 = square;
shape b2 = 2;//Error in C++
int c = triangle//Reverse is ok

shape b2 = (shape)2; //Allowed

char s1[2]="xy" ; //ok in C but error in C++.As in C, null \0 is ignored
char s1[3] = "xy"

constant pointer:
char  * const ptr1 = "GOOD";

we cannot modify the address ptr1 is initialized to.

Pointer to constant:
Address can be changed but not its value it points to.

int const * ptr2 = &m;

Reference variable: alias names
data-type & ref-name = var-name

Here & is not address.
float & -- it is reference if type float

::var-name -- refers tp global version of variable if a local variable with same name exists.

This is used in classes as scope-resolution-operator to identify the class to which a member belongs.

malloc,caloc and free are used functions for memory management.
new and delete operators are also used mainly for this.

pointer-var = new data-type
int *p = new int;

Or we can initialize also directly.
pointer-var = new data-type(value)
float *q = new float(7.5);

can be used for arrays also..i mean any datatype.

int *p = new int[10]

p[0] is first element.

delete pointer-var;

delete p;

delete [size] pointer-var;
size number of elements in the array are freed.

delete []p; //deletes entire array

while,for,do while,switch,etc all are like C.

 



No comments:

Post a Comment