源程序:

#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;

}

运行结果:

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
  • 2021-10-08
  • 2021-11-21
  • 2022-12-23
  • 2021-09-15
猜你喜欢
  • 2021-04-24
  • 2022-12-23
  • 2022-02-13
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
相关资源
相似解决方案