- 原题题目
- 代码实现
- 提交结果
原题题目
代码实现
#include#include #include #include using namespace std; class Dog { public: Dog() {} Dog(int weight = 0, int year = 0) : weight_(weight), year_(year) {} int weight() const { return weight_; } int year() const { return year_; } private: int weight_; int year_; }; int main() { Dog dog1(5, 10); ofstream outfile; outfile.open("silyfile.txt"); outfile << dog1.weight() << '_' << dog1.year() << endl; outfile.close(); string content; ifstream infile; infile.open("silyfile.txt"); infile >> content; string tmpweight, tmpyear; int index = 0; while (index < static_cast (content.size()) && content[index] != '_') ++index; tmpweight = content.substr(0, index); tmpyear = content.substr(index + 1); cout << "Weight:" << tmpweight << ' ' << "Age:" << tmpyear; infile.close(); return 0; }
提交结果



