一、选择题(20 题)
-
B(
char
通常占 1 字节) -
A(
const int *p;
表示p
指向的整数是常量,但p
本身可以改变指向) -
C(
int *arr = new int[10];
是正确的动态数组分配方式) -
B(
final
关键字可防止继承类重写函数) -
B(
friend
关键字允许外部函数访问类的私有成员) -
C(
private
继承会使基类的public
和protected
成员变为private
,派生类无法访问基类的private
成员) -
B(运算符重载必须是类的成员函数或友元函数)
-
D(虚函数允许基类的指针或引用调用派生类的方法)
-
B(
cout
是iostream
提供的对象) -
B(
cin
和cout
用于标准输入和标准输出) -
A(动态分配的内存未释放会导致内存泄漏)
-
A(
template
允许创建泛型函数和类) -
D(
throw
可抛出任意类型异常,try
必须和catch
一起使用,catch(...)
可捕获所有异常) -
C(
protected
继承会将基类的public
成员变成protected
) -
A(
vector
是动态数组,可自动增长和缩小) -
A(
dynamic_cast
用于在继承关系中将基类指针转换为派生类指针) -
C(
std::map
的底层数据结构通常是平衡二叉搜索树,如红黑树) -
C(释放
new
分配的内存后再次访问会导致未定义行为,但不会抛出std::bad_alloc
) -
D(
try
代码块内可以包含多个throw
语句) -
D(
unique_ptr
不能复制,但可以移动,且只能用于new
分配的对象)
-
二、填空题答案
-
delete
-
类名::静态变量名
-
空格或换行
-
虚拟
-
0
-
友元函数
-
throw
-
<memory>
-
动态数组
-
参数列表
三、判断题答案
-
(√) C++ 支持函数重载和运算符重载。
-
(√) 在 C++ 中,cin 和 cout 属于标准库 <iostream>。
-
(√) 继承可以改变基类成员的访问权限。
-
(×) 纯虚函数必须在派生类中实现,除非派生类也是抽象类。
-
(×) C++ 支持多继承。
-
(√) C++ 允许对 new 运算符进行重载。
-
(×) std::vector<int> v; 默认情况下 v.size() 立即返回 0。
-
(×) friend 关键字不仅能用于类的成员函数,还可以用于外部函数。
-
(√) C++ 允许类模板定义静态成员变量。
-
(√) 处理异常时,如果 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; }
填空答案:
-
cout
-
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; }
填空答案:
-
5
-
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; }
填空答案:
-
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; }
填空答案:
-
length(l)
-
width(w)
-
length * width
-
area()
5. 进行异常处理
#include <iostream> using namespace std; int main() { try { throw "Error occurred!"; // 填空 } catch (const char* msg) { cout << "Caught exception: " << msg << endl; } return 0; }
填空答案:
-
"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; }
-