C++ (C+;+;查询)全局访问实例化对象
这是一个基本程序,用于获取两个5位数字作为字符串,并使用“+”上的运算符重载对这两个数字进行加法C++ (C+;+;查询)全局访问实例化对象,c++,global,C++,Global,这是一个基本程序,用于获取两个5位数字作为字符串,并使用“+”上的运算符重载对这两个数字进行加法 #include <iostream> #include <limits> #include <cstdlib> #include <cstring> #include <sstream> using namespace std; class IntStr { int InputNum; public: //In
#include <iostream>
#include <limits>
#include <cstdlib>
#include <cstring>
#include <sstream>
using namespace std;
class IntStr
{
int InputNum;
public:
//IntStr();
IntStr::IntStr(int num);
IntStr operator+ (const IntStr &);
//~IntStr();
void Display();
};
IntStr::IntStr(int num)
{
InputNum = num;
}
void IntStr::Display()
{
cout << "Number is (via Display) : " << InputNum <<endl;
}
IntStr IntStr::operator+ (const IntStr & second) {
int add_result = InputNum + second.InputNum;
return IntStr(add_result);
}
int main()
{
string str;
bool option = true;
bool option2 = true;
while (option)
{
cout << "Enter the number : " ;
if (!getline(cin, str))
{
cerr << "Something went seriously wrong...\n";
}
istringstream iss(str);
int i;
iss >> i; // Extract an integer value from the stream that wraps str
if (!iss)
{
// Extraction failed (or a more serious problem like EOF reached)
cerr << "Enter a number dammit!\n";
}
else if (i < 10000 || i > 99999)
{
cerr << "Out of range!\n";
}
else
{
// Process i
//cout << "Stream is: " << iss << endl; //For debugging purposesc only
cout << "Number is : " << i << endl;
option = false;
IntStr obj1 = IntStr(i);
obj1.Display();
}
}//while
while (option2)
{
cout << "Enter the second number : " ;
if (!getline(cin, str))
{
cerr << "Something went seriously wrong...\n";
}
istringstream iss(str);
int i;
iss >> i; // Extract an integer value from the stream that wraps str
if (!iss) //------------------------------------------> (i)
{
// Extraction failed (or a more serious problem like EOF reached)
cerr << "Enter a number dammit!\n";
}
else if (i < 10000 || i > 99999)
{
cerr << "Out of range!\n";
}
else
{
// Process i
//cout << "Stream is: " << iss << endl; //For debugging purposes only
cout << "Number is : " << i << endl;
option2 = false;
IntStr obj2 = IntStr(i);
obj2.Display();
//obj1->Display();
}
}//while
//IntStr Result = obj1 + obj2; // --------------------> (ii)
//Result.Display();
cin.get();
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类IntStr
{
int-InputNum;
公众:
//IntStr();
IntStr::IntStr(intnum);
IntStr运算符+(常量IntStr&);
//~IntStr();
void Display();
};
IntStr::IntStr(intnum)
{
InputNum=num;
}
void IntStr::Display()
{
库特
测试流是否处于错误状态,如果转换失败或您处于流的末尾,就会出现这种情况
obj1定义如下:
else
{
// Process i
//cout << "Stream is: " << iss << endl; //For debugging purposesc only
cout << "Number is : " << i << endl;
option = false;
IntStr obj1 = IntStr(i);
obj1.Display();
}
else
{
//过程一
//库特
调用在布尔上下文中计算流的重载运算符。这将检查流的状态,以查看上一个操作是否失败-如果失败,则不能依赖整数变量i
中的值有效,因为流上的输入不是整数
变量obj1
和obj2
在while循环的作用域中定义-它们在作用域之外不可用。您可以在while循环的作用域之外声明它们,在这种情况下,变量将保留它在while循环中保留的最后一个值
1) 发件人:
布尔运算符!()常数;求值
流对象
如果
设置了错误标志(failbit或badbit)
在流上。否则它将返回
错
发件人:
故障位通常由输入设置
与错误相关时的操作
具有内部逻辑的
操作本身,而badbit是
通常在涉及错误时设置
河流完整性的丧失,
这可能会持续下去,即使
在上执行不同的操作
小溪
2) 这两个对象不在范围内,它们只存在于前面的括号中。我也没有,但在这种情况下,检查文档很容易……理解“失败”部分。但是我们不应该在这些行上检查一些东西:让“abc”表示操作“iss>>I”;现在我们不应该检查(!abc),然后说“cerr”吗
else
{
// Process i
//cout << "Stream is: " << iss << endl; //For debugging purposesc only
cout << "Number is : " << i << endl;
option = false;
IntStr obj1 = IntStr(i);
obj1.Display();
}