4. Tokens
The smallest individual units in a program are
known as tokens.
C++ has the following tokens :
Keywords
Identifiers
Strings
operators
4
By:-Gourav Kottawar
7. 3.2 Basic Data Types, User-Defined Data Types,
Primitive Built-in Types
Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t
7
By:-Gourav Kottawar
8. Several of the basic types can be modified
using one or more of these type modifiers:
signed
unsigned
short
long
8
By:-Gourav Kottawar
9. Type Typical Bit Width Typical Range
char 1byte -127 to 127
unsigned char 1byte 0 to 255
signed char 1byte -128 to 127
int 2bytes -32768 to 32767
unsigned int 2bytes 0 to 65,535
signed int 2bytes -32768 to 32767
short int 2bytes -32768 to 32767
unsigned short
int
2 byte 0 to 65,535
signed short int 2 byte -32768 to 32767
long int 4bytes
-2,147,483,647 to
2,147,483,647
signed long int 4bytes same as long int
unsigned long
int
4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character
9 By:-Gourav Kottawar
10. #include <iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; return 0;
}
10
By:-Gourav Kottawar
11. typedef Declarations:
You can create a new name for an existing
type using typedef.
Syntax : typedef type newname;
For example, the following tells the compiler that feet is another name
for int:
typedef int feet;
Now, the following declaration is perfectly legal
and creates an integer variable called
distance:
feet distance;
11
By:-Gourav Kottawar
12. Enumerated Types:
An enumerated type declares an optional type
name and a set of zero or more identifiers that
can be used as values of the type. Each
enumerator is a constant whose type is the
enumeration.
Syntax - enum enum-name { list of names }
var-list;
Ex- enum color { red, green, blue } c;
c = blue;
enum color { red, green=5, blue };
12
By:-Gourav Kottawar
13. 3.4 Reference Variables
A reference variable is an alias, that is, another
name for an already existing variable.
Once a reference is initialized with a variable,
either the variable name or the reference
name may be used to refer to the variable.
Creating References in C++:
int i = 17;
We can declare reference variables for i as
follows.
int& r = i;
13
By:-Gourav Kottawar
14. #include <iostream>
using namespace std;
int main ()
{ // declare simple variables
int i;
double d;
// declare reference variables
int& r = i;
double& s = d;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0; }
14 By:-Gourav Kottawar
15. Operators in C++
All c operators are valid in c++.
<<
>>
:: Scope resolution operator
::* Pointer to member declarator
->*Pointer to member operator
.* Pointer to member operator
delete
endl
new
setw
15
By:-Gourav Kottawar
16. Memory Management Operator
new
Pointer_variable = new data-type;
Ex- p = new int;
int *p= new int;
We can also initialize the memory
using the new operator
Pointer_variable = new data-
type(value);
Ex- int *p=new int (25);
16
By:-Gourav Kottawar
17. Memory Management Operator
One dimensional array
Pointer_variable = new data-type[size];
int *p=new int[10];
Multidimensional array
array_ptr = new int[3][5][4];
array_ptr = new int[m][5][4];
17
By:-Gourav Kottawar
18. Memory Management Operator
delete
delete destroyed to release the memory space
for reuse.
delete pointer_variable;
Ex - delete p;
Free a dynamically allocated array
delete [size] pointer_variable;
Ex- delete []p;
18
By:-Gourav Kottawar
19. Memory Management Operator
Advantages of new operator
1. It automatically computes size of the data
object. No need of sizeof.
2. It automatically returns the correct pointer
type, no need to use type cast.
3. It is possible to initialize the object while
creating the memory space.
4. new , delete can be overloaded.
19
By:-Gourav Kottawar