专攻C++真题合集(1)

        C++语言作为一门广泛使用的编程语言,已经成为了许多IT领域从业者的必备技能之一。为了帮助大家更好地掌握C++语言,本文将为大家提供一些专门的C++真题。

1. 指针

题目一:请编写一个函数,函数名为swap,交换两个整数型变量的值。

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

题目二:请编写一个函数,实现对一个整型数组进行冒泡排序。

void bubble_sort(int arr[], int len) {
    for (int i = 0; i < len - 1; i++) {
        for (int j = 0; j < len - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(&arr[j], &arr[j + 1]);
            }
        }
    }
}

题目三:请编写一个函数,实现将一个字符数组中的每个字符倒序输出。

void reverse_string(char *str) {
    int len = strlen(str);
    for (int i = len - 1; i >= 0; i--) {
        cout << str[i];
    }
}

2. 继承与多态

题目一:请编写一个基类Shape和三个派生类Circle、Rectangle和Triangle,实现求各自面积的功能。

class Shape {
public:
    virtual double get_area() {
        return 0;
    }
};

class Circle : public Shape {
private:
    double r;

public:
    Circle(double r) {
        this->r = r;
    }

    double get_area() {
        return 3.14 * r * r;
    }
};

class Rectangle : public Shape {
private:
    double l, w;

public:
    Rectangle(double l, double w) {
        this->l = l;
        this->w = w;
    }

    double get_area() {
        return l * w;
    }
};

class Triangle : public Shape {
private:
    double a, b, c;

public:
    Triangle(double a, double b, double c) {
        this->a = a;
        this->b = b;
        this->c = c;
    }

    double get_area() {
        double p = (a + b + c) / 2;
        return sqrt(p * (p - a) * (p - b) * (p - c));
    }
};

题目二:请编写一个基类Animal和三个派生类Cat、Dog和Bird,实现各自发出声音的功能。

class Animal {
public:
    virtual void make_sound() {
        cout << "Animal makes sound!" << endl;
    }
};

class Cat : public Animal {
public:
    void make_sound() {
        cout << "Meow~" << endl;
    }
};

class Dog : public Animal {
public:
    void make_sound() {
        cout << "Woof!" << endl;
    }
};

class Bird : public Animal {
public:
    void make_sound() {
        cout << "Chirp~" << endl;
    }
};

3. STL库

题目一:请使用STL库中的vector定义一个整型数组,并利用vector进行遍历、排序、查找等操作。

vector<int> vec = {8, 3, 6, 2, 7, 4, 1, 5};

// 遍历
for (auto item : vec) {
    cout << item << " ";
}
cout << endl;

// 排序
sort(vec.begin(), vec.end());
for (auto item : vec) {
    cout << item << " ";
}
cout << endl;

// 查找
auto it = find(vec.begin(), vec.end(), 7);
if (it != vec.end()) {
    cout << "Found: " << *it << endl;
}
else {
    cout << "Not found!" << endl;
}

题目二:请使用STL库中的map定义一个字典,实现添加、查找、删除单词的功能。

map<string, string> dict;

// 添加单词
dict.insert(pair<string, string>("apple", "苹果"));
dict.insert(make_pair("banana", "香蕉"));
dict["cherry"] = "樱桃";

// 查找单词
auto it = dict.find("apple");
if (it != dict.end()) {
    cout << it->second << endl;
}
else {
    cout << "Not found!" << endl;
}

// 删除单词
dict.erase("banana");

4. 文件操作

题目一:请编写一个程序,将一个文本文件中的每一行反转。

ifstream fin("input.txt");
ofstream fout("output.txt");

string line;
while (getline(fin, line)) {
    reverse(line.begin(), line.end());
    fout << line << endl;
}

fin.close();
fout.close();

题目二:请编写一个程序,实现将一个二进制文件中的数据进行加密和解密。

ifstream fin("input.bin", ios::binary);
ofstream fout("output.bin", ios::binary);

char key = 123; // 密钥可以自己定义

char ch;
while (fin.get(ch)) {
    ch ^= key; // 异或运算实现加密和解密
    fout.put(ch);
}

fin.close();
fout.close();
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值