【软件构造】ADP + OOP——三、数据类型与类型检验

ADP + OOP

三、数据类型与类型检验

阅读资料:

MIT 课程阅读01-静态检查

MIT 课程阅读08-可变性与不变性

1、编程语言中的数据类型

基本数据类型primitive types

  • int(范围 ± 2 31 \pm 2^{31} ±231

  • long (范围 ± 2 63 \pm2^{63} ±263

  • boolean(true/false)

  • double(浮点数)

  • char(单个字符,如‘A’)

对象数据类型object types

  • String 表示一个字符序列
  • BigInteger 表示任意大小的整数

根据java的规定,原始类型是小写的,对象类型以大写开头

基本数据类型 对象数据类型
int, long, byte, short, char, float, doule, boolean Classes, interfaces, arrays, enums, annotations
只有值,没有ID,即与其他值无法区分 既有ID,也有值
immutable 不可变的 有的mutable,有的immutable
在栈stack中分配内存 在堆heap中分配内存
无法实现表达的统一 用泛型统一表达
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,我可以给出以下的示例代码: ```c++ #include <iostream> #include <string> using namespace std; class RomanNumeral { private: int num; string roman; public: RomanNumeral(int n = 0) : num(n) { toRoman(); } RomanNumeral(string r) : roman(r) { toNum(); } void toRoman() { string result = ""; int n = num; while (n > 0) { if (n >= 1000) { result += "M"; n -= 1000; } else if (n >= 900) { result += "CM"; n -= 900; } else if (n >= 500) { result += "D"; n -= 500; } else if (n >= 400) { result += "CD"; n -= 400; } else if (n >= 100) { result += "C"; n -= 100; } else if (n >= 90) { result += "XC"; n -= 90; } else if (n >= 50) { result += "L"; n -= 50; } else if (n >= 40) { result += "XL"; n -= 40; } else if (n >= 10) { result += "X"; n -= 10; } else if (n >= 9) { result += "IX"; n -= 9; } else if (n >= 5) { result += "V"; n -= 5; } else if (n >= 4) { result += "IV"; n -= 4; } else { result += "I"; n--; } } roman = result; } void toNum() { int result = 0; for (int i = 0; i < roman.length(); i++) { if (roman[i] == 'M') { result += 1000; } else if (roman[i] == 'D') { result += 500; } else if (roman[i] == 'C') { if (i < roman.length() - 1 && roman[i + 1] == 'M') { result += 900; i++; } else if (i < roman.length() - 1 && roman[i + 1] == 'D') { result += 400; i++; } else { result += 100; } } else if (roman[i] == 'L') { result += 50; } else if (roman[i] == 'X') { if (i < roman.length() - 1 && roman[i + 1] == 'C') { result += 90; i++; } else if (i < roman.length() - 1 && roman[i + 1] == 'L') { result += 40; i++; } else { result += 10; } } else if (roman[i] == 'V') { result += 5; } else if (roman[i] == 'I') { if (i < roman.length() - 1 && roman[i + 1] == 'X') { result += 9; i++; } else if (i < roman.length() - 1 && roman[i + 1] == 'V') { result += 4; i++; } else { result++; } } } num = result; } RomanNumeral operator+(const RomanNumeral& other) { int result = num + other.num; return RomanNumeral(result); } friend ostream& operator<<(ostream& os, const RomanNumeral& rn) { os << rn.roman; return os; } friend istream& operator>>(istream& is, RomanNumeral& rn) { string input; is >> input; RomanNumeral temp(input); rn.num = temp.num; rn.roman = temp.roman; return is; } operator int() const { return num; } }; int main() { RomanNumeral rn1("VIII"); RomanNumeral rn2("XIV"); RomanNumeral rn3 = rn1 + rn2; cout << rn1 << " + " << rn2 << " = " << rn3 << endl; int n = rn3; cout << "rn3 = " << rn3 << ", n = " << n << endl; RomanNumeral rn4; cout << "Input a Roman Numeral: "; cin >> rn4; cout << "rn4 = " << rn4 << endl; return 0; } ``` 在上述代码中,我们定义了一个 `RomanNumeral` 类,用于表示罗马数字。其中,构造函数可以接受一个整数或者一个字符串作为参数,分别对应数字和罗马数字,然后分别调用 `toRoman` 和 `toNum` 方法将其转换为罗马数字和数字。此外,我们还重载了加法运算符 `+`,使得可以对两个 `RomanNumeral` 对象进行加法操作。为了方便输出,我们还重载了输出运算符 `<<`,并且为了方便输入,我们重载了输入运算符 `>>`。最后,我们还定义了一个类型转换函数,可以将 `RomanNumeral` 对象转换为整数。 在 `main` 函数中,我们演示了如何使用这个类进行加法运算、类型转换和输入输出操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值