C++试卷答案

一、选择题(20 题)

  1. Bchar 通常占 1 字节)

  2. Aconst int *p; 表示 p 指向的整数是常量,但 p 本身可以改变指向)

  3. Cint *arr = new int[10]; 是正确的动态数组分配方式)

  4. Bfinal 关键字可防止继承类重写函数)

  5. Bfriend 关键字允许外部函数访问类的私有成员)

  6. Cprivate 继承会使基类的 publicprotected 成员变为 private,派生类无法访问基类的 private 成员)

  7. B(运算符重载必须是类的成员函数或友元函数)

  8. D(虚函数允许基类的指针或引用调用派生类的方法)

  9. Bcoutiostream 提供的对象)

  10. Bcincout 用于标准输入和标准输出)

  11. A(动态分配的内存未释放会导致内存泄漏)

  12. Atemplate 允许创建泛型函数和类)

  13. Dthrow 可抛出任意类型异常,try 必须和 catch 一起使用,catch(...) 可捕获所有异常)

  14. Cprotected 继承会将基类的 public 成员变成 protected

  15. Avector 是动态数组,可自动增长和缩小)

  16. Adynamic_cast 用于在继承关系中将基类指针转换为派生类指针)

  17. Cstd::map 的底层数据结构通常是平衡二叉搜索树,如红黑树)

  18. C(释放 new 分配的内存后再次访问会导致未定义行为,但不会抛出 std::bad_alloc

  19. Dtry 代码块内可以包含多个 throw 语句)

  20. Dunique_ptr 不能复制,但可以移动,且只能用于 new 分配的对象)


  1. 二、填空题答案

    1. delete

    2. 类名::静态变量名

    3. 空格或换行

    4. 虚拟

    5. 0

    6. 友元函数

    7. throw

    8. <memory>

    9. 动态数组

    10. 参数列表


    三、判断题答案

    1. (√) C++ 支持函数重载和运算符重载。

    2. (√) 在 C++ 中,cin 和 cout 属于标准库 <iostream>。

    3. (√) 继承可以改变基类成员的访问权限。

    4. (×) 纯虚函数必须在派生类中实现,除非派生类也是抽象类。

    5. (×) C++ 支持多继承。

    6. (√) C++ 允许对 new 运算符进行重载。

    7. (×) std::vector<int> v; 默认情况下 v.size() 立即返回 0。

    8. (×) friend 关键字不仅能用于类的成员函数,还可以用于外部函数。

    9. (√) C++ 允许类模板定义静态成员变量。

    10. (√) 处理异常时,如果 catch(...) 捕获了异常,仍然可以使用 throw; 重新抛出异常。


四、问答题答案(10 题)

1. 解释 C++ 中的“深拷贝”和“浅拷贝”的区别,并举例说明。

  • 浅拷贝 仅复制对象的指针,而不复制指针所指向的数据,可能导致多个对象共享同一块内存,进而引发内存泄漏或野指针问题。

  • 深拷贝 复制对象的指针和指针所指向的数据,确保每个对象都有独立的数据。

示例代码:

#include <iostream>
#include <cstring>
using namespace std;
​
class String {
private:
    char *data;
public:
    String(const char *str = "") {
        data = new char[strlen(str) + 1];
        strcpy(data, str);
    }
    String(const String &other) { // 深拷贝
        data = new char[strlen(other.data) + 1];
        strcpy(data, other.data);
    }
    ~String() { delete[] data; }
    void show() { cout << data << endl; }
};
​
int main() {
    String s1("Hello");
    String s2 = s1; // 触发深拷贝
    s2.show();
    return 0;
}

2. 说明 C++ 中 virtual 关键字的作用,并给出示例代码。

  • virtual 关键字用于声明虚函数,实现多态,使得派生类的函数能够覆盖基类的函数。

示例代码:

#include <iostream>
using namespace std;
​
class Animal {
public:
    virtual void makeSound() { cout << "Animal makes sound" << endl; }
};
​
class Dog : public Animal {
public:
    void makeSound() override { cout << "Woof!" << endl; }
};
​
int main() {
    Animal *a = new Dog();
    a->makeSound(); // 调用 Dog::makeSound(),输出 "Woof!"
    delete a;
    return 0;
}

3. 解释 C++ 中 static 关键字的不同用法,并举例说明。

  • 静态变量:在函数或类中声明 static 变量,它在整个程序运行期间存在且共享。

  • 静态成员:属于整个类,而不是类的实例。

