定义一个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
c++ operator
答案:2 悬赏:60
解决时间 2021-02-27 13:25
- 提问者网友:傀儡离开
- 2021-02-26 16:20
最佳答案
- 二级知识专家网友:气场征服一切
- 2021-02-26 16:45
给你一段代码,已经测试过的了,给我最佳啊
#include
#include
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)
{
cout <
return output;
}
istream & operator >> (istream &input,my_int &p)
{
cin >>p.n;
return input;
}
int main()
{
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;
system("Pause");
return 0;
}
#include
#include
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)
{
cout <
}
istream & operator >> (istream &input,my_int &p)
{
cin >>p.n;
return input;
}
int main()
{
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;
system("Pause");
return 0;
}
全部回答
- 1楼网友:错过的是遗憾
- 2021-02-26 17:44
operator和运算符号 连起来就看做是函数名
调用的时候 直接写 运算符 就可以了
和一般函数没区别
===============================
可以有 也可以没有返回值
不过有一种特例,
比如 把某一种类型自动转换成另一种类型
struct a
{
int c;
operator int* () //将 a类型转换成int*类型
{
return &c;
}
};
void main()
{
a a;
a.c=5;
int *p;
p=a;//调用了自动转换,一般的写法应该是 p=&(a.c),
cout<<*p<
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