You are on page 1of 75

Introduction to OOPS

Semester – I I

Copyright@ Amity University


Examples

Copyright@ Amity University


Example of Constant...

#include <iostream.h> /* Use of constant*/


const float Pi = 3.1415926;
main()
{
float radius;
cout << "Enter the radius of a circle: ";
cin >> radius;
cout << "The area is " << Pi * radius * radius << "\n"
return 0;
}
Copyright@ Amity University
Example of Input/output operator...

#include<iostream.h> /*Use of I/O Operators*/


main()
{
cout << "HelloFriends...\n";
cout << ”Enter Your Name \n";
cin>>name;
return 0;
}

Copyright@ Amity University


Example of Arithmetic Operators ...

#include <iostream.h>
/* Use of arithmetic operator */
main()
{
float result;
result = 1.0 + 2.0 * 3.0 / 4.0;
cout << '\n' << result;
result = 1.0 / 2.0 + 3.0;
cout << '\n' << result;

Copyright@ Amity University


Example of Arithmetic Operators ...

result = (1.0 + 2.0) / 3.0;


cout << '\n' << result;
result = (1.0 + 2.0 / 3.0) + 4.0;
cout << '\n' << result;
return 0;
}

Copyright@ Amity University


Example of Incremental / Decrement Operators ...

#include <iostream.h>
/* Use of incremental operator */
int main()
{
int val = 1;
cout << "\n val is " << val++ << " and then
post- incremented \n";
cout << "val is now " << val << "\n";
cout << "val is pre-incremented to " <<
++val << '\n';
Copyright@ Amity University
Example of Incremental / Decrement Operators ...

cout << "\n val is " << val++ << " and then
post- decremented \n";
cout << "val is now " << val << "\n";
cout << "val is pre-decremented to " <<
++val << '\n';

return 0;
}

Copyright@ Amity University


Example of Relational Operators ...

#include <iostream.h>
/* Use of Relational Operators */
int main()
{
int first, second;
cout << "\nInput two numbers ";
cin >> first >> second;
cout << "First > second has the value "<<
(first > second) << '\n';
cout << "first < second has the value " <<
(first < second) << '\n';
Copyright@ Amity University
Example of Relational Operators ...

cout << "first == second has the value "<<


(first == second)<< '\n';
return 0;
}

Copyright@ Amity University


Example of Variables ...

#include <iostream.h> /* Use of variable*/


int main()
{
int RollNo=10;
float Marks=20.5;
char section=‘A’

cout<<'\n'<< Section<<RollNo<<Marks;
return 0;
}
Copyright@ Amity University
One use for typecasting for is when you want to use
the ASCII characters. For example, what if you want to
create your own chart of all 256 ASCII characters. To
do this, you will need to use to typecast to allow you to
print out the integer as its character equivalent.

Copyright@ Amity University


#include <iostream.h>
int main()
{
for(int x=0; x<256; x++)
{ //The ASCII character set is from 0 to 255
cout<<x<<". "<<(char)x<<" ";
//Note the use of the int version of x to
//output a number and the use of (char) to

Copyright@ Amity University


// typecast the x into a character
//which outputs the ASCII character that
//corresponds to the current number
}
return 0;
}

Copyright@ Amity University


//To print out a message telling which is bigger

int x = 3; int y = 4;
if (x > y) {
cout << "x is bigger than y" << endl;
}
else {
cout << "x is smaller than y" << endl;
}
return 0;
}
Copyright@ Amity University
Example of If condition...
#include <iostream.h>

int main()
{
int your_number;
cout << "Enter a whole number: ";
cin >> your_number;

if (your_number % 2 == 0)
cout << "\nYour number is even\n";

Copyright@ Amity University


Example of If condition...
if (your_number % 2 != 0)
{
cout << "Your number is odd.\n";
}
cout << "That's all!\n";

return 0;
}

Copyright@ Amity University


Example of Switch condition...
#include <iostream.h>
#include <conio.h>
void main()
{ int no;
cout << "Enter number of week's day(1-7)";
cin>>no;
switch (no)
{
case 1: cout << "Sunday \n"; break;
case 2: cout << "Monday \n"; break;

Copyright@ Amity University


Example of Switch condition...
case 3: cout << "Tuesday \n"; break;
case 4: cout << "Wednesday \n"; break;
case 5: cout << "Thursday \n"; break;
case 6: cout << "Friday \n"; break;
case 7: cout << "Saturday\n"; break;
default : cout << "Invalid choice.\n";
}return 0;}

Copyright@ Amity University


Example of For Loop...
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=0;i<10;i++)
{
cout<<endl;

Copyright@ Amity University


Example of For Loop...

for(j=1;j<=i;j++)
{
cout<<"*";
}
}
getch();
}

Copyright@ Amity University


Example of For Loop...

Output :

*
**
***
****
*****
******
*******
********
*********

Copyright@ Amity University


Example of While Loop...
#include <iostream.h>

