0% found this document useful (0 votes)
25 views

Function Overloading

The document discusses function overloading in C++ by defining multiple functions with the same name but different parameters to calculate the area of different shapes. It also demonstrates pass by reference to swap two variables, checking if a number is Armstrong, and passing objects as arguments to a class method.

Uploaded by

ritz rawat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Function Overloading

The document discusses function overloading in C++ by defining multiple functions with the same name but different parameters to calculate the area of different shapes. It also demonstrates pass by reference to swap two variables, checking if a number is Armstrong, and passing objects as arguments to a class method.

Uploaded by

ritz rawat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#function overloading

#include <iostream>

using namespace std;

#include <iostream>

using namespace std;


void area(int base,int height)
{
float Area=0.5*base*height;
cout<<"Area triangle:"<<Area<<endl;
}
void area(int length,float breadth)
{
float Area=length*breadth;
cout<<"Area of rectangle is:"<<Area<<endl;
}
void area(int side)
{
float area=side*side;
cout<<"Area of sqaure is:"<<area;
}

int main()
{
int b,h,l,s;
float breadth;
cout<<"Enter the base and height of traingle"<<endl;
cin>>b;
cin>>h;
cout<<"Enter the sides of traingle"<<endl;
cin>>l;
cin>>breadth;
cout<<"Enter the side of square"<<endl;
cin>>s;
area(b,h);
area(l,breadth);
area(s);
return 0;
}

by reference Swap
#include<iostream>
using namespace std;
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp
}
int main()
{
int x,y;
cout<<"Enter the vale of a and b:"<<endl;
cin>>x;
cin>>y;
swap(x,y);
cout<<"The swapped vales of a and b are:"<<x<<y<<endl;
return 0;
}

Armstrong
#include<iostream>
#include<cmath>
using namespace std;
class Armstrong
{
int number;
public:
void getnumber()
{
cout<<"Enter the number to check"<<endl;
cin>>number;
}
void checknumber()
{
int num=number;
int num2=number,b=0,sum=0;

while(num!=0)
{
sum++;
num=num/10;
}
while(num2!=0)
{
int a=num2 % 10;
b=b+pow(a,sum);
num2=num2 / 10;
}
if(number==b)
cout<<"Number is armstrong: "<<number<<endl;
else
cout<<"Number is not armstrong:"<<number<<endl;
}
};
int main()
{
Armstrong A1;
A1.getnumber();
A1.checknumber();
return 0;
}

Object as argument
#include<iostream>
using namespace std;
class Time
{
int hours;
int minutes;
public:
void gettime(int h,int m)
{
hours=h;
minutes=m;
}
void puttime(void)
{
cout<<"Hours"<<hours<<endl;
cout<<"Minutes"<<minutes<<endl;
}
void sum(Time, Time);//declaration with objects as arguments
};
void Time::sum(Time t1,Time t2)
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
int main()
{ Time t1,t2,t3;
t1.gettime(2,45);
t2.gettime(3,30);

t3.sum(t1,t2);
t3.puttime();
cout<<"T1:";
return 0;
}

You might also like