常对象调用常成员函数--简单

源程序:

#include <iostream>

using namespace std;

class ctest

{

private:

  int x;

public:

  ctest(const int x)

  {

    this->x = x;

  }

  int getx() const   //const必须写在函数的后面

  {

    return x;

  }

};

int main()

{

  const ctest obj(5);  //常对象

  cout << obj.getx() << endl;  //常对象调用常成员函数

  system("pause");

  return 0;

}

运行结果:

posted @ 2019-12-03 14:33  bobo哥  阅读(760)  评论(0编辑  收藏  举报