niusouti.com

( 35 )有如下程序::#includeusing namespace std:class Sample{friend long fun ( Sample s ) ;public:Sample ( long a ) {x=a;}Private:long x;};long fun ( Sample s ){if ( s.x<2 ) retum 1;retum s.x*fun ( Sample ( s.x-a ))}int main (){int sum=0for ( int i =0; i <6; i +

题目

( 35 )有如下程序::

#include

using namespace std:

class Sample

{

friend long fun ( Sample s ) ;

public:

Sample ( long a ) {x=a;}

Private:

long x;

};

long fun ( Sample s )

{

if ( s.x<2 ) retum 1;

retum s.x*fun ( Sample ( s.x-a ))

}

int main ()

{

int sum=0

for ( int i =0; i <6; i ++ ) { sum+=fun ( Sample ( i ) ; ) }

cout<<SUM;

retum 0;

}

运行时输出的结果是

A ) 120

B ) 16

C ) 154

D ) 34


相似考题
更多“( 35 )有如下程序::#includeusing namespace std:class Sample{friend long fun ( Sample s ) ;p ”相关问题
  • 第1题:

    使用VC6打开考生文件夹下的工程test12_1,此工程包含一个源程序文件test_12.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下:

    fun (Sample &p) 1 2

    fun (Sample *p) 3 4

    20 10

    源程序文件test12_1清单如下:

    include<iostream .h>

    class Sample

    {

    private:

    int x,y;

    static int z;

    public:

    Sample(int a,int b){ x=a;y=b; }

    void fun(Sample &p);

    void fun(Sample *p);

    static void print(Sample s);

    };

    /*************** found ***************/

    int z=10;

    void Sample::fun(Sample &p)

    {

    x=p.K;y=p.y;

    cout<<"fun(Sample &p)"<<" "<<x<<" "<<y<<endl;

    }

    void Sample::fun(Sample *p)

    {

    /************** found **************/

    x=p.x; y=p.y;

    cout<<"fun(Sample *p) "<<" '<<x<<" "<<y<<endl;

    }

    void Sample::print (Sample s)

    {

    /*************** found *****************/

    x=20;

    cout<<s. x<<" "<<z<<endl;

    }

    void main()

    {

    Sample p(1,2),q(3,4);

    p. fun(p);

    p. fun(&q);

    p. print(p);

    }


    正确答案:(1)错误:int z=10; 正确:int Sample::z=10; (2)错误:x=p.x;y=p.y; 正确;x=p->x;y=p->y; (3)错误:x=20; 正确;s.x=20;
    (1)错误:int z=10; 正确:int Sample::z=10; (2)错误:x=p.x;y=p.y; 正确;x=p->x;y=p->y; (3)错误:x=20; 正确;s.x=20; 解析:(1)主要考查考生对于静态成员初始化定义的理解,静态成员使用关键字static修饰,应对其进行类体外初始化,格式为数据类型“类名::静态变量名:初始值”;
    (2)主要考查考生对于指针与引用区别的掌握,x和y都是指针类型的变量,应使用“->”调用类的成员;
    (3)主要考查考生对于静态成员函数的掌握,在静态成员函数中使用非静态成员,需要用对象来引用。

  • 第2题:

    完成下面的程序,使显示结果如下图所示。

    Private Sub Form_Click()

    FontSize=18

    Sample$s="【 】"

    x=(ScaleWidth-TextWidth(Sample$))/2

    y=(ScaleHeight-TextHeight(Sample$))/2

    CurrentX=x

    CurrentY=y

    【 】Sample$

    End Sub


    正确答案:Welcome to Beijing! Print
    Welcome to Beijing! ,Print 解析:该题中主要是考查了赋值语句与显示语句。赋值语句是根据图示给出的信息,填写上字符串,然后,用Print命令,使字符串显示在窗体上。赋值语句为Sample$= "Welcome to Beijing!";输出语句为:Print Sample$。

  • 第3题:

    有如下类定义,请将Sample类的拷贝构造函数补充完整。 class Sample { public: Sample(){} ~Sample(){if(p)delete p;} Sample(const Sample&s){ p=new int; *p=___________; } void SetData(int data) {p=new int(data);} void print(){cout<<*p<<endl;} private: int*p; };


    p=new int;p=s.p; p=new int;p=s.p; 解析:此题考查的是复制构造函数。复制构造函数定义的一般格式如下:类名::类名(const类名&引用对象名){复制构造函数体)。而在类中的声明部分可省去“类名::”。

  • 第4题:

    有如下程序: #inClude<iostream> using namespaCe std; Class Sample{ friend long fun(Sample S); publiC: Sample(10ng A.{x=a;} private: long X; }; long fun(Sample S){ if(S.x<2)return l; return S.X*fun(Sample(s.x-1)); } int main( ) { int sum=0; for(int i=0;i<6;i++) {sum+=fun(Sample(i));} Cout<<sum: return 0; } 执行这个程序的输出结果是( )。

    A.120

    B.16

    C.154

    D.34


    正确答案:C
    本题考查默认构造函数,当i=0、1时,fun(Sample(i))为1;当i=2时,fun(Sample(i)1为2;当i=3时,fun(Sample(i))为6;当i=4时,fun(Sample(i))为24;当i=5时,fun(Sample(i))为120。所以总和为l54

  • 第5题:

    若有如下程序: include using namespaces std;int s=O;class sample {static int n;p

    若有如下程序: #include <iostream> using namespaces std; int s=O; class sample { static int n; public: sample(int i) { n=i; } static void add() { s+=n; } }; int sample::s=O; int main() { sample a(2),b(5); sample: :add(); cout<<s<<end1; return 0; } 程序运行后的输出结果是

    A.2

    B.5

    C.7

    D.3


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。程序中定义一个类sample,它包括一个静态数据成员n和一个静态成员函数add,并在类的构造函数中给类私有静态数据成员n赋值。在主函数main中,定义对象a(2)时,通过构造函数使静态数据成员n的值变为2,在定义对象b(5)时,通过构造函数使静态数据成员n=5(覆盖了前面的n=2),再执行sample::add()使全局变量s=5。