int main()
{
int number = 1, total = 0;
while (number < 11)
{
total += number;
number++;
}

Copyright@ Amity University


Example of While Loop...
cout << "\nTotal of numbers from 1 to 10 is " << total;

return 0;
}

Copyright@ Amity University


The following do-while loop prints characters from
‘A’ onwards
until the condition ch<=‘Z’ becomes false.
char ch=‘A’;
do {
cout<<“\n” <<
ch;
ch++;
} while
(ch<=‘Z’);
Copyright@ Amity University
The output of the preceding program would be:
A
B
C
...
Z

Copyright@ Amity University


Example of Break/Continue...
#include <iostream.h>
int main()
{
int line=0; char ch;
while(ch!=‘&’)
{ cin>>ch
if(ch==‘Q’)
break;

Copyright@ Amity University


Example of Break/Continue...

if(ch!=‘\n’)
continue;
line++;
}
return 0;
}

Copyright@ Amity University


gets (input a string from the keyboard)
puts(display a string on the screen followed by a newline

Copyright@ Amity University


#include<stdio.h>
#include<string.h>
int main(){
char ch[25]; int len;
cout<<“Enter a string\n”;
gets(ch);
len=strlen(ch);
cout<<“String entered was :”;
puts(ch);
cout<<“length of the string is :”<<len;
return 0;}
Copyright@ Amity University
An Array is a collection of variables of the same data type
that are referenced by a common name.

Copyright@ Amity University


Why do we need arrays?
Arrays are very much useful in a case where quite many
elements of the same data type need to be stored and processed.
(Example : Storing Marks of 50 students)

float marks[50]

The element number in [] are called subscripts or indexes. The indexes


of an element designates its position in the array’s order.

Copyright@ Amity University


Arrays are a kind of composite type because they allow you to
clump a lot of variables together, one right after the other,
under a single identifier name. If you say:

int a[10];

You create storage for 10 int variables stacked on top of each


other, but without unique identifier names for each variable.
Instead, they are all lumped under the name a.

Copyright@ Amity University


To access one of these array elements, you use the same
square-bracket syntax that you use to define an array:

a[5] = 47;

However, you must remember that even though the size of a is


10, you select array elements starting at zero (this is
sometimes called zero indexing), so you can select only the
array elements 0-9

Copyright@ Amity University


Arrays can be :

 Single Dimension Array

 Two Dimension Array

 Multi Dimension Array

Copyright@ Amity University


Single Dimension Array
0 1 2 3 4--------------n
marks

Copyright@ Amity University


• String as an Array
•char name[20];
0 1 2 3 4 - - - - - - - - - - - - - - 20
name t i n a \n

Copyright@ Amity University


Two Dimension Array
0 1 2 3 4 ---------n
0
•char name[10][10];
1
..
.

Copyright@ Amity University


Large programs are generally avoided 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 function is a named unit of a group of program statements.


This unit can be invoked from other parts of the program.

Copyright@ Amity University


Use functions
 To make program handling easier as only a
small part of the program is dealt with at a
time.
 To reduce program size.
 To make program more understandable and
manageable.

Copyright@ Amity University


• A function declaration in C and C++ gives the function name
• The argument types passed to the function,
• The return value of the function.

For example, here is a declaration for a function called func1( ) that


takes two integer arguments (integers are denoted in C/C++ with the
keyword int) and returns an integer:
int func1(int,int);

Copyright@ Amity University


The first keyword you see is the return value
all by itself: int. The arguments are enclosed
in parentheses after the function name in the
order they are used. The semicolon indicates
the end of a statement; in this case, it tells the
compiler “that’s all – there is no function
definition here!”

Copyright@ Amity University


•Function definitions look like function declarations except that they have
bodies.
•A body is a collection of statements enclosed in braces.
•Braces denote the beginning and ending of a block of code.
•To give func1( ) a definition that is an empty body
(a body containing no code), write:
int func1(int length, int width) { }

Copyright@ Amity University


int func1(int length, int width) { }

Notice that in the function definition, the braces replace the semicolon.
Since braces surround a statement or group of statements, you don’t
need a semicolon. Notice also that the arguments in the function
definition must have names if you want to use the arguments in the
function body (since they are never used here, they are optional).

Copyright@ Amity University


A function prototype is a declaration of the
function that tells the
program about the type of the value returned by
the function and
the number and type of each arguments.

Copyright@ Amity University


The prototype declaration looks like a function
definition except that it has no body i.e., its code is
missing. Empty parenthesis in a function means that the
function is without parameters i.e., parameter list is
empty.

Example : int add(int, int);

Copyright@ Amity University


A definition is automatically also a declaration. C++
makes prototyping
essential i.e., for a function, its prototyping must be
provided before its
use. However, if the called function definition
appears before its calling
function’s definition then the called function’s
prototype may be skipped
because a definition itself is a declaration i.e.,
prototype of a function.
Copyright@ Amity University
A C++ function prototype must specify the return value type
of the function. The return type specification precedes the
function name.
To specify that no value is returned, use the void keyword.
This will generate an error if you try to return a value from
the function.

Copyright@ Amity University


Here are some complete function prototypes:

int f1(void); // Returns an int, takes no arguments


int f2(); // Like f1
float f3(float, int, char, double); // Returns a float
void f4(void); // Takes no arguments, returns nothing

To return a value from a function, you use the return statement

Copyright@ Amity University


A function can be invoked in two manners :

 Call By Value

 Call By Reference

Copyright@ Amity University


Terms To Know

 The parameter that appears in a function call statement are actual


parameter

 The parameter that appears in a function definition are formal


parameters

Copyright@ Amity University


The call by value method copies the values of actual parameters
into the formal parameters, that is, the function creates its own
copy of
argument values and then uses them.

Copyright@ Amity University


The main benefit of call by value

method is that you cannot alter the variable that are used to call

the function because any change that occurs inside function is on

the function’s copy of the argument value. The original copy of

the arguments value remains intact.

Copyright@ Amity University


The call by reference method in place copies the values of
actual parameters into the formal parameters, a reference to
the original variable is passed. (reference is an alias (i.e. a
different name) for the predefined variable)That is, the same
variable’s value can be accessed by any of the two names.
When a function is called by reference , then the formal
parameters become references (or alias) to the actual
parameter in the calling function.

Copyright@ Amity University


This means that in the call by reference method, the called
function does not create its own copy of original values,
rather , it refers to the original values only by different
names. Thus the called function works with the original data
and any change in the values gets reflected to the data.

Copyright@ Amity University


Function overloading allows you to have multiple
functions with the
same name. The compiler identifies the function
based on the parameters
to the functions.

Copyright@ Amity University


Overloading is possible if a function has Different set of parameters
in terms of :

• sequence { void add(int,char) & void add(char.int) }

• number { void add(int,int) & void add(int,int,int) }

• type { void add(int,int) & void add(float,float) }

Function overloading is used for the ease of the programmer.


The programmer does not have to remember multiple function names.
Copyright@ Amity University
Function overloading is used for the ease of the programmer.

The programmer does not have to remember multiple function names.

Copyright@ Amity University


Examples

Copyright@ Amity University


Example of an Array ...

#include <iostream>
void main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
for(int i = 0; i < 10; i++)
{
cout << "a[" << i << "] = " << a[i] << endl;
}
}

