• c++第二章测试


     

    第二章 测验(代码自己测试)

    返回

    本次得分为:13.00/13.00, 本次测试的提交时间为:2020-03-26, 如果你认为本次测试成绩不理想,你可以选择再做一次。
    1
    单选(1分)

    有类A如下定义:

    class A {

    int v;

    public:

    A ( int n) { v = n; }

    };

    下面哪条语句是编译不会出错的?

    • A.

      D)  A a1(3);

      1.00/1.00
    • B.

      C)  A * p = new A();

    • C.

      A)  A a = new A();

    • D.

      B)  A a2;

    2
    单选(1分)

    假设 A 是一个类的名字,下面的语句生成了几个类A的对象?

    A * arr[4] = { new A(), NULL,new A() };

    • A.

      B) 2

      1.00/1.00
    • B.

      D) 4

    • C.

      C) 3

    • D.

      A) 1

    3
    单选(1分)

    假设A 是一个类的名字,下面哪段程序不会用到A的复制构造函数?

    • A.

      A) A a1,a2; a1 = a2;

      1.00/1.00
    • B.

      B) void func( A a) { cout << "good" << endl; }

    • C.

      C) A func( ) { A tmp; return tmp; }

    • D.

      D) A a1; A a2(a1);

    4
    单选(1分)

    类A定义如下:

    class A {

    int v;

    public:

    A(int i) { v = i; }

    A() { }

    };

    下面哪段程序不会引发类型转换构造函数被调用?

    • A.

      A) A a1(4)

    • B.

      B) A a2 = 4;    

    • C.

      C) A a3; a3 = 9;

    • D.

      D) A a1,a2; a1 = a2;

      1.00/1.00
    5
    单选(1分)

    假设A是一个类的名字,下面的程序片段会调用类A的调用析构函数几次? 

    int main() {

    A * p = new A[2];

    A * p2 = new A;

    A a;

    delete [] p;

    }

    • A.

      C) 3

      1.00/1.00
    • B.

      D) 4

    • C.

      A) 1

    • D.

      B) 2

    6,编程填空:学生信息处理程序
    实现一个学生信息处理程序,计算一个学生的四年平均成绩。

    要求实现一个代表学生的类,并且类中所有成员变量都是【私有的】。

    补充下列程序中的 Student 类以实现上述功能。
    输入:
    输入数据为一行,包括:
    姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。
    其中姓名为由字母和空格组成的字符串(输入保证姓名不超过20个字符,并且空格不会出现在字符串两端),年龄、学号和学年平均成绩均为非负整数。信息之间用逗号隔开。
    输出:
    输出一行数据,包括:
    姓名,年龄,学号,四年平均成绩。
    信息之间用逗号隔开。
    样例输入
    Tom Hanks,18,7817,80,80,90,70
    样例输出
    Tom Hanks,18,7817,80
    提示
    必须用类实现,其中所有成员变量都是私有的。
    输出结果中,四年平均成绩不一定为整数。

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <sstream>
    #include <cstdlib>
    using namespace std;

    class Student {
    private:
    char name[23];
    int age;
    int xuehao;
    int score1,score2,score3,score4;
    double ave;
    public:
    void input(){
    scanf("%[^,],%d,%d,%d,%d,%d,%d",name,&age,&xuehao,&score1,&score2,&score3,&score4);
    }
    void calculate(){
    ave=(score1+score2+score3+score4)/4.0;
    }
    void output(){
    printf("%s,%d,%d,%g",name,age,xuehao,ave);
    }
    };

    int main() {
    Student student; // 定义类的对象
    student.input(); // 输入数据
    student.calculate(); // 计算平均成绩
    student.output(); // 输出数据
    }

    7,奇怪的类复制

    程序填空,使其输出9 22 5

    #include <iostream>
    using namespace std;
    class Sample {
    public:
    int v;
    Sample(int n=0)
    {
    v=n;
    }
    Sample(const Sample &x)
    {
    v=x.v+2;
    }
    };
    void PrintAndDouble(Sample o)
    {
    cout << o.v;
    cout << endl;
    }
    int main()
    {
    Sample a(5);
    Sample b = a;
    PrintAndDouble(b);
    Sample c = 20;
    PrintAndDouble(c);
    Sample d;
    d = a;
    cout << d.v;
    return 0;
    }

    8,超简单的复数类

    下面程序的输出是:

    3+4i
    5+6i

    请补足Complex类的成员函数。不能加成员变量。

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    class Complex {
    private:
    double r,i;
    public:
    void Print() {
    cout << r << "+" << i << "i" << endl;
    }
    Complex& operator = (const char* s)
    {
    string str = s;
    int pos = str.find("+", 0);
    string strReal = str.substr(0, pos);
    r = atof(strReal.c_str());
    string strImaginary = str.substr(pos + 1, str.length() - pos - 2);
    i = atof(strImaginary.c_str());
    return *this;
    }
    };
    int main() {
    Complex a;
    a = "3+4i"; a.Print();
    a = "5+6i"; a.Print();
    return 0;
    }
    9.哪来的输出
    程序填空,输出指定结果 2 1

    #include <iostream>
    using namespace std;
    class A {
    public:
    int i;
    A(int x) { i = x; }
    ~A()
    {
    cout<<i<<endl;
    }
    };
    int main()
    {
    A a(1);
    A * pa = new A(2);
    delete pa;
    return 0;
    }

     
  • 相关阅读:
    如何使员工能力和收入相匹配?
    微软Windows Phone 7新特性详解
    微软MSDN中文网络广播(Webcast)——Visual Studio 2010 & ALM应用实践系列课程预告(2011)
    博客园开发征途新书《我也能做CTO之.程序员职业规划》出版
    架构抉择:享用微软SQL云平台就像吃烤鸭
    微软北京.NET俱乐部免费活动(2010年7月18日)–Visual Studio 2010 敏捷开发与云计算Azure
    WCF与Hprose在微软云计算平台Azure上的对决
    在Visual Studio 2010中实现数据驱动Coded UI Tests
    基于微软Dryad分布式并行计算平台云技术的研究
    微软Visual Studio 2010架构设计功能应用
  • 原文地址:https://www.cnblogs.com/gongsuiqing/p/12572648.html
Copyright © 2020-2023  润新知