注意:你只需要考虑非负情况!在测试时也将保证不会出现负数。在减法中结果也不会为负数。
目前已有的整数类仍有一些缺陷,例如int的存储范围为 [-2^31, 2^31 - 1]。因此mfc希望实现MyInteger类,并可以高效地实现部分操作。你需要实现的操作有:
构造函数:
MyInteger() //默认构造很熟构造函数, 默认值为0
MyInteger(string& num) // 构造函数,从字符符串构造为大整数。
MyInteger(int num) // 构造函数,从整型构造为大整数。
~MyInteger() // 析构函数
成员函数:
string getnum() // 返回表示大整数的字符字符串,注意去除前导零。
例:
MyInteger bint(123);
cout << bint.getnum() << endl;
// will output "123"
MyInteger bint("01895");
cout << bint.getnum() << endl;
// will output "1895"
MyInteger operator+(const MyInteger& bint2) //大整数加法
MyInteger operator-(const MyInteger& bint2) //大整数减法
MyInteger operator-(const MyInteger& bint2) //大整数乘法
按照顺序测试以下函数:
下面是写好的私有成员变量
class MyInteger{
private:
string num;
public:
//balabala
};
如有帮助,望采纳
#include <iostream>
using namespace std;
class Complex {
public:
Complex(){real = 0;imag = 0;} //构造函数
Complex(double r,double i){real = r;imag = i;} //构造函数重载
Complex operator+(Complex &c2); //加法运算符重载
Complex operator-(Complex &c2); //减法运算符重载
Complex operator*(Complex &c2); //乘法运算符重载
Complex operator/(Complex &c2); //除法运算符重载
void display(); // 打印函数
private:
double real; //实部
double imag; //虚部
};
//加号重载函数
Complex Complex::operator+(Complex &c2)
{
//运算符重载作为类成员函数,传进去的参数只需要一个
//real这里是省略了this指针
//返回一个Complex类型的数据
return Complex(real+c2.real,imag+c2.imag);
}
//减号重载函数
Complex Complex::operator -(Complex &c2)
{
return Complex(this->real-c2.real,this->imag-c2.imag);
}
//乘号重载函数
Complex Complex::operator *(Complex &c2)
{
return Complex(real*c2.real,imag*c2.imag);
}
//除号重载函数
Complex Complex::operator /(Complex &c2)
{
return Complex(real/c2.real,imag/c2.imag);
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complex c1(3,4),c2(5,-10) ,c3;
c3 = c1+c2; //加号重载
cout<<"c3 = ";
c3.display();
c3 = c1-c2; //减号重载
cout<<"c3 = ";
c3.display();
c3 = c1*c2; //乘号重载
cout<<"c3 = ";
c3.display();
c3 = c1/c2; //除号重载
cout<<"c3 = ";
c3.display();
}
已经写出来了,谢谢各位
参考一下
https://blog.csdn.net/xiangguang_fight/article/details/122389719