c++标准异常使用和<<运算符重载示例
1 #include <iostream> 2 #include <stdexcept> 3 4 using namespace std; 5 6 7 class Data 8 { 9 public: 10 Data(int a,int b):y(a),m(b) 11 { 12 if(b < 1 || b > 12) throw(logic_error("Error:...") ); 13 } 14 friend ostream & operator<<(ostream & os,Data x) 15 { 16 cout << x.y << x.m << endl; 17 return os; 18 } 19 private: 20 int y{1970}; 21 int m{1}; 22 }; 23 24 int main() 25 { 26 Data d(2015,19); 27 cout << d; 28 return 0; 29 }