Professional Documents
Culture Documents
Unit-1-lect-2
….continued
I (read)/O(write) operations in C++ are done using cin and cout statements.
cin is pre-defined that represents a standard input stream; it passes data into the program
(say from keyboard).
The file iostream.h header file defines cin object.
The cin object uses >> operator that moves data from input device to the variable.
>> is a redirection operator in C++ and is also called as stream extract operator as it
extracts data values from the input stream (stream means sequence of bytes).
For e.g.
cin >> u;
cin >> v;
cin >> w;
Or we can combine it also as follows:
cin>> u >> v >> w;
This is also called as cascading.
So, we can cascade this redirection operator without any limits.
Similarly, cout object is a standard output stream that passes data out of the program.
For e.g.
int u,v;
cout<<“\nEnter the values of u and v: “;
cout<< u << v;
<< is the output operator / insertion operator.
This insertion operator inserts data which is on the rhs into output stream on its lhs.
Even cout allows cascading of the output operator, <<.
In C, we have already used malloc() and calloc() functions for memory allocation. But
malloc() returns a void pointer. So, we need to typecast it explicitly into appropriate type
of pointer. This is not required with new operator of C++.
E.g.
In C, we used to write,
Int *p1 = (int *) malloc(sizeof(int));
Similarly, delete operator is used to free the memory used by the object.
Remember, C has free() to deallocate memory.
Just as new automatically calls the constructor, similarly delete calls the destructor of the
class whose object is being destroyed.
E.g.
delete a;
This statement will delete the complete array.
3. Global declaration section- sometimes we need same variables in both the functions
and in the main program. In such cases we use global variables. Such variables are
declared at the start of the program, before main() program.
Whenever there is a fight between a local and a global variable then it is the local
variable that gets the priority first.
4. main() function- the program execution starts from the main function.
void main() means no return value whereas int main() means value is being returned and
thus we must have return (0); at the last.
It is user-defined section only.
Identifiers are the names that we specify for the variables, types, functions, classes and
labels in our program.
Identifiers includes everything like variables, labels (used with goto) etc.
It may be upto 32 characters but may vary from compiler to compiler.
E.g. area_of_square, goto t1; where t1 is an indetifier of the type label.
Keywords:
Are reserved keywords of C++ language like char, catch, const, private, int, float, else, if, for,
if, goto , void, private, public, switch, this, throw, try, void, virtual, while and so on.
Note: don’t use these keywords as a variable name.
A constant is a fixed value that cannot be changed during the execution of a program.
E.g. integer constants, floating point constants, character constants, escape sequences like
\n = newline
\t = 8 col. Position right.
\’ = single quote.
\” = double quote.
\0 = null character.
\b = backspace.
\v = vertical tab.
E.g.
bool x, y;
x = true;
y = false;
int sum = true + 2*6 –false = 12;
wchar_t type: we know that char type variable takes 1 byte=8 bits. But some foreign language
character sets have more than 256 chars. To solve this problem, C++ provides wchar_t type to
accommodate character sets that require more than 8 bits. wchar_t data type is typically 16-bits
wide.
Standard C++ iostream class library defines wout object.
These assignment statements work from right to left i.e. the value of rhs is assigned and stored in lhs
variable.