Unit 2 - Basics of C++ Programming
Unit 2 - Basics of C++ Programming
main( ) Function
cout<<a<<b<<c;
cin>>x>>y>>z;
Declaration:
enum shape {circle, square, triangle};
enum Boolean {true, false};
enum switch {on, off};
Eg:
cout << “First Line”;
cout << endl << “Next Line”;
cout << endl << “Third Line”;
\n – Just adds a new line
endl – In addition to inserting a new line, it also
flushes the stream (buffer)
(2) setw
This manipulator changes the field width of
output
The setw manipulator causes the number (or
string) that follows it in the stream to be printed
within a field n characters wide, where n is the
argument to setw(n)
The value is right-justified within the field
Syntax:
if (condition)
{
//statements
}
The statements within the statement block
execute only if the ‘condition’ results to Boolean
true
Prepared by Sherin Joshi
(2) if…else
This construct is suitable if there are mutually
dependent conditions that need to be checked
to decide which statement block to execute
Syntax:
if (condition) else
{ {
//statements1 //statements2
} }
if (condition1) ………
{ else if (condition N)
//statements1 {
} //statementsN
else if(condition 2) }
{ else
//statements2 {
}
//final statement block
………
}
Prepared by Sherin Joshi
(3) Switch Case
statements can be
any valid statement like declaration and
assignment of any variable
any function call
instantiation of class
break exits the switch case block and the
statement immediately after the switch case
block will get executed
default need not be written at last compulsorily
Prepared by Sherin Joshi
Classwork
Syntax:
for(initialization; test_condition; increment/decrement)
{
//statements
}
initialization – Section for loop declaration, assigning
and re-assigning variables
test_condition – Any valid expression that results to
Boolean true or false can be used (including
function that returns a Boolean value)
Prepared by Sherin Joshi
for Loop
Syntax:
while(condition)
{
//statements
//change in loop variable
}
data_type arr_name[dim1][dim2]…[dimN];
//multi dimensional
int main( )
{
repchar( ); //prints 45 exclamation marks
repchar(‘=’); //prints 45 equal signs
repchar(‘+’, 30); //prints 30 plus signs
return 0;
} Prepared by Sherin Joshi
void repchar(char ch, int n)
{
for(int j=0; j<n; j++)
cout<<ch;
cout << endl;
}
int& setx( )
{
return 3; //not possible
}
int& setx( )
{
int x = 3;
return x; //error
}
Example declarations:
25
0073H
int value[10];
int *ptr;
ptr+6 == &value[6]
*ptr == value[0]
*(ptr + 3) == value[3]
*(ptr + 7) == value[7]
Prepared by Sherin Joshi
//Reading values of an array using pointer
#include <iostream>
using namespace std;
int main( )
{
int myArr[5] = {5,6,7,8,9};
int *ptr;
ptr = &myArr[0]; //or, ptr = myArr;
for(int i = 0; i<5; i++)
cout << *((ptr)+i) << endl;
return 0;
} Prepared by Sherin Joshi
Pointer to Arrays
Then,ptr++ == &value[0][1];
ptr+6 == &value[2][0];
ret_type func_name
(ret_type(*func_ptr)(list_of_args),other_arg_types);
int main( ) {
int a = 10, b = 20, n = 3;
ptrsum = ∑
calculate(ptrsum, a, b, n);
return 0;
} Prepared by Sherin Joshi
int sum(int x, int y)
{
return (x+y);
}