You are on page 1of 20

1

Unit-1-lect-2
….continued

unit-1; lect-2; dr-rajiv


2 Input /Output in C++

 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).

unit-1; lect-2; dr-rajiv


3

 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.

unit-1; lect-2; dr-rajiv


4

 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, <<.

unit-1; lect-2; dr-rajiv


5 New and Delete operators in C++

 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));

But in C++ we can write it in a simpler way as:-


int *p1 = new int;
Also new operator automatically call the constructor while malloc() doesn’t. furthermore, new operator can
be overloaded also. (to study a bit later).

unit-1; lect-2; dr-rajiv


6

 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.

unit-1; lect-2; dr-rajiv


7 Basic structure of a C++ Program

 In general, a C++ program is divided into following sub-sections:-


 Documentation/ comments section.
 Preprocessor directives/ compiler directives/C++ preprocessor.
 Definition section.
 Global declaration section.
 main() function.
 Beginning of program (‘{‘) and with local variables and executable statements.
 End of the main program (‘}’)
 User-defined functions.

unit-1; lect-2; dr-rajiv


8

 1. Documentation/ comments do not generate any executable statement but increases


program readability. Both // and /*….*/ can be used for comments.
 2. Preprocessor directives- #include is used as a preprocessor directive.
 E.g.
 #include<iostream.h> means search for file in 1 or more std. directories.
 #include “iostream.h” means look in programmer’s own directory; if not found then search
in standard directories too.
 #define PI 3.1417 will initialize a variable PI with a value of 3.1417
 These directives cannot be placed anywhere in a program; only at the start of the program.
No spaces between # and define. Do not use = sign with #define preprocessor directive.

unit-1; lect-2; dr-rajiv


9

 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.

unit-1; lect-2; dr-rajiv


10 C++ character set

 In C++, characters are grouped as follows:-


 A) letters—from A…Z and a…z
 B) digits---from 0 to 9.
 C) special characters-- , ; & * ^ : ! + - / [ ] {} () % / \ _ and so on.
 D) White spaces– blank spaces, vertical tab, new line etc.
Tokens:
5 types of tokens in C++ are as follows:-
 Identifiers.
 Keywords
 Constants.
 Strings
 Operators and special characters.

unit-1; lect-2; dr-rajiv


11

 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.

unit-1; lect-2; dr-rajiv


12 Constants

 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.

unit-1; lect-2; dr-rajiv


13

 string constants- a sequence of zero or more set of characters surrounded by double


quotes (“ “).
 E.g.
 “this is a string”
 “ Enter the number”
 The compiler automatically places a NULL character ‘\0’ at the end of each string so that we can
easily detect the end of the string.
 Character constants- a single character may be enclosed within a single quotation marks. It is a
character constant.
 E.g. ‘9’ is a valid character constant.

unit-1; lect-2; dr-rajiv


14 Variables

 A variable is a storage location that contains a data value.


 All variables must be declared before using them.
 Rules for forming variable names:-
 Each variable name begins with a letter or an underscore (_).
 Variable name can have 8 characters (C++ version dependent).
 They are case sensitive. It means that uppercase and lowercase letters are treated as different.
 They cannot be reserved words.
 It cannot have a blank space, period, comma, slash or a semicolon.
 E.g. t, area, length_of_side, radius, rad etc are valid variables while area 2, len.a, for, int are
invalid variables.

unit-1; lect-2; dr-rajiv


15 Data Types in C++

 2 types of data types-


 A) basic data types: like int, char, float, double.
 B) derived data types: array, functions, pointers.
 C) signed and unsigned data types.
 Short, long, unsigned and signed----are type qualifiers; can be used with basic datatypes.
 Qualifiers signed and unsigned, long and short can be used with character and integer basic
data types.
 Qualifier long is used only with double.
 void data type has no qualifier.

unit-1; lect-2; dr-rajiv


16 I. Signed data type (TURBO C++ SIGNED
data types)
Type Size (bytes) Range
signed char 1 -128 to 127
enum 2 -32,768 to 32,767
short 2 -32,768 to 32,767
int 2 -32,768 to 32,767
long 4 -2,147,483,648 to
2,147,483.647
float 4 -3.4E-38 to 3.4E+38
double 8 -1.7E-308 to 1.7E+308
long double 10 -3.4E-4932 to 3.4E+4932

unit-1; lect-2; dr-rajiv


17

 A bit is the lowest unit in memory.


 A bit can be 0 or 1.
 A byte consists of 8 bits.
 It represents 1 character in the ASCII code.
 From table, we see that both short and int take 2 bytes.
 long takes twice the memory reserved for an int.
 double takes twice the float / long data type.
 long double requires higher memory than the double data type.

unit-1; lect-2; dr-rajiv


18 II. Unsigned data types (TURBO C++
UNSIGNED data types) and BOOL type
 The range of unsigned data type is higher as these data types are all positive.
 Unsigned data types have more requirements as the leftmost bit (MSB) is reserved for the
sign.
 E.g. an unsigned int can be twice as large as the signed int. (see table).

Type Size (bytes) Range


unsigned char 1 0 to 255
unsigned short 2 0 to 65,535
unsigned long 4 0 to 4,294,967,295
bool 1 bit 0 or 1

unit-1; lect-2; dr-rajiv


19

 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.

unit-1; lect-2; dr-rajiv


20 Expressions and Assignment statements

 An expression is a combination of constants, variables, function calls and operators that


when evaluated returns a value.
 E.g.
 count + 5;
 numb * 10;
 An assignment statement is used to assign a value.
 E.g. count = count + sum;
 numb = numb * 10;

These assignment statements work from right to left i.e. the value of rhs is assigned and stored in lhs
variable.

unit-1; lect-2; dr-rajiv

You might also like