问题及代码:
#include <iostream>
using namespace std;
class Test
{
private:
int x;
const int y;
//************* begin *****************
public:
Test(int a, int b);
void printxy() const ; //(1)
} ;
Test::Test(int a, int b):x(a),y(b) {} ; //(2)
void Test::printxy() const //(3)
{
cout<<"x*y="<<x*y<<endl;
}
//************* end *****************
int main()
{
int x1,x2;
cin>>x1>>x2;
const Test t(x1,x2);
t.printxy( );
return 0;
}
运行结果:
本文通过一个C++程序实例介绍了如何定义和使用类中的常量成员函数。该程序创建了一个名为Test的类,其中包含一个常量成员函数printxy()用于输出两个整型变量x和y的乘积。在主函数中,通过输入两个整数并创建Test类的常量对象来演示如何调用此成员函数。
983

被折叠的 条评论
为什么被折叠?



