-4

C++ では、関数の戻り値が void の場合、関数内の引数に加えられた変更は実際の変数に反映されませんが、メンバー関数の場合はそうではなく、
変更が永続的に発生することがわかります。

 #include<iostream>
 using namespace std;

 class Student {
 
 public:
 int age;
 float marks;
 Student()
  {
    cout << "call by default";
  }
 void ageInc()
  {
    age = age + 1;
  }
 };

 int main()
 {
  Student s;

   s.age = 34;
   cout << s.age << endl;

   s.ageInc();
   cout << s.age << endl;
   return 0;
 }
4

2 に答える 2