Function Overloading
Function Overloading
#include <iostream>
#include <iostream>
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;
}