MST

星途 面试题库

面试题:C++类型转换与运算符重载的深度应用

有一个 `NumericValue` 类,它可以表示不同类型的数值(如整数、浮点数),内部通过 `union` 存储数据,并带有一个枚举类型标记当前存储的数据类型。请设计并实现类型转换运算符,将 `NumericValue` 对象转换为 `int` 或 `double`,同时考虑可能的精度损失和数据截断问题。另外,重载 `+`、`-`、`*`、`/` 运算符,使其能根据实际存储的数据类型进行正确的运算,并在不同类型运算时进行合理的类型转换。详细说明你的设计思路和实现细节。
11.6万 热度难度
编程语言C++

知识考点

AI 面试

面试题答案

一键面试

设计思路

  1. NumericValue 类设计
    • 使用 union 来存储不同类型的数据,以节省内存空间。
    • 使用枚举类型来标记当前存储的数据类型。
  2. 类型转换运算符
    • 实现 operator int()operator double()。在转换时,根据枚举类型判断当前数据类型,若为浮点数转 int 则考虑精度损失和截断问题;若为 intdouble 则直接转换。
  3. 运算符重载
    • 对于 +-*/ 运算符,首先判断两个操作数的数据类型。
    • 如果类型相同,直接进行相应类型的运算。
    • 如果类型不同,将 int 类型转换为 double 类型后再进行运算,以保证精度。

实现细节

#include <iostream>
#include <stdexcept>

// 定义枚举类型表示数据类型
enum class NumericType {
    Int,
    Double
};

class NumericValue {
private:
    union {
        int intValue;
        double doubleValue;
    };
    NumericType type;

public:
    // 构造函数,根据传入的值类型初始化
    NumericValue(int value) : intValue(value), type(NumericType::Int) {}
    NumericValue(double value) : doubleValue(value), type(NumericType::Double) {}

    // 类型转换运算符
    operator int() const {
        if (type == NumericType::Int) {
            return intValue;
        } else if (type == NumericType::Double) {
            return static_cast<int>(doubleValue); // 考虑精度损失和截断
        }
        throw std::runtime_error("Invalid type conversion");
    }

    operator double() const {
        if (type == NumericType::Int) {
            return static_cast<double>(intValue);
        } else if (type == NumericType::Double) {
            return doubleValue;
        }
        throw std::runtime_error("Invalid type conversion");
    }

    // 运算符重载
    NumericValue operator+(const NumericValue& other) const {
        if (type == NumericType::Int && other.type == NumericType::Int) {
            return NumericValue(intValue + other.intValue);
        } else if (type == NumericType::Double && other.type == NumericType::Double) {
            return NumericValue(doubleValue + other.doubleValue);
        } else {
            double left = static_cast<double>(*this);
            double right = static_cast<double>(other);
            return NumericValue(left + right);
        }
    }

    NumericValue operator-(const NumericValue& other) const {
        if (type == NumericType::Int && other.type == NumericType::Int) {
            return NumericValue(intValue - other.intValue);
        } else if (type == NumericType::Double && other.type == NumericType::Double) {
            return NumericValue(doubleValue - other.doubleValue);
        } else {
            double left = static_cast<double>(*this);
            double right = static_cast<double>(other);
            return NumericValue(left - right);
        }
    }

    NumericValue operator*(const NumericValue& other) const {
        if (type == NumericType::Int && other.type == NumericType::Int) {
            return NumericValue(intValue * other.intValue);
        } else if (type == NumericType::Double && other.type == NumericType::Double) {
            return NumericValue(doubleValue * other.doubleValue);
        } else {
            double left = static_cast<double>(*this);
            double right = static_cast<double>(other);
            return NumericValue(left * right);
        }
    }

    NumericValue operator/(const NumericValue& other) const {
        if (type == NumericType::Int && other.type == NumericType::Int) {
            if (other.intValue == 0) {
                throw std::runtime_error("Division by zero");
            }
            return NumericValue(intValue / other.intValue);
        } else if (type == NumericType::Double && other.type == NumericType::Double) {
            if (other.doubleValue == 0) {
                throw std::runtime_error("Division by zero");
            }
            return NumericValue(doubleValue / other.doubleValue);
        } else {
            double left = static_cast<double>(*this);
            double right = static_cast<double>(other);
            if (right == 0) {
                throw std::runtime_error("Division by zero");
            }
            return NumericValue(left / right);
        }
    }
};

你可以通过以下方式测试这些函数:

int main() {
    NumericValue a(5);
    NumericValue b(2.5);

    NumericValue resultAdd = a + b;
    std::cout << "Addition result: " << static_cast<double>(resultAdd) << std::endl;

    NumericValue resultSub = a - b;
    std::cout << "Subtraction result: " << static_cast<double>(resultSub) << std::endl;

    NumericValue resultMul = a * b;
    std::cout << "Multiplication result: " << static_cast<double>(resultMul) << std::endl;

    NumericValue resultDiv = a / b;
    std::cout << "Division result: " << static_cast<double>(resultDiv) << std::endl;

    return 0;
}