c++写一个代码,定义基本数据类型(包括整形,浮点型,字符型,长整型,布尔型,指针,整形数组,浮点型数组,字符型数组)和组合数据类型,以及类的生成,并输出,只需一个代码,不需要分开三个
时间: 2024-10-08 21:05:14 浏览: 36
```cpp
#include <iostream>
using namespace std;
// 基本数据类型
int int_type; // 整形
float float_type; // 浮点型
char char_type; // 字符型
long long long_int; // 长整型
bool bool_type; // 布尔型
// 指针
int* ptr_int;
float* ptr_float;
char* ptr_char;
// 数组
int arr_int[5]; // 整形数组
float arr_float[5]; // 浮点型数组
char arr_char[5]; // 字符型数组
// 组合数据类型:简单类
class ComplexNumber {
private:
double real;
double imag;
public:
ComplexNumber(double r = 0, double i = 0) : real(r), imag(i) {}
void display() { cout << "Complex Number (" << real << ", " << imag << ")" << endl; }
};
int main() {
// 输出基本数据类型
cout << "Int type: " << int_type << endl;
cout << "Float type: " << float_type << endl;
cout << "Char type: " << char_type << endl;
cout << "Long int type: " << long_int << endl;
cout << "Boolean type: " << (bool_type ? "True" : "False") << endl;
// 初始化指针并显示值
*ptr_int = 100;
*ptr_float = 3.14;
*ptr_char = 'A';
cout << "Integer pointer value: " << *ptr_int << endl;
cout << "Float pointer value: " << *ptr_float << endl;
cout << "Character pointer value: " << *ptr_char << endl;
// 数组赋值并输出
for(int i=0; i<5; i++) {
arr_int[i] = i + 1;
arr_float[i] = i / 3.0;
arr_char[i] = static_cast<char>('A' + i);
cout << "Int array[" << i << "]: " << arr_int[i] << endl;
cout << "Float array[" << i << "]: " << arr_float[i] << endl;
cout << "Char array[" << i << "]: " << arr_char[i] << endl;
}
// 创建并输出复杂数
ComplexNumber complex(3.5, 2.7);
complex.display();
return 0;
}
```
阅读全文