示例代码:

cpp复制编辑#include <iostream>
using namespace std;
​
class Test {
public:
    static int count;
    Test() { count++; }
};
​
int Test::count = 0;
​
int main() {
    Test t1, t2;
    cout << "Object count: " << Test::count << endl; // 输出 2
    return 0;
}

4. 说明 C++ 中的 friend 关键字如何使用,并举例说明。 friend 关键字允许一个函数访问类的私有成员。

示例代码:

#include <iostream>
using namespace std;
​
class Test {
private:
    int data;
public:
    Test(int d) : data(d) {}
    friend void show(const Test &t);
};
​
void show(const Test &t) {
    cout << "Data: " << t.data << endl;
}
​
int main() {
    Test t(10);
    show(t);
    return 0;
}

5. 解释 try-catch 语句的作用,并给出示例代码。 try-catch 语句用于异常处理,防止程序因错误崩溃。

示例代码:

#include <iostream>
using namespace std;
​
int main() {
    try {
        throw "An error occurred!";
    } catch (const char* msg) {
        cout << "Caught exception: " << msg << endl;
    }
    return 0;
}

6. 说明 C++ 中 new 和 delete 的作用,并解释它们的使用注意事项。

  • new 用于动态分配内存,delete 用于释放内存,避免内存泄漏

示例代码:

int *p = new int(5);
delete p;  // 释放内存

7. 解释运算符重载的基本规则,并给出 + 运算符重载的代码示例。

#include <iostream>
using namespace std;
​
class Complex {
public:
    int real, imag;
    Complex(int r, int i) : real(r), imag(i) {}
​
    Complex operator+(const Complex &c) {
        return Complex(real + c.real, imag + c.imag);
    }
​
    void show() { cout << real << " + " << imag << "i" << endl; }
};
​
int main() {
    Complex c1(2, 3), c2(4, 5);
    Complex c3 = c1 + c2;
    c3.show();
    return 0;
}

8. STL 容器:vector, list, map, set

9. 继承中的“菱形继承”问题可用虚继承解决。

10. 模板机制的优点是代码复用,示例代码:

