奇怪的输出
描述
程序填空,输出指定结果
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
class A {
// 在此处补充你的代码
};
int main()
{
A a(5),b(6);
cout << (string)a << endl;
cout << b << endl;
return 0;
}
输入
无
输出
按样例输出
输入样例
None
输出样例
A:v=5
6
来源
Guo Wei
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
class A {
// 在此处补充你的代码
private:
int v;
char c[10];
public:
A(int n):v(n){}
friend ostream &operator<<(ostream& o, const A& a) {
o << a.v;
return o;
}
operator string() {
sprintf(c,"%d",this->v);//字符串转换
return string("A:v=") + c;//string类重载,字符串相加
}
};
int main()
{
A a(5),b(6);
cout << (string)a << endl;
cout << b << endl;
return 0;
}