一起答
主观

#include ﹤iostream﹥

#include ﹤strinh﹥

#include ﹤iomanip﹥

using namespace std;

class student

{

char name[ 8];

int deg;

char level[7];

friend class process; //说明友元类

public:

student( char na[ ],

 int d)

{

strcpy(name,na);

deg=a;

}

};

class process

{

public:

void trans( student &s)

{

int i=s. deg/10;

switch(i)

{

case 9:

strcpy(s. level, "优"); break;

case 8:

stecpy(s. level, "良"); break;

case 7:

strcpy(s. level, "中"); break;

case 6:

strcpy(s. level, "及格");

break;default:

strcpy(s. level, "不及格"); break;

}

}

void show( student &s)

{cout ﹤﹤setw(10)﹤﹤s.name﹤﹤setw(4)﹤﹤s. deg﹤﹤ setw(8)﹤﹤s.level﹤﹤ endl;}

};

void main( )

{student st[ ]={ student("Jack", 78), student ("Lucy",92),student("Lily", 62), student("Tom", 99 )};

process p;

cout﹤﹤"结果:"﹤﹤"姓名"﹤﹤setw(6)﹤﹤"成绩"﹤﹤sew(8)﹤﹤"等级"﹤﹤endl;

for(int i=0; i ﹤4;i++)

{

p. trans(st[i]);

p. show(st[i]);

}

}

试题出自试卷《自考C++程序设计2016年4月试题及答案解析》
参考答案
查看试卷详情
相关试题
  1. 定义一个图形类(figure),其中有保护类型的成员数据:高度(height)和宽度(width),一个公有的构造函数。由该图形类建立两个派生类:矩形类和等腰三角形类。在每个派生类中都包含一个函数area( ),分别用来计算矩形和等腰三角形的面积。其中,主函数已给出,请完成其他部分的代码。

    int main( )

    {

    triangle tri(2, 3);

    rectangle rec(2, 3);

    cout ﹤﹤"The area of triangle is: "﹤﹤ tri. area( )﹤﹤endl;cout ﹤﹤"The area of rectangle is: "﹤﹤rec. area( )﹤﹤ endl;return 0;

    }

  2. #include ﹤iostream﹥

    using namespace std;

    class CBase

    {

    public:

    void fun( );

    };

    void CBase:: fun( )

    {cout﹤﹤"调用基类类的函数fun( )\n";}

    class CDerived:

    public CBase

    {public:void fun( );

    };

    void CDerived:: fun( )

    { cout﹤﹤"调用派生类的函数fun( )\n”;

    }

    void main( )

    {

    CDerived d1;

    CBase *pd=&d1;

    CBase &pd=d1;

    d1. fun( );

    pb-﹥fun( );

    pd.fun( );

    }

  3. #include ﹤iostream﹥

    using namespace std;

    int a[ ]={2,4,6,7,10};

    int &index( int i)

    { retum a[i]; }

    void main( ) {

    int i;

    _________

     //调用 index函数,将数组a中的元素7替换为8

    for( _________ ) //输出数组a

    cout﹤﹤a[i]﹤﹤" ";

    }

  4. #include ﹤iostream﹥

    #include ﹤strinh﹥

    #include ﹤iomanip﹥

    using namespace std;

    class student

    {

    char name[ 8];

    int deg;

    char level[7];

    friend class process; //说明友元类

    public:

    student( char na[ ],

     int d)

    {

    strcpy(name,na);

    deg=a;

    }

    };

    class process

    {

    public:

    void trans( student &s)

    {

    int i=s. deg/10;

    switch(i)

    {

    case 9:

    strcpy(s. level, "优"); break;

    case 8:

    stecpy(s. level, "良"); break;

    case 7:

    strcpy(s. level, "中"); break;

    case 6:

    strcpy(s. level, "及格");

    break;default:

    strcpy(s. level, "不及格"); break;

    }

    }

    void show( student &s)

    {cout ﹤﹤setw(10)﹤﹤s.name﹤﹤setw(4)﹤﹤s. deg﹤﹤ setw(8)﹤﹤s.level﹤﹤ endl;}

    };

    void main( )

    {student st[ ]={ student("Jack", 78), student ("Lucy",92),student("Lily", 62), student("Tom", 99 )};

    process p;

    cout﹤﹤"结果:"﹤﹤"姓名"﹤﹤setw(6)﹤﹤"成绩"﹤﹤sew(8)﹤﹤"等级"﹤﹤endl;

    for(int i=0; i ﹤4;i++)

    {

    p. trans(st[i]);

    p. show(st[i]);

    }

    }

  5. 任意输入10个同学的成绩,计算其平均成绩。要求用函数average( )计算平均成绩。主函数输入数据并输出结果。

    #include "stdafx.h"

    #include ﹤iostream﹥

    using namespace std;

    void main(void)

    {

    float average(float a[ ]);

    float score[10];

    for( _________ )

    {

    cin﹥﹥score[i];

    }

    cout ﹤﹤"average: "﹤﹤ average( score)﹤﹤endl;

    }

    float average( float a[ ])

    {

    float sum =0;

    for( int i=0; i﹤10; i ++)

    {

    _________

    }

    return (sum/10);

    }

  6. 下面程序中Base是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:Der1 called!

    Der2 called!

    #include ﹤ iostream. h﹥

    class Base

    { public:

    _________;

    };

    class Der1: public Base

    { public:

    void display( ) { cout ﹤﹤"Der1 called! "﹤﹤endl; }

    };

    class Der2: public Base

    { public:

    void display( ) { cout ﹤﹤"Der2 called! "﹤﹤endl; }

    };

    void fun( _________ )

    { p-﹥display( ); }

    void main( )

    { Der1 b1;

    Der2 b2;

    Base * p=&b1;

    fun(p);

    p=&b2;

    fun(p);

    }

  7. 完成下面类中成员函数的定义。

    #include ﹤iostream﹥

    #include ﹤string﹥

    using namespace std;

    class str

    { private:

    char * st;

    public:

    str(char *a)

    {

    set (a);

    }_________ //运算符“=”重载函数

    {

    delete st;set(a.st);

    return * this;

    }

    void show( )

    {

    cout ﹤﹤ st ﹤﹤ endl;

     }

    ~str( )

     { delete st; }

    void set(char *s);

    {

    _________strcpy( st, s);

    }

    };

    void main( );

    {

    str s1("hello"),

    s2("world");

    s1. show( ),

    s2.show( );

    s2=s1;

    s1. show( ),

    s2.show( );

    }

  8. Test_Static类内有一静态成员变量num,且初始值设为15。

    #include ﹤iostream﹥

    using namespace std;

    class Test_Static

    {

    private:_________

    public:

    Test_Static(int);void print( );

    };_________

    Test_Static:: Test_Static(int n)

    {

    num=n;

    }

    void Test_Static:: print( )

    {

    cout﹤﹤num﹤﹤endl;

    }

    void main( )

    {

    Test_Static ts(10);ts.print( );

    }

  9. #include ﹤iostream﹥

    using namespace std;

    int main( )

     {

    int a= 10;

    int *p1=NULL;

    int *p2=a;

    cout ﹤﹤p1﹤﹤*p2﹤﹤endl;

    return 0;

    }

  10. #include ﹤iostream﹥

    using namespace std;

    void main( )

    {

    int x1(3), x2(8);

    int * const p =&x1;

    cout ﹤﹤*p﹤﹤ endl;

    p=&x2;

    cout ﹤﹤*p﹤﹤ endl;

    }