0% found this document useful (0 votes)
130 views9 pages

Example Programs 3 Function Overloading

The document contains 8 code examples demonstrating different C++ programming concepts: 1. Using call by value, call by address, and call by reference to swap two numbers. 2. Overloading functions to swap integers, floats, and characters. 3. Using an inline function to find the area of a rectangle. 4. Overloading functions for addition of integers and floats. 5. Overloading functions to find the area of a circle, rectangle, and triangle. 6. Returning a function by reference to calculate the square and cube of a number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views9 pages

Example Programs 3 Function Overloading

The document contains 8 code examples demonstrating different C++ programming concepts: 1. Using call by value, call by address, and call by reference to swap two numbers. 2. Overloading functions to swap integers, floats, and characters. 3. Using an inline function to find the area of a rectangle. 4. Overloading functions for addition of integers and floats. 5. Overloading functions to find the area of a circle, rectangle, and triangle. 6. Returning a function by reference to calculate the square and cube of a number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1. C++ Program to swap two numbers using Call by Value.

#include<iostream>

using namespace std;

#include<conio.h>

void swap(int,int);

int main()

int a,b;

cout<<"Enter 2 numbers";

cin>>a>>b;

cout<<"\n Before swapping "<<"a= "<<a<<" b= "<<b;

swap(a,b);

cout<<"\n After swapping "<<"a= "<<a<<" b= "<<b;

getch();

void swap(int c,int d)

int temp;

temp=c;

c=d;

d=temp;

cout<<"\n After swapping "<<"c= "<<c<<" d= "<<d;

}
2. C++ Program to swap two numbers using Call by Address.

#include<iostream>

using namespace std;

#include<conio.h>

void swap(int *,int *);

int main()

int a,b;

cout<<"Enter 2 numbers";

cin>>a>>b;

cout<<"\n Before swapping "<<"a= "<<a<<" b= "<<b;

swap(&a,&b);

cout<<"\n After swapping "<<"a= "<<a<<" b= "<<b;

getch();

void swap(int *c,int *d)

int temp;

temp=*c;

*c=*d;

*d=temp;

}
3. C++ Program to swap two numbers using Call by Reference.

#include<iostream>

using namespace std;

#include<conio.h>

void swap(int &,int &);

int main()

int a,b;

cout<<"Enter 2 numbers";

cin>>a>>b;

cout<<"\n Before swapping "<<"a= "<<a<<" b= "<<b;

swap(a,b);

cout<<"\n After swapping "<<"a= "<<a<<" b= "<<b;

getch();

void swap(int &c,int &d)

int temp;

temp=c;

c=d;

d=temp;

}
4. C++ Program to swap two integers, two floating-point numbers and two characters using
function overloading.

#include<iostream>

using namespace std;

void swap(int &,int &);

void swap(float &,float &);

void swap(char &,char &);

int main()

int ia,ib;

float fa,fb;

char ca,cb;

cout<<"Enter two integers: ";

cin>>ia>>ib;

cout<<"\n Before Swapping of integers: ";

cout<<ia<<" "<<ib;

swap(ia,ib);

cout<<"\n\n After Swapping of integers: ";

cout<<ia<<" "<<ib;

cout<<"\n\n Enter two floating-point numbers: ";

cin>>fa>>fb;

cout<<"\n Before Swapping of floats: ";

cout<<fa<<" "<<fb;

swap(fa,fb);
cout<<"\n\n After Swapping of floats: ";

cout<<fa<<" "<<fb;

cout<<"\n\n Enter two characters: ";

cin>>ca>>cb;

cout<<"\n Before Swapping of characters: ";

cout<<ca<<" "<<cb;

swap(ca,cb);

cout<<"\n\n After Swapping of characters: ";

cout<<ca<<" "<<cb;

void swap(int &p, int &q)

int temp=p; p=q; q=temp;

void swap(float &a, float &b)

float temp=a; a=b; b=temp;

void swap(char &m, char &n)

char temp=m; m=n; n=temp;

}
5. C++ Program to find the area of a rectangle using an inline function.

#include<iostream>

using namespace std;

float area(float,float);

int main()

float l,b;

cout<<"Enter the length and breadth of the rectangle: ";

cin>>l>>b;

cout<<"The area of the rectangle is: "<<area(l,b)<<" sq. units";

inline float area(float len, float br)

return(len*br);

6. C++ Program for addition of integers and floating-point numbers using function overloading.

#include<iostream>

using namespace std;

int sum(int,int,int);

float sum(float,float,float);

int main()

int ia,ib,ic,isum;
float fa,fb,fc,fsum;

cout<<"Enter three integers: ";

cin>>ia>>ib>>ic;

cout<<"The integer sum is: "<<sum(ia,ib,ic);

cout<<"\n Enter three floats: ";

cin>>fa>>fb>>fc;

cout<<"The float sum is: "<<sum(fa,fb,fc);

int sum(int p, int q, int r)

return(p+q+r);

float sum(float x, float y, float z)

return(x+y+z);

7. C++ Program to find the area of a circle, rectangle and triangle using function overloading.

#include<iostream>

using namespace std;

#include<math.h>

#define PI 3.142

float area(float);

float area(float,float);
float area(float,float,float);

int main()

float l,b,r,s1,s2,s3;

cout<<"Enter the radius of a circle: ";

cin>>r;

cout<<"Enter the length and breadth of a rectangle: ";

cin>>l>>b;

cout<<"Enter the three sides of a triangle: ";

cin>>s1>>s2>>s3;

cout<<"\n The area of the circle is: "<<area(r)<<" sq. units";

cout<<"\n The area of the rectangle is: "<<area(l,b)<<" sq. units";

cout<<"\n The area of the triangle is: "<<area(s1,s2,s3)<<" sq. units";

float area(float radius)

return(PI*radius*radius);

float area(float length, float breadth)

return(length*breadth);

float area(float a, float b, float c)


{

float s=(a+b+c)/2.0;

return(sqrt(s*(s-a)*(s-b)*(s-c)));

8. C++ Program to return a function by reference.

#include<iostream>

using namespace std;

void sqcb(int,int &,int &);

int main()

int num,sq,cb;

cout<<"Enter a number: ";

cin>>num;

sqcb(num,sq,cb);

cout<<"Square of "<<num<<" is: "<<sq<<endl;

cout<<"Cube of "<<num<<" is: "<<cb;

return 0;

void sqcb(int n, int &s,int &c)

s=n*n;

c=n*n*n;

You might also like