データ・メンバーを宣言する順序に問題がある
#include <iostream> #include <string> using namespace std; class CEmployee { int salary; int age; string name; public: CEmployee(const string&, int); int get_salary(){return salary;} const string get_name(){return name;} }; CEmployee::CEmployee(const string& name_, int age_) :age(age_), salary(age * 10000), name(name_){} int main() { CEmployee emp("John", 35); cout << emp.get_name() << "の給料は"; cout << emp.get_salary() << "円です\n"; return 0; }
実行結果 Johnの給料は-1351680000円です