Copyright@ Amity University


Example of an Array ...

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float marks[50];
(for(int i=0;i<50;i++)
{

Copyright@ Amity University


Example of an Array ...
cout<<“Enter the marks of “<<i<<“th student\n”
cin>>marks[i];
}
cout<<“\n”;
return 0;
}

Copyright@ Amity University


Example of Double Dimension Array ...

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[2][5]={
{‘A’,’M’,’I’,’T’},
{‘A’,’M’,’R’,’I’,’T’}
};

Copyright@ Amity University


Example of Double Dimension Array ...

cout<<“The string in name[0] is” <<name[0]<<“\n”;


cout<<“The string in name[1] is” <<name[1]<<“\n”;
cout<<“The character in name[1][2] is ”
<<name[1][2]<<“\n”;
}

Copyright@ Amity University


Example of Function Declaration & Definition ...

#include<iostream.h>
int add(int,int);
void main()
{
int a=10;
int b=20;
int total=add(a,b);
cout<<total;
}
Copyright@ Amity University
Example of Function Declaration & Definition ...

int add(x,y)
{
int c=x+y;
return c;
}

Copyright@ Amity University


Example Call By Value ...

#include <iostream.h>
int main()
{
int orig=10;
cout<<“The original value is”<<orig<<“/n”;
change(orig);
cout<<“The value after function change() is
over” <<orig<<“/n”;
Copyright@ Amity University
Example Call By Value ...

return 0;
}
void change(int orig)
{
orig=20; cout<< Value of orig in function
change is”<<orig;
return;
}

Copyright@ Amity University


Example Call By Value ...

The original value is 10


Value of orig in function change is 20
The value after function change() is over 10

Copyright@ Amity University


Example Call By Reference ...

#include <iostream.h>
int main()
{
int orig=10;
cout<<“The original value is”<<orig<<“/n”;
change(orig);
cout<<“The value after function change() is
over” <<orig<<“/n”;
return 0;
}
Copyright@ Amity University
Example Call By Reference ...

void change(int &a)


{
a=20; cout<< “Value of orig in function change
is”<<a;
return;
}

Copyright@ Amity University


Example Call By Reference ...

The original value is 10


Value of orig in function change is 20
The value after function change() is over 20

Copyright@ Amity University


Example of Function Overloading ...
#include<iostream.h>
int add(int,int);
int add(int,int,int);
void main()
{
int a=10;
int b=20;
int total=add(a,b);
int total1=add(a,b,total);
cout<<total;
cout<<total1;
}

Copyright@ Amity University


Example of Function Overloading ...
int add(int x,int y)
{
int c=x+y;
return c;
}

int add(int x, int y, int z)


{
int c=x+y+z;
return c;
}
Copyright@ Amity University
Thank You

Copyright@ Amity University

You might also like