template <typename T>
T max(T a, T b) { return (a > b) ? a : b; }
  • 五、程序填空题答案(5 题)

  • 1. 填空,使程序能正确输出 "Hello, C++!"

    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello, C++!" << endl;
        return 0;
    }

    填空答案:

    1. cout

    2. endl


    2. 计算数组元素的总和

    #include <iostream>
    using namespace std;
    int main() {
        int arr[] = {1, 2, 3, 4, 5};
        int sum = 0;
        for(int i = 0; i < 5; i++) { // 填空 1
            sum += arr[i];           // 填空 2
        }
        cout << "Sum = " << sum << endl;
        return 0;
    }

    填空答案:

    1. 5

    2. arr[i]


    3. 使用指针访问数组元素

    #include <iostream>
    using namespace std;
    int main() {
        int arr[] = {10, 20, 30, 40, 50};
        int *ptr = arr; // 填空
        for(int i = 0; i < 5; i++) {
            cout << *(ptr + i) << " ";
        }
        return 0;
    }

    填空答案:

    1. arr


    4. 计算矩形面积的类

    #include <iostream>
    using namespace std;
    class Rectangle {
        private:
            int length, width;
        public:
            Rectangle(int l, int w) : length(l), width(w) {} // 填空 1, 2
            int area() {
                return length * width; // 填空 3
            }
    };
    int main() {
        Rectangle rect(10, 5);
        cout << "Area: " << rect.area() << endl; // 填空 4
        return 0;
    }

    填空答案:

    1. length(l)

    2. width(w)

    3. length * width

    4. area()


    5. 进行异常处理

    #include <iostream>
    using namespace std;
    int main() {
        try {
            throw "Error occurred!"; // 填空
        } catch (const char* msg) {
            cout << "Caught exception: " << msg << endl;
        }
        return 0;
    }

    填空答案:

    1. "Error occurred!"


    六、程序设计题答案(5 题)


    1. 设计 Rectangle 类

    #include <iostream>
    using namespace std;
    
    class Rectangle {
    private:
        int length, width;
    public:
        Rectangle(int l, int w) : length(l), width(w) {} // 构造函数
        int area() {
            return length * width;
        }
    };
    
    int main() {
        Rectangle rect(10, 5);
        cout << "Area: " << rect.area() << endl; // 输出 50
    }

    2. 设计模板函数 findMax

    #include <iostream>
    using namespace std;
    
    template <typename T>
    T findMax(T arr[], int size) {
        T maxVal = arr[0];
        for (int i = 1; i < size; i++) {
            if (arr[i] > maxVal) {
                maxVal = arr[i];
            }
        }
        return maxVal;
    }
    
    int main() {
        int intArr[] = {1, 5, 3, 9, 7};
        double doubleArr[] = {2.1, 3.5, 1.2, 5.6};
        
        cout << "Max int: " << findMax(intArr, 5) << endl; // 输出 9
        cout << "Max double: " << findMax(doubleArr, 4) << endl; // 输出 5.6
    }

    3. 设计 Complex 类的运算符重载

    #include <iostream>
    using namespace std;
    
    class Complex {
    public:
        double real, imag;
        Complex(double r, double i) : real(r), imag(i) {}
    
        Complex operator+(const Complex& other) {
            return Complex(real + other.real, imag + other.imag);
        }
    
        void display() {
            cout << real << " + " << imag << "i" << endl;
        }
    };
    
    int main() {
        Complex c1(2.5, 3.2), c2(1.5, 4.8);
        Complex c3 = c1 + c2;
        c3.display(); // 输出 4.0 + 8.0i
    }

    4. 设计多态机制

    #include <iostream>
    using namespace std;
    
    class Animal {
    public:
        virtual void makeSound() { cout << "Some animal sound" << endl; }
    };
    
    class Dog : public Animal {
    public:
        void makeSound() override { cout << "Woof!" << endl; }
    };
    
    class Cat : public Animal {
    public:
        void makeSound() override { cout << "Meow!" << endl; }
    };
    
    int main() {
        Animal* a1 = new Dog();
        Animal* a2 = new Cat();
    
        a1->makeSound(); // 输出 Woof!
        a2->makeSound(); // 输出 Meow!
    
        delete a1;
        delete a2;
    }

    5. 文件操作

    #include <iostream>
    #include <fstream>
    using namespace std;
    ​
    int main() {
        string text;
        
        // 写入文件
        ofstream outFile("output.txt");
        cout << "请输入一段文本: ";
        getline(cin, text);
        outFile << text;
        outFile.close();
        
        // 读取文件
        ifstream inFile("output.txt");
        cout << "文件内容: ";
        while (getline(inFile, text)) {
            cout << text << endl;
        }
        inFile.close();
        
        return 0;
    }
C++课后习题及答案 一、 选择填空 1. 下列各种高级语言中,( )是面向对象的程序设计语言。 A.BASIC; B.PASCAL; C.C++ D.Ada 2. 下列各种高级语言中,( )是最早提出了对象的概念。 A.Algol 60; B.Simula 67; C.Smalltalk; D.C++ 3. 下述面向对象抽象的原理中,( )是不对的。 A. 数据抽象; B. 行为共享; C.进化; D. 兼容; 4. ( )不是面向对象系统所包含的要数。 A. 重载; B. 对象; C. 类; D. 继承; 5. 关于C++与C语言的关系的描述中,( )是错误的。 A. C语言C++的一个子集; B. C语言C++是兼容的; C. C++对C语言进行了一些改进; D. C++和C语言都是面向对象的; 6. 下面关于对象概念的描述中,( )是错误的。 A.对象就是C语言中的结构变量; B.对象代表着正在创建的系统中的一个实体; C. 对象是一个状态和操作(或方法)的封装体; D.对象之间的信息传递是通过消息进行的; 7. 下面关于类概念的描述中,( )是错误的。 A.类是抽象数据类型的实现; B.类是具有共同行为的若干对象的统一描述体; C.类是创建对象的样板; D.类就是C语言中的结构类型; 8. C++对C语言作了很多改进,下列描述中( )使得C语言发生了质变,即从面向过程变成为面向对象。 A.增加了一些新的运算符; B.允许函数重载,并允许设置缺省参数; C.规定函数说明必须用原型; D.引进了类和对象的概念; 9. 按照标识符的要求,( )符号不能组成标识符。 A.连接符; B. 下划线; C.大小写字母; D.数字字符; 10. 下列符号中,( )不可作为分隔符。 A.,; B.:;C.?; D.;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值