Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
KOVAIPUDUR,COIMBATORE – 42
Presented by,
Sangeetha T
Assistant Professor,
Dept of IT,
SKCET.
1
i ng
m m
gr a
P ro
Introduction
Smartphones,
home appliances,
commercial tools, etc.
Applications
Servers
System Programs
s
ig m
a d
a r
g P
in
m
a m
g r
ro
P
Definition
9
Monolithic Programming
• The program is written with a single function
• The data variables are declared as global & the statements are
written in sequence
10
CONTD…
• The global data can be accessed from any portion of the program
• Eg:FORTAN,BASIC,ASSEMBLY LANGUAGE
11
Procedural Programming
(Procedure oriented)
Top down approach
Procedures, also known as functions or methods simply contains a
series of computational(Algorithmic) steps to be carried out.
procedural programming specify the syntax and procedure to write
a program.
Big program is a divided into small pieces.
Functions are more important than data.
Input- arguments, output-return values.
12
Contd…
Contd…
14
Structured Programming Concept(Modular
Prog.)
Structured programming techniques assist the programmer in
writing effective error free programs.
15
Contd…
After a piece has been tested and studied in detail individually,
it is than integrated into the overall program structure.
• Decision Structure(if-else)
16
17
Elements of OOP
Definition of OOP:
“Object oriented programming is a programming methodology that
associates data structures with a set of operators which act upon it.”
Basic Concepts of OOPS
• Objects
• Classes
• Data Abstraction and Encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Objects
• OOP uses objects as its fundamental building blocks.
• Objects are the basic run-time entities in an object-oriented
system.
• Every object is associated with data and functions which define
meaningful operations on that object.
• Object is a real world existing entity.
• Object is an Instance of a particular class.
Contd…
24
Object
Operation
Operation
Example: StudentObject
Enroll()
st_name
st_id
Performa Displayinfo
branch
nce() ()
semester
Result()
Cont…
Class
• Common behaviors
• sleep, sit, eat,run
Contd…
Abstraction
• It binds together the data and functions that manipulate the data,
and that keeps both safe from outside interference and misuse.
Cont…
Point
Line
Types of Inheritance
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance
Polymorphism
• Polymorphism means that the same thing can exist in two forms.
Performance
MgmtObject Performance
Result
Overview of c++
C is the function or procedure programming language. C++ is the object oriented programming language.
In this programming, programs are divided into modules and In this programming, programs are divided into classes and
functions. functions.
C program file is saved with .C extension. C++ program file is saved with .CPP extension.
67
Character Set
• C++ The characters in C++ are grouped into the following categories:
68
Types Character Set
Lower Case Letters a-z
Upper Case Letters A-Z
Digits 0-9
Special Characters !@#$%^&*
White Spaces Tab Or New line Or Space
C++ Character set
A – Z= 26 = 65 to 90
a – z = 26 = 97 to 122
0 – 9 = 10 = 48 to 57
Symbols = 34= other values ASCII Values
TOTAL = 96
Character Set
• Valid C++ Characters: Special Characters are listed below:
Symbol Meaning Sym Meaning Symbol Meaning
bol
70
! Exclamation mark | Vertical bar ; Semicolon
Opening angle
# Number sign \ Backslash <
bracket
$ Dollar sign ` Apostrophe > Closing angle bracket
% Percent sign – Minus sign ? Question mark
^ Caret = Equal to sign , Comma
& Ampersand { Left brace . Period
* Asterisk } Right brace / Slash
( Left parenthesis [ Left bracket
_ Underscore : Colon
Tokens
1. Keywords
int ,switch,class
2.Variables
marks,age,name
3.Constants
121,350.75,2e3
4.Special Characters
#,?,~
5.Operators
+,-,: ?
71
RESERVED KEYWORDS
• Programmer-designed tokens
• Meaningful & short
• Long enough to understand
• C++ rules for Identifiers
- alphabets, digits, underscore
- should not start with digits.
- Case sensitive
- Unlimited length
- Declared anywhere
Variable in C++
Variable declarations
•This is the process of allocating sufficient memory space for the
data in term of variable.
Syntax
Datatype variable_name;
eg: int a;
Variable initialization
•It is the process of allocating sufficient memory space with user
defined values.
Syntax
Datatype variable_name=value;
eg: int b = 30;
75
CONTD…
Variable assignment
•It is a process of assigning a value to a variable.
Syntax
Variable_Name = value
Example
int a= 20;
int b;
76
C++ Constants/literals
• Is a symbol that takes more than one operands & operates on them
to produce a result.
- Arithmetic
- Relational
- Logical
- Assignment
- increment/Decrement
- conditional
- scope resolution(::)
- special operators: new, delete, endl, setw
S. No OPERATORS SYMBOLS
1. Arithmetic +,-,/,*,%
2. Logical &&,||,!
3. Relational <,>,>=,<=,==,!=
4. Assignment =
5. Increment ++
6. Decrement --
7. Comma ,
8. Conditional (Ternary) ?:
9. Bitwise &,|,^,!,>>,<<
10. Special Operator Sizeof
11. Extraction >>
12. Insertion <<
13. Dynamic Memory Allocator New
14. Dynamic memory De-allocator Delete
SEPARATORS
83
C++ Data types
S. No DATA TYPE Size (in bytes) RANGE
• cout
• cin
• Operator << (Insertion)
• Operator >> (Extraction)
• endl
87
C vs C++
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<”enter the number”<<endl;
cin>>a>>b;
c=a+b;
cout <<“the sum is”<<c;
return 0;
}
Contd…
#include <iostream>
int main()
{
int a,b,c;
std::cout<<”enter the number”;
cin>>a>>b;
c=a+b;
std::cout <<“the sum is”<<c;
return 0;
}
90
In trubo c++
#include <iostream.h>
#include<conio.h>
int main()
{
int a,b,c;
clrscr();
cout<<”enter the number”<<endl;
cin>>a>>b;
c=a+b;
cout <<“the sum is”<<c;
getch();
return 0;
}
91
Control Structures in C++
Control structures control the flow of execution in a program or
function.
• A combination of individual instructions into a single logical unit with one entry point
and one exit point
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Control Structures in C++
A realistic C program may require that a logical test be carried out at
some particular point within the program.
One of several possible actions will then be carried out depending on
the outcome of the logical test. This is known as branching.
93
There is also a special kind of branching, called selection, in which one
group of statements is selected from several available groups.
In addition, the program may require that a group of instructions be
executed repeatedly, until some logical condition has been satisfied.
This is known as looping.
UNI
T
III /
DEP
T OF
IT /
SKC
ET
C++ STATEMENTS
C++ stmts
Control
stmt
If contin
If switch while do for break
ue
return
else
Three kinds of execution flow
◦ Sequence:
The execution of the program is sequential.
◦ Selection / Branching
A control structure which chooses alternative to execute/program chooses to follow one branch or
another
UNI
T
III /
DEP
T OF
IT /
SKC
ET
The simple if Statement
• The if statement has the following syntax:
if ( condition )
statement;
UNI
T
III /
DEP
T OF
IT /
SKC
ET
The if-else Statement- flow
#include <iostream>
using namespace std;
int main()
{
int age;
cin>>age;
if (age>= 18)
{
cout<<“Eligible";
}
else
cout<<“Not eligible”;
}
OUTPUT 1 OUTPUT 2 UNI
T
Age=30 Age=12 III /
Eligible Not Eligible DEP
T OF
IT /
SKC
ET
The if-else Statement-Program to find the largest of 2 numbers
#include <iostream>
using namespace std;
int main( )
{
int a,b ;
cout<<"Enter numbers a and b\n";
cin>>a>>b;
if(a>b)
cout<<“ largest is:“<<a;
else
cout<“largest is:“<<b;
UNI
return 0; T
III /
} DEP
T OF
IT /
SKC
ET
The if-else Statement-Program to find the given number is odd or even
int main( )
{
int num;
cout<<"Enter a number you want to check.\n“;
cin>>num;
if((num%2)==0) //checking whether remainder is 0 or not.
cout<<“ The even no is:”<<num;
else
cout<<“the odd no is:“<<num;
return 0;
}
OUTPUT
Enter a number you want to check. UNI
25 T
III /
25 is odd.
DEP
Enter a number you want to check. T OF
2 IT /
2 is even. SKC
ET
Sample programs
• In a company an employee is paid as under:
• If his basic salary is less than Rs. 1500,
then HRA = 10% of basic salary and
DA = 90% of basic salary.
• If his salary is either equal to or above Rs. 1500,
then HRA = Rs. 500 and DA = 98% of basic salary.
• If the employee's salary is input through the keyboard write a program
to find his gross salary.
UNI
T
III /
DEP
T OF
IT /
SKC
ET
(if...elseif....else / else-if ladder Statement)
The if...else if statement is used when program requires more than one test
expression
if (test expression1)
{
statement/s to be executed if test expression1 is true;
}
else if(test expression2)
{
statement/s to be executed if test expression1 is false and 2 is true;
}
.
.
.
else
{
UNI
statements to be executed if all test expressions are false; T
} III /
DEP
T OF
IT /
SKC
ET
To print given number is positive or negative or zero-
using elseif ladder
#include <iostream>
using namespace std; else if (num < 0)
int main() {
{ cout<<“The number is
int num; negative\n“<<num;
cout<<"Enter a number \n";
}
cin>>num;
if (num > 0)
else if (num = = 0)
{ {
cout<<“The number is cout<<“number is zero
positive\n“<<num; “;
}
} UNI
cout<<“Thank you”; IIIT /
} DEP
T OF
IT /
SKC
ET
Calculate the EB bill using if-else-if ladder
UNI
T
III /
DEP
T OF
IT /
SKC
ET
e n t
t e m
h Sta
itc
Sw
109
Selection-Using Switch-CASE
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Switch Statement Syntax
switch(expr/var)
{
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
} UNI
T
III /
DEP
T OF
IT /
SKC
ET
Flow of execution-switch
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Rules in switch
• All case expressions must be different. Expression must evaluate to an integer.
• First the expression is evaluated. Then the value of expression is compared with the
case expressions. The execution begins at the case statement, whose case expression
113
matches. All the statements below are executed.
• If default is present and if no other case matches then default statement is executed
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Rules-Contd….
You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
114
The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Rules-Contd…
When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow
of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is needed
in the default case.
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Example 1
#include <iostream>
case 4:
using namespace std; cout<<“Thursday\n”;
cin>>day; break ;
switch ( day ) case 5:
{
cout<<“Friday\n”;
case 0: break ;
cout<<“Sunday\n” ;
case 6:
break ;
cout<<“Saturday\n”;
case 1: break ;
cout<<“Monday\n” ;
default:
break ;
cout<<“Error -- invalid
case 2: day.\n”;
cout<<“Tuesday\n”; break ;
break ; }
case 3:
cout<<“Wednesday\n”;
break ;
116
Example 2
#include <iostream> case'e':
using namespace std; case‘I':
int main() case'i':
case'O':
{
case'o':
char a; case'U':
cout<<"Enter any Alphabet : "; case'u': cout<<"It is a Vowel";
cin>>a; break;
default: cout<<"It is a Consonent";
switch (a)
}
{ }
case'A':
case'a':
case'E':
117
Example 3
#include <iostream> cout<<"Well done\n" ;
using namespace std; break;
int main () case 'D' :
{ cout<<"You passed\n" ;
char grade; break;
cin>>grade; case 'F' :
switch(grade) cout<<"Better try again\n" ;
{ break;
case 'A' : default :
cout<<"Excellent!\n" ; cout<<"Invalid grade\n" ;
break; }
case 'B' : cout<<"Your grade is %c\n", grade ;
}
case 'C' :
118
Example 4 Menu driven Calculator
#include <iostream> case 2:c=a-b;
using namespace std; cout<<“Difference is :\n“<<c ;
int main () break;
{ case 3:c=a*b;
int a,b,c,choice; cout<<“Multiply is :\n“<<c ;
cin>>a>>b; break;
cout<<“Menu \n”<<“1.Add case 4:c=a/b;
“<<“2.Sub”<<“ 3.Mul cout<<“Division is :\n“<<c;
“<<“4.Div” <<“5.Mod\n”;
break;
cin>>choice;
case 5:c=a%b;
switch(choice)
cout<<“Remainder is :\n“<<c ;
{
break;
case 1 :c=a+b;
default :
cout<<“Sum is :\n“<<c );
cout<<"Invalid Choice\n" ; } }
break;
119
Switch vs if-else ladder
• Cannot do with switch:
1. A float expression cannot be tested .
2. Cases cannot have variable expressions(eg: case a+3)
3. Multiple cases cannot use same expressions
Eg: case 3: case 1+2:
• Switch is faster than if-else ladder when more conditions to be tested
(because switch matches the value but in if-else all conditions are
evaluated)
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Switch vs if-else ladder
• A nested if-else structure is just as efficient as a switch
statement.
• In most software, the statements in the program may need to repeat for
many times.
• e.g., calculate the Simple interest for 10 users.
• It’s not elegant to write the code 10 times.
• Drawback:
• Lines of code increases
• Memory space wasted
• Compilation time increases
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Decision Making- LOOPING
Repetition / Looping
A control structure which repeats a group of statements.
Example:
UNI
T
III /
DEP
T OF
IT /
SKC
ET
While Loop
It is pre tested loop. It is used when we have to execute a part of code for
‘n’ numbers of times until the condition is true.
124
Syntax:
while (Expression/Condition)
{
Loop body/Statements;
}
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Calculate Sum of digits in a Number -while
#include<iostream>
using namespace std;
int main()
{
int n, digit, temp, sum=0;
cout<<"Enter the Any Integer No: \n";
cin>>n;
temp=n;
while(n!=0)
{
digit = n%10;
sum=sum+ digit;
n=n/10;
}
cout<<“Number is :\n“<<temp;
cout<<"Sum of digit is:\n“<<sum;
}
UNI
T
III /
DEP
T OF
IT /
SKC
ET
Calculate reverse of a Number(palindrome)-
while loop
#include<iostream>
using namespace std;
int main()
{
int n, rev = 0, rem,temp;
cout<<"Enter an integer: ";
cin>>n;
temp=n;
while(n != 0)
{
rem = n%10;
rev = rev*10 + rem;
n =n/ 10; UNI
if(temp==rev) T
}
cout<< temp<<is a palindrome”,; III /
cout<<“Number is :\n“<<temp; DEP
cout<<"Reversed Number = “<< rev; T OF
IT /
} SKC
ET
To check given number is armstrong or not
void main()
{
int n, temp, rem,result=0;
cout<<"Enter a three digit integer: ";
cin>>n;
temp= n;
while (n!= 0)
{
rem = n%10;
result =result+ rem*rem*rem;
n=n/10;
}
if(result == temp)
cout<<temp<<“ is an Armstrong number";
else
cout<<temp<<“is not an Armstrong number";
} UNI
T
III /
DEP
T OF
IT /
SKC
ET
To check given number is prime or not
void main( )
{
int num, i ;
cout<<"Enter a number " ;
cin>>num ;
i=2;
while ( i <= num - 1 )
{
if ( num % i == 0 )
128
{
cout<< "Not a prime number" ;
break ;
}
i++ ;
}
if ( i == num )
cout<<"Prime number" ;
}
UNI
T
III /
DEP
T OF
IT /
10/17/2016 PSTC SKC
ET
do...while loop
Syntax:
do
{
statement(s);
UNI
} T
III /
while( condition ); DEP
T OF
IT /
SKC
ET
Calculate factorial -while loop
#include<iostream>
using namespace std; Sample output:
int main() Enter a number: 5
{ Factorial of 5 is: 120
int i=1,fact=1,num;
cout<<"Enter a number: ";
cin>>num;
while(i<=num)
{
fact=fact*i;
i++;
} UNI
T
cout<<"Factorial is:“<<num<<fact; III /
DEP
} T OF
IT /
SKC
ET
For loop
• A for loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times.
• SYNTAX:
for (initialization expression;
loop repetition condition;
update expression)
{
Statements;
}
UNI
T
III /
DEP
T OF
IT /
SKC
ET
To print Fibonacci series
#include<iostream>
using namespace std;
int main()
{
int n, first = 0, second = 1, next, c;
cout<<"Enter the number of terms\n";
cin>>n;
cout<<"First terms of Fibonacci series are \n“<<n;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout<<“\n“<<next); UNI
T
}
III /
return 0; DEP
} T OF
IT /
SKC
ET
•An Array is a collection of same data type. The elements of an
array are referred by a common name and are differentiate from
one another by their position with in an array. The elements of
an array can be of any data type but all elements in an array must
be of the same type.
The general form of declaring a array is
type array_name[size];
where type is a valid datatype, array_name is the name of the
array and size is the number of elements that array_name
contains.
Example:
int A[100];
int data type of elements that an array
A name of array 100 size of an array
The individual elements of an array can be referenced by means of its subscript (or
index)
Suppose A is an array of 20 elements, we can reference each element as
A[0] 1st element
A[1] 2nd element
A[2] 3rd element
:
:
A[19] 20th element
Note: Subscript enclosed within parenthesis.
In C subscript starts from 0. That is, if we declare an array of size n, then we can refer
the elements from 0 to (n-1)th element.
Arrays are of 3 types. They are
Single Dimensional Array
Two Dimensional Array
Multi Dimensional Array
•The general form of Single Dimensional array is:
data type variable[size];
Example:
int A[20];
139
Inserting an elements in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,pos,item,n;
clrscr();
printf("Enter the no of elements in an array \n");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
140
printf("enter the item to be inserted");
scanf("%d",&item);
printf("enter the position to be inserted");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
n++;
a[i+1]=item;
for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
printf("\n");
getch();
}
141
Deleting an elements in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,pos,item,n;
clrscr();
printf("Enter the no of elements in an array \n");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
142
printf("enter the item to be deleted");
scanf("%d",&item);
printf("enter the position to be deleted");
scanf("%d",&pos);
for(i=pos;i<n;i++)
{
a[i]=a[i+1];
}
n--;
for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
printf("\n");
getch();
}
143
•The general form of Two-Dimensional Arrays is
type array name[row_size][column size];
Example:
int a[2][2];
• Transpose
b[j][i]=a[i][j] or b[i][j]=a[j][i]
• Diagonal
if(i==j)
{
sum=sum+a[i][j];
}
147
Matrix Multiplication
#include<iostream.h>
#include<conio.h>
void main()
{
int row1,col1,row2,col2;
int i,j,k;
int mat1[5][5],mat2[5][5],multi[5][5];
clrscr();
cout<<"enter the row for first matrix\n";
cin>>row1;
cout<<"enter column for first matrix\n";
cin>>col1;
cout<<"enter elements of first matrix\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
cin>>mat1[i][j];
}
}
cout<<"enter the row of second matrix\n";
cin>>row2;
cout<<"enter column for second matrix\n";
cin>>col2;
cout<<"enter elements of second matrix\n";
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
cin>>mat2[i][j];
}
}
if(col1==row2)
{
cout<<"multiplication of matrices is\n";
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
multi[i][j]=0;
for(k=0;k<col1;k++)
{
multi[i][j]+=mat1[i][k]*mat2[k][j];
}
cout<<multi[i][j];
}
cout<<endl;
}
}
else
cout<<"multiplication is not compatible";
getch();
}
Functions are building blocks of C. Function performs the
same set of instructions on different sets of data or at different
portions of a program.
C functions can be classified into two categories, namely
1.Library functions and
2.User-defined functions.
main is an example of user-defined functions.
printf and scanf belong to the category of library functions.
The main distinction between these two categories is that
library functions are not required to be written by us whereas a
user-defined function has to be developed by the user at the
time of writing a program.
It facilitates top-down modular programming. In this
programming style, the high level logic of the overall problem is
solved first while the details of each lower-level function are
addressed later.
The length of a source program can be reduced by using
functions at appropriate places. This factor is particularly
critical with microcomputers where memory space is limited.
Many other programs may use a function. This means that a
C programmer can build on what others have already done,
instead of starting over, from scratch.
As mentioned earlier, it is easy to locate and isolate a faulty
function for further investigations.
Elements of user-defined
functions
1.Function/Prototype Declaration
2.Function Definition
3.Function Call
Function prototype(Declaration):
• Every function in C++ programming should be declared before
they are used. These type of declaration are also called function
prototype. Function prototype gives compiler information about
function name, type of arguments to be passed and return type.
Syntax of function prototype
function_name(argument(1),....argument(n));
int sum ( )
{
int a,b, result;
cout<<"\n Enter the two numbers : ";
cin>>a>>b; /* taking two numbers as input*/
result = a + b ; /*adding two numbers*/
cout<<“the result is”<<result;
}
Program
#include<iostream.h>
using namespace std;
int sum(); /*declaring prototype of function*/
void main()
{
int c;
c=sum( ); /* calling function,
cout<<“the result is “<<c;
}
int sum ( )
{
int a,b,result;
cout<<"\n Enter the two numbers : ";
cin>>a>>b; /* taking two numbers as input*/
result = a + b ; /*adding two numbers*/
return(result);
}
Program
#include<iostream.h>
using namespace std;
void sum(int,int); /*declaring prototype of function*/
void main()
{
int a,b;
cout<<"\n Enter the two numbers : ";
cin>>a>>b; /* taking two numbers as input*/
sum(a,b); /* calling function,
}
• Call by Value
• Call by reference
• In call by value method, the #include <iostream.h>
value of the variable is passed to #include<conio.h>
void swap(int,int);
the function as parameter. void main() {
• The value of the actual int i=7, j=-3;
parameter can not be modified clrscr();
by formal parameter. cout <<"i = "<< i << endl
<<"j = "<< j << endl;
• Different Memory is allocated swap(i,j);
for both actual and formal getch();
parameters. Because, value of }
void swap(int a, int b)
actual parameter is copied to {
formal parameter. int t;
Note: t = a;
a = b;
• Actual parameter – This is the b = t;
argument which is used in cout<<<<“a = "<< a << endl
function call. <<“b = "<< b << endl;
}
• Formal parameter – This is the
argument which is used in
function definition
Program : Pointers as function Parameters
#include <iostream.h>
In functions we can pass the duplicate
#include<conio.h>
values for the actual parameters, but we
void swap(int *, int *);
can pass the address of a variable as an
void main() {
argument to a function in the normal int i=7, j=-3;
fashion. Clrscr();
When we pass addresses to a function, swap(&i,&j);
the parameters receiving the addresses cout <<"i = "<< i <<
should be pointers. The process of endl <<"j = "<< j << endl;
calling a function using pointers to pass getch();
the addresses of variable is known as }
call by address. void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;}
Difference
or
Draw()
Draw()
Draw()
#include <iostream> void display(int var)
using namespace std; {
void display(int); cout << "Integer number: " << var ;
void display(float); }
void display(int, float);
void display(float var)
int main()
{
{ cout << "Float number: " << var ;
int a = 5; }
float b = 5.5;
display(a); void display(int var1, float var2)
display(b); {
display(a, b); cout << "Integer number: " << var1;
cout << " and float number:" << var2;
}
}
Eg
•Swapping of two numbers using function overloading
•Area of various shapes using function overloading
170