重载加法运算符+ c++_C ++程序,用于重载二进制运算符,加法,减法,乘法,除法和比较...

重载加法运算符+ c++

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.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class demo
{
                float
a,b;
                public:
                void
getdata();
                void
display();
                demo
operator +(demo);
                demo
operator -(demo);
                demo
operator *(demo);
                demo
operator /(demo);
                int
operator ==(demo);
};
void demo::getdata()
{
                cout<<“Enter
values of a and b:”;
                cin>>a>>b;
}
void demo::display()
{
                cout<<“a=”<<a<<“tb=”<<b;
}
demo demo::operator +(demo d1)
{
                demo
d2;
                d2.a=a+d1.a;
                d2.b=b+d1.b;
                return
d2;
}
demo demo::operator -(demo d1)
{
                demo
d2;
                d2.a=a-d1.a;
                d2.b=b-d1.b;
                return
d2;
}
demo demo::operator *(demo d1)
{
                demo
d2;
                d2.a=a*d1.a;
                d2.b=b*d1.b;
                return
d2;
}
demo demo::operator /(demo d1)
{
                demo
d2;
                d2.a=a/d1.a;
                d2.b=b/d1.b;
                return
d2;
}
int demo::operator ==(demo d1)
{
                if((a==d1.a)&&(b==d1.b))
                                return
1;
                else
                                return
0;
}
int main()
{
                clrscr();
                int ch;
                demo
d1,d2,d3;
                cout<<“First
Object:n”;
                d1.getdata();
                cout<<“nSecond
Object:n”;
                d2.getdata();
                cout<<“nnOperator
Overloadig Menu”;
                cout<<“nn1.Additionn2.Subtractionn3.Multiplicationn4.Divisionn5.Comparisonn6.Exit”;
                cout<<“nnEnter
your choice(1-6):”;
                cin>>ch;
                switch(ch)
                {
                                case
1:  d3=d1+d2;
                                                cout<<“nThird
Object:n”;
                                                d3.display();
                                                break;
                                case
2:  d3=d1-d2;
                                                cout<<“nThird
Object:n”;
                                                d3.display();
                                                break;
                                case
3: d3=d1*d2;
                                                cout<<“nThird
Object:n”;
                                                d3.display();
                                                break;
                                case
4: d3=d1/d2;
                                                cout<<“nThird
Object:n”;
                                                d3.display();
                                                break;
                                case
5: if(d1==d2)
                                                                cout<<“nObjects
are Equal”;
                                                else
                                                                cout<<“nObjects
are Not Equal”;
                                                break;
                                case
6: exit(0);
                                                break;
                                default:
cout<<“Wrong Choice!!!Press any key to exit”;
                                                 getch();
                }
                getch();
                return
0;
}
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.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class demo
{
                float
a,b;
                public:
                void
getdata();
                void
display();
                demo
operator +(demo);
                demo
operator -(demo);
                demo
operator *(demo);
                demo
operator /(demo);
                int
operator ==(demo);
};
void demo::getdata()
{
                cout<<“Enter
values of a and b:”;
                cin>>a>>b;
}
void demo::display()
{
                cout<<“a=”<<a<<“tb=”<<b;
}
demo demo::operator +(demo d1)
{
                demo
d2;
                d2.a=a+d1.a;
                d2.b=b+d1.b;
                return
d2;
}
demo demo::operator -(demo d1)
{
                demo
d2;
                d2.a=a-d1.a;
                d2.b=b-d1.b;
                return
d2;
}
demo demo::operator *(demo d1)
{
                demo
d2;
                d2.a=a*d1.a;
                d2.b=b*d1.b;
                return
d2;
}
demo demo::operator /(demo d1)
{
                demo
d2;
                d2.a=a/d1.a;
                d2.b=b/d1.b;
                return
d2;
}
int demo::operator ==(demo d1)
{
                if((a==d1.a)&&(b==d1.b))
                                return
1;
                else
                                return
0;
}
int main()
{
                clrscr();
                int ch;
                demo
d1,d2,d3;
                cout<<“First
Object:n”;
                d1.getdata();
                cout<<“nSecond
Object:n”;
                d2.getdata();
                cout<<“nnOperator
Overloadig Menu”;
                cout<<“nn1.Additionn2.Subtractionn3.Multiplicationn4.Divisionn5.Comparisonn6.Exit”;
                cout<<“nnEnter
your choice(1-6):”;
                cin>>ch;
                switch(ch)
                {
                                case
1:  d3=d1+d2;
                                                cout<<“nThird
Object:n”;
                                                d3.display();
                                                break;
                                case
2:  d3=d1-d2;
                                                cout<<“nThird
Object:n”;
                                                d3.display();
                                                break;
                                case
3: d3=d1*d2;
                                                cout<<“nThird
Object:n”;
                                                d3.display();
                                                break;
                                case
4: d3=d1/d2;
                                                cout<<“nThird
Object:n”;
                                                d3.display();
                                                break;
                                case
5: if(d1==d2)
                                                                cout<<“nObjects
are Equal”;
                                                else
                                                                cout<<“nObjects
are Not Equal”;
                                                break;
                                case
6: exit(0);
                                                break;
                                default:
cout<<“Wrong Choice!!!Press any key to exit”;
                                                 getch();
                }
                getch();
                return
0;
}

翻译自: https://www.thecrazyprogrammer.com/2013/09/c-program-for-overloading-binary-operators.html

重载加法运算符+ c++

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值