In this program we will first create a class demo that
contains two float data members a and b. Values of objects d1 and d2 are
entered by user and then arithmetic operations are performed on them by
overloading binary operators and result is stored in object d3. The code for
the program is given below, just understand and run it. If you feel any
difficulty in understanding the program then you can ask your problem by placing
your comment in the comment box given at the end of the program.
contains two float data members a and b. Values of objects d1 and d2 are
entered by user and then arithmetic operations are performed on them by
overloading binary operators and result is stored in object d3. The code for
the program is given below, just understand and run it. If you feel any
difficulty in understanding the program then you can ask your problem by placing
your comment in the comment box given at the end of the program.
Also Read: C++ program to swap two numbers using class
#include<iostream.h>
#include<conio.h>
#include<process.h>
class demo
{
float
a,b;
a,b;
public:
void
getdata();
getdata();
void
display();
display();
demo
operator +(demo);
operator +(demo);
demo
operator -(demo);
operator -(demo);
demo
operator *(demo);
operator *(demo);
demo
operator /(demo);
operator /(demo);
int
operator ==(demo);
operator ==(demo);
};
void demo::getdata()
{
cout<<“Enter
values of a and b:”;
values of a and b:”;
cin>>a>>b;
}
void demo::display()
{
cout<<“a=”<<a<<“tb=”<<b;
}
demo demo::operator +(demo d1)
{
demo
d2;
d2;
d2.a=a+d1.a;
d2.b=b+d1.b;
return
d2;
d2;
}
demo demo::operator -(demo d1)
{
demo
d2;
d2;
d2.a=a-d1.a;
d2.b=b-d1.b;
return
d2;
d2;
}
demo demo::operator *(demo d1)
{
demo
d2;
d2;
d2.a=a*d1.a;
d2.b=b*d1.b;
return
d2;
d2;
}
demo demo::operator /(demo d1)
{
demo
d2;
d2;
d2.a=a/d1.a;
d2.b=b/d1.b;
return
d2;
d2;
}
int demo::operator ==(demo d1)
{
if((a==d1.a)&&(b==d1.b))
return
1;
1;
else
return
0;
0;
}
int main()
{
clrscr();
int ch;
demo
d1,d2,d3;
d1,d2,d3;
cout<<“First
Object:n”;
Object:n”;
d1.getdata();
cout<<“nSecond
Object:n”;
Object:n”;
d2.getdata();
cout<<“nnOperator
Overloadig Menu”;
Overloadig Menu”;
cout<<“nn1.Additionn2.Subtractionn3.Multiplicationn4.Divisionn5.Comparisonn6.Exit”;
cout<<“nnEnter
your choice(1-6):”;
your choice(1-6):”;
cin>>ch;
switch(ch)
{
case
1: d3=d1+d2;
1: d3=d1+d2;
cout<<“nThird
Object:n”;
Object:n”;
d3.display();
break;
case
2: d3=d1-d2;
2: d3=d1-d2;
cout<<“nThird
Object:n”;
Object:n”;
d3.display();
break;
case
3: d3=d1*d2;
3: d3=d1*d2;
cout<<“nThird
Object:n”;
Object:n”;
d3.display();
break;
case
4: d3=d1/d2;
4: d3=d1/d2;
cout<<“nThird
Object:n”;
Object:n”;
d3.display();
break;
case
5: if(d1==d2)
5: if(d1==d2)
cout<<“nObjects
are Equal”;
are Equal”;
else
cout<<“nObjects
are Not Equal”;
are Not Equal”;
break;
case
6: exit(0);
6: exit(0);
break;
default:
cout<<“Wrong Choice!!!Press any key to exit”;
cout<<“Wrong Choice!!!Press any key to exit”;
getch();
}
getch();
return
0;
0;
}
Can u explain to me wat does float#datatype store or wat is its purpose
It is used to store decimal values like 2.58, 5.778, etc.
Float is a num or value in floating point its use for store or read num like 1.5,2.50
I want to perform same operations on objects of a class big integer a data type in which we can store int values upto 45 digits how to do so please help if anyone know