若有以下程序: #include <iostream> using namespace Std; class sample { int i; publiC: sample(){}

admin2010-03-29  26

问题 若有以下程序:    #include  <iostream>    using  namespace  Std;    class  sample    {       int  i;    publiC:       sample(){}       void  setvalue(int  m)       {           i=m;       }       void fun(int m)       {          i+=m;       }       void disp()       {       cout<<i<<end1;       }    };    int main()    {       sample  *ps;       ps=new sample;       ps->setvalue(20);       ps->fun(5);       ps->disp();       return 0;    }    程序运行后,输出的结果是(    )。

选项 A、15
B、20
C、25
D、30

答案4

解析 本题考核对象指针的应用。程序首先定义了一个类sample,其中包含一个私有成员i和3个公有成员函数。函数setvalue()的功能是给私有成员i赋值,函数fun()的功能是将私有成员i的值增加5,函数disp()的功能是输出变量i的值。在主函数中,先定义了类sample的一个对象指针ps,并申请了一块内存空间。执行语句ps->setvalue(20);后,类中i的值为20,执行语句ps->fun(5);后,类中i的值为 25。所以程序最后输出25。
转载请注明原文地址:https://kaotiyun.com/show/9Wjp777K
0

最新回复(0)