c++ operator

来源:百度知道 编辑:UC知道 时间:2024/06/19 11:05:56
定义一个class > my_int

下面提供MAIN函数参数,哪位高手帮我把CLASS完成?
int main(void){
my_int a, b, c;
my_int plus, minus, product, divide, asso;
cin >> a;
cin >> b;
cin >> c;
plus = a+b;
minus = a-b;
product = a*b;
divide = a/b;
asso = a*b-c;
cout << "a = " << a << "b = " << b <<"c = " << c << endl;
cout << "a + b = " << plus;
cout << "a - b = " << minus;
cout << "a * b = " << product;
cout << "a / b = " << divide;
cout << " a * b - c = " << asso;
return 0;
}

运行后屏幕输出:

3
2
1
a=3
b=2
c=1
a+b=5
a-b=1
a*b=6
a/b=1
a*b-c=5

给你一段代码,已经测试过的了,给我最佳啊

#include <iostream>
#include <fstream>
using namespace std;
class my_int
{
public:
my_int(){};
my_int (double m)
{
n = m;
}
my_int operator + (const my_int& a);
my_int operator - (const my_int& a);
my_int operator * (const my_int& a);
my_int operator / (const my_int& a);
friend ostream & operator << (ostream &,my_int &);
friend istream & operator >> (istream &,my_int &);

double n;
};

my_int my_int::operator + (const my_int& a)
{
return my_int(n + a.n);
}
my_int my_int::operator - (const my_int& a)
{
return my_int(n - a.n);
}
my_int my_int::operator * (const my_int& a)
{
return my_int(n * a.n);
}
my_int my_int::operator / (const my_int& a)
{
return my_int(n / a.n);
}
ostream & operator << (ostream &output,my_int &p)