21xrx.com
2025-06-24 10:17:53 Tuesday
登录
文章检索 我的文章 写文章
练习c++类与对象:题目及答案解析
2023-07-07 13:31:05 深夜i     14     0
C++ 对象 练习 答案解析

C++是一种面向对象的编程语言,类和对象是其中非常重要的部分。在编写C++代码时,我们经常需要定义类和创建对象来完成具体的任务。但是,对于初学者而言,练习C++类和对象可能会感到困难。因此,本文将为您提供一些练习题目及答案解析,帮助您更好地理解C++类与对象。

1. 编写一个类,实现计算器的加、减、乘、除四个基本运算,并且能够输出结果。

答案解析:以下是具体的实现代码。

#include <iostream>
using namespace std;
class Calculator {
public:
  double x, y; // 定义两个成员变量,用于存储计算器的两个数字
  Calculator(double a, double b)
    x = a;
    y = b;
  
  double add() { // 加法操作
    return x + y;
  }
  double subtract() // 减法操作
    return x - y;
  
  double multiply() { // 乘法操作
    return x * y;
  }
  double divide() // 除法操作
    return x / y;
  
};
int main() {
  double a, b;
  cout << "请输入两个数字:";
  cin >> a >> b;
  Calculator cal(a, b);
  cout << "加法结果为:" << cal.add() << endl;
  cout << "减法结果为:" << cal.subtract() << endl;
  cout << "乘法结果为:" << cal.multiply() << endl;
  cout << "除法结果为:" << cal.divide() << endl;
  return 0;
}

2. 编写一个类,实现字符串的反转操作,并且能够输出结果。

答案解析:以下是具体的实现代码。

#include <iostream>
#include <string>
using namespace std;
class StringReverse {
public:
  string s; // 定义一个成员变量,用于存储字符串
  StringReverse(string str)
    s = str;
  
  string reverse() { // 反转字符串操作
    string res = "";
    for (int i = s.length() - 1; i >= 0; i--) {
      res += s[i];
    }
    return res;
  }
};
int main() {
  string str;
  cout << "请输入一个字符串:";
  cin >> str;
  StringReverse sr(str);
  cout << "反转后的字符串为:" << sr.reverse() << endl;
  return 0;
}

3. 编写一个类,实现二叉树的基本操作,包括插入节点、删除节点、查找节点、输出节点等,并且能够输出结果。

答案解析:以下是具体的实现代码。

#include <iostream>
using namespace std;
struct TreeNode {
  int val;
  TreeNode* left;
  TreeNode* right;
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class BinaryTree {
public:
  BinaryTree() : root(nullptr) {}
  void insert(int val) { // 插入节点操作
    if (root == nullptr) {
      root = new TreeNode(val);
      return;
    }
    TreeNode* cur = root;
    while (true) {
      if (val < cur->val) {
        if (cur->left == nullptr) {
          cur->left = new TreeNode(val);
          return;
        }
        cur = cur->left;
      }
      else {
        if (cur->right == nullptr) {
          cur->right = new TreeNode(val);
          return;
        }
        cur = cur->right;
      }
    }
  }
  void erase(int val) { // 删除节点操作
    root = erase(root, val);
  }
  TreeNode* erase(TreeNode* root, int val) {
    if (root == nullptr)
      return nullptr;
    
    if (val < root->val) {
      root->left = erase(root->left, val);
    }
    else if (val > root->val) {
      root->right = erase(root->right, val);
    }
    else {
      if (root->left == nullptr) {
        TreeNode* node = root->right;
        delete root;
        return node;
      }
      else if (root->right == nullptr) {
        TreeNode* node = root->left;
        delete root;
        return node;
      }
      else {
        TreeNode* node = root->right;
        while (node->left != nullptr)
          node = node->left;
        
        root->val = node->val;
        root->right = erase(root->right, node->val);
      }
    }
    return root;
  }
  bool find(int val) { // 查找节点操作
    TreeNode* cur = root;
    while (cur != nullptr) {
      if (cur->val == val)
        return true;
      
      else if (val < cur->val)
        cur = cur->left;
      
      else
        cur = cur->right;
      
    }
    return false;
  }
  void print() { // 输出节点操作
    print(root);
  }
  void print(TreeNode* root) {
    if (root == nullptr)
      return;
    
    print(root->left);
    cout << root->val << " ";
    print(root->right);
  }
private:
  TreeNode* root;
};
int main() {
  BinaryTree bt;
  bt.insert(5);
  bt.insert(3);
  bt.insert(7);
  bt.insert(2);
  bt.insert(4);
  bt.insert(6);
  bt.insert(8);
  cout << "原始的树为:";
  bt.print();
  cout << endl;
  bt.erase(5);
  cout << "删除5后的树为:";
  bt.print();
  cout << endl;
  cout << "查找2是否存在:";
  if (bt.find(2))
    cout << "存在" << endl;
  
  else
    cout << "不存在" << endl;
  
  return 0;
}

练习C++类与对象不仅能够帮助我们更好地理解C++的对象面向特性,还能够提高我们的编程能力和实际应用水平。希望以上的题目及答案解析能够对大家有所帮助。

  
  

评论区