OVERLOADING USING C++
/*FUNCTION OVERLOADING AND OPERATOR OVERLOADING*/
PROGRAM:
#include<iostream.h>
#include<conio.h>
float square(int,float);
float square(int,int,float);
class space
{
private:
int x,y,z;
public:
void getdata(int a,int b,int c);
void display(void);
void operator-();
void operator+();
}p;
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout<<"\n X="<<x<<"\n";
cout<<"\n Y="<<y<<"\n";
cout<<"\n Z="<<z<<"\n";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
void space::operator+()
{
x=x+1;
y=y+1;
z=z+1;
}
float square(int a,float s)
{
return((a*a)+(s*s));
}
float square(int x,int y,float z)
{
float temp;
temp=x+y+z;
return(temp*temp);
}
void main()
{
int a,x,y,z,d,n;
float s,asq,bsq;
clrscr();
cout<<”\n FUNCTION OVERLOADING AND OPERATOR OVERLOADING”;
cout<<”\n ________________________________________________________”;
while(n!=3)
{
cout<<"\n 1.FUNCTION OVERLOADING \n 2.OPERATOR OVERLOADING \n 3.EXIT";
cout<<"\n Enetr the choice:";
cin>>n;
switch(n)
{
case 1:
cout<<"\n Enter two integer:"<<"\n";
cin>>a>>d;
cout<<"\n Enter the float value:"<<"\n";
cin>>s;
asq=square(a,s);
cout<<"\n a="<<a<<"\n s="<<s<<"\n And its square="<<asq;
cout<<endl;
bsq=square(a,d,s);
cout<<"\n X="<<a<<"\n";
cout<<"\n Y="<<d<<"\n";
cout<<"\n Z="<<s<<"\n";
cout<<"\n And its sum of square="<<bsq<<"\n";
break;
case 2:
p.getdata(10,-20,30);
cout<<"\n P:\n";
p.display();
+p;
cout<<"\n +p:\n";
p.display();
-p;
cout<<"\n -p:\n";
p.display();
break;
case 3:
break;
default:
cout<<"\n INVALID CHOICE.......!";
break;
}
}
getch();
}
OUTPUT:
FUNCTION OVERLOADING AND OPERATOR OVERLOADING
_________________________________________________________
1.FUNCTION OVERLOADING
2.OPERATOR OVERLOADING
3.EXIT
Enetr the choice:1
Enter two integer:
34
35
Enter the float value:
12.5
a=34
s=12.5
And its square=1312.25
X=34
Y=35
Z=12.5
And its sum of square=6642.25
1.FUNCTION OVERLOADING
2.OPERATOR OVERLOADING
3.EXIT
Enetr the choice:2
P:
X=10
Y=-20
Z=30
+p:
X=11
Y=-19
Z=31
-p:
X=-11
Y=19
Z=-31
1.FUNCTION OVERLOADING
2.OPERATOR OVERLOADING
3.EXIT
Enetr the choice:3
Comments
Post a Comment