Oops Module 2
Oops Module 2
for loop
Execute a sequence of statements multiple times and abbreviates the code that manages the
loop variable.
do...while loop
Like a ‘while’ statement, except that it tests the condition at the end of the loop body.
While loop
The syntax of a while loop in C++ is
while(condition)
i=1;
{ while (i<=10)
{
statement(s);
cout<<i;
} i++;
}
Do -while
The syntax of a while loop in C++ is
do
i=1;
{ do
{
statement(s);
cout<<i;
} i++;
}
while(condition) While(i>=10)
For loops
The syntax of a while loop in C++ is
return 0;
}
DECISION MAKING STATMENTS
conditional statements
goto Statement
# include <iostream>
using namespace std;
int main() Output
{ int I, num ,average, ,sum;
for(i = 1; i <= 10; ++i) Sum 1 to 7 = 28
{ if( i==8)
{ goto a;
}
sum =sum + i;
}
cout << "\nsum = " << sum;
a:cout<<“sum 1 to 7”<<sum;
return 0
;
}
for(i = 1; i <= n; ++i) { cout << "Enter n" << i << ": "; cin >> num; if(num < 0.0) { // Control of the program move to jump: goto jump; } sum += num; } jump: average = sum / (i - 1); cout << "\nAverage = " << average; return 0; }
break Statement
# include <iostream>
using namespace std;
int main() Output
{ int I, num ,average, ,sum;
for(i = 1; i <= 10; ++i) sum = 28
{ if( i==8)
{ break;
}
sum =sum+i;
}
cout << "\nsum = " << sum;
return 0
;
}
Continue Statement
# include <iostream>
using namespace std;
int main() Output
{ int I, num ,average, ,sum;
for(i = 1; i <= 10; ++i) sum = 47
{ if( i==8)
{ continue;
}
sum =sum+i;
}
a: cout << "\nsum = " << sum;
return 0
;
}
Array and Strings
ARRAY
Introduction to array
types of array
syntax of Array declaration
example
c program to Martrix (single,double dimenension) operations
STRING
Introduction
String operation
String header file
Functions
C++ Functions
A Function is a sub-program that acts on data and often returns a value. Large programs are generally avoiding
because it is difficult to manage a single list of instructions. Thus, a large program is broken down into smaller
units known as functions. A functions is a named unit of a group of program statements. This unit can be
invoked from other parts of the program.
Why to use Functions ?
The most important reason to use functions is to make program handling easier as only a small part of the
program is dealt with at a time, thereby avoiding ambiguity. Another reason to use functions is to reduce
program size. Functions make a program more readable and understandable to a programmer thereby making
program management much easier.
C++ Function Types
There are two types of functions: library functions and user-defined functions
*Refer your class notes
C++ function prototype
A function prototype is a declaration of the function that tells the program about the type of value
returned by the function and the number and type of arguments.\
Function prototyping is one very useful feature of C++ functions. A function prototype describes
the function interface to the compiler by giving details such as the number and type of arguments
and the type of return values.
Function prototype has the following parts:
•return type
•name of the function
•argument list
Syntax returntype functionname(Argument list)
C++ Standard Library Functions
{
int fib(int);
int i=0, n;
cout<<"Enter any number: ";
cin>>n;
while(i<=n)
{
cout<<" " <<fib(i);
i++;
}
return 0;
}
int fib(int n)
{
int f;
if ((n==1)|| (n==0))
return(n);
else
return(fib(n-1)+fib(n-2));
}
Write a program to print fibonaci series using recursion
Write a program to solve towers of Hanoi using recursion
The program which related to Tree concepts in data structures is based on recursion
Inline functions-reduce function call
overhead
The main use of the inline function in C++ is to save memory space. Whenever the function is called, then it takes a lot of time to execute the
tasks, such as moving to the calling function.
It will be greater than the time taken required to execute that function. use inline function if function called many times
In the inline function, we do not need to call a function, so it does not cause any overhead.
It does not require any stack on which we can push or pop the variables as it does not perform any function calling.
An inline function is mainly beneficial for the embedded systems as it yields less code than a normal function.
If a function is recursive.
It is the default storage class for all local variables. The auto keyword is applied to all local
variables automatically.
#include <stdio.h>
int main( ) The variables defined using auto storage class are called as local
{ variables.
auto int j = 1; Auto stands for automatic storage class.
{ A variable is in auto storage class by default if it is not explicitly specified.
auto int j= 2; The scope of an auto variable is limited with the particular block only.
{ Once the control goes out of the block, the access is destroyed. This
auto int j = 3; means only the block in which the auto variable is declared can access it.
printf ( " %d ", j);
}
printf ( "\t %d ",j);
}
printf( "%d\n", j);
}
Output 3 2 1
Register Storage Class
The register variable allocates memory in register than RAM. Its size is same of register size. It
has a faster access than other variables.
It is recommended to use register variable only for quick access such as in counter.
Example
register int counter=0;
Static Storage Class
The static variable is initialized only once and exists till the end of a program.
It retains its value between multiple functions call.
The static variable has the default value 0 which is provided by compiler.
#include <iostream>
using namespace std; int main()
void func() {
{ func();
static int i=0; //static variable func();
int j=0; //local variable func();
i++; }
j++;
cout<<"i=" << i<<" and j=" <<j<<endl;
}
External Storage Class
The extern variable is visible to all the programs. It is used if two or more files are sharing same variable or
function.
The extern storage class is required when the variables need to be shared across multiple files.
Extern variables have global scope and these variables are visible outside the file in which they are declared.
The extern variable is visible to all the programs. It is used if two or more files are sharing the same variable or
function.
The lifetime of the extern variables is as long as the program in which it is declared is terminated
#include<iostream>
using namespace std; int main()
class test {
{
mutable int a; const test x(2,3);
int b; cout<<"Initial value"<<endl;
public:
test(int x,int y) x.display();
{
a=x;
x.square_a();
b=y; cout<<"Final value"<<endl;
}
void square_a() const
x.display();
{ return 0;
}
a=a*a; }
void display() const
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
}
Storage Class Keyword Lifetime
Automatic auto Function Block
External extern Whole Program
Static static Whole Program
Register register Function Block
Mutable mutable Class