我需要创建一个罗马数字和整数混合计算器。 我有一个类名罗马,我上传运算符,但我有转换和构造函数的问题。我想我需要两个构造函数用于string和int。 到目前为止,我有这个。 int main()必须保持不变。我正在寻求帮助。
class roman
{
string roman_input; //for string input
int num_input; //for integer input
public:
roman()
{
roman_input = "";
num_input = 0;
}
roman(int a, string b)
{
roman_input = b;
num_input = a;
}
roman(string s)
{
num_input = stoi(s);
}
roman(char* a)
{
roman temp = roman(std::string(a));
this->num_input = temp.num_input;
this->roman_input = temp.roman_input;
}
roman operator+ (const roman& c) const //+ operator overload
{
roman result;
result.roman_input = (this->roman_input + c.roman_input);
result.num_input = (this->num_input + c.num_input);
return result;
}
friend ostream& operator<< (ostream& os, const roman& r) // << operator overload
{
os << r.num_input << r.roman_input;
return os;
}
};
int main()
{
roman r1 = 1234;
roman r2 = "DLXVII"; // 567
roman r3 = r1 + r2;
cout << r1 << " + " << r2 << " = " << r3;
cin.get();
}
//Produces the following output :
//MCCXXXIV + DLXVII = MDCCCI