C++学习第二课

一、C++数据类型

使用编程语言进行编程时,我们需要用到各种变量来存储各种信息,变量保留的时他所存储的值的内存位置,这意味着,你创建一个变量时,就会在内存中保留一些空间。

在我们的编程中我们一般需要的数据类型有比如字符型、宽字符型、整型、浮点型、双浮点型、布尔型等,操作系统会根据数据类型来分分配内存空间。(巨重要,这玩意儿写错比出BUG还难受!!!)

类型关键字
布尔类型bool
字符型char
整形int
浮点型float
双浮点型double
无类型void
宽字符型wchar_t
类型范围
char1 个字节-128 到 127 或者 0 到 255
unsigned char1 个字节0 到 255
signed char1 个字节-128 到 127
int4 个字节-2147483648 到 2147483647
unsigned int4 个字节0 到 4294967295
signed int4 个字节-2147483648 到 2147483647
short int2 个字节-32768 到 32767
unsigned short int2 个字节0 到 65,535
signed short int2 个字节-32768 到 32767
long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int8 个字节0 到 18,446,744,073,709,551,615
float4 个字节精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double8 个字节双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long long8 个字节双精度型占8 个字节(64位)内存空间,表示 -9,223,372,036,854,775,807 到 9,223,372,036,854,775,807 的范围
long double16 个字节长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t2 或 4 个字节1 个宽字符

在计算机中不同的数据类型它占用的空间也是不一样的,下面给大家展示一下。

#include<iostream>  
#include <limits>
 
using namespace std;  
  
int main()  
{  
    cout << "type: \t\t" << "************size**************"<< endl;  
    cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);  
    cout << "\t最大值:" << (numeric_limits<bool>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;  
    cout << "char: \t\t" << "所占字节数:" << sizeof(char);  
    cout << "\t最大值:" << (numeric_limits<char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;  
    cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);  
    cout << "\t最大值:" << (numeric_limits<signed char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;  
    cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);  
    cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;  
    cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);  
    cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;  
    cout << "short: \t\t" << "所占字节数:" << sizeof(short);  
    cout << "\t最大值:" << (numeric_limits<short>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;  
    cout << "int: \t\t" << "所占字节数:" << sizeof(int);  
    cout << "\t最大值:" << (numeric_limits<int>::max)();  
    cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;  
    cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);  
    cout << "\t最大值:" << (numeric_limits<unsigned>::max)();  
    cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;  
    cout << "long: \t\t" << "所占字节数:" << sizeof(long);  
    cout << "\t最大值:" << (numeric_limits<long>::max)();  
    cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;  
    cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);  
    cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();  
    cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;  
    cout << "double: \t" << "所占字节数:" << sizeof(double);  
    cout << "\t最大值:" << (numeric_limits<double>::max)();  
    cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;  
    cout << "long double: \t" << "所占字节数:" << sizeof(long double);  
    cout << "\t最大值:" << (numeric_limits<long double>::max)();  
    cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;  
    cout << "float: \t\t" << "所占字节数:" << sizeof(float);  
    cout << "\t最大值:" << (numeric_limits<float>::max)();  
    cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;  
    cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);  
    cout << "\t最大值:" << (numeric_limits<size_t>::max)();  
    cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;  
    cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;  
    // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;  
    cout << "type: \t\t" << "************size**************"<< endl;  
    return 0;  
}

 我们在上面的代码中用到了endl,它的作用是和C语言中的"\n"一样。(都是回车换行)我们使用sizeof()可以来获取各种数据类型的大小。

运行结果:

type:         ************size**************
bool:         所占字节数:1    最大值:1        最小值:0
char:         所占字节数:1    最大值:        最小值:?
signed char:     所占字节数:1    最大值:        最小值:?
unsigned char:     所占字节数:1    最大值:?        最小值:
wchar_t:     所占字节数:4    最大值:2147483647        最小值:-2147483648
short:         所占字节数:2    最大值:32767        最小值:-32768
int:         所占字节数:4    最大值:2147483647    最小值:-2147483648
unsigned:     所占字节数:4    最大值:4294967295    最小值:0
long:         所占字节数:8    最大值:9223372036854775807    最小值:-9223372036854775808
unsigned long:     所占字节数:8    最大值:18446744073709551615    最小值:0
double:     所占字节数:8    最大值:1.79769e+308    最小值:2.22507e-308
long double:     所占字节数:16    最大值:1.18973e+4932    最小值:3.3621e-4932
float:         所占字节数:4    最大值:3.40282e+38    最小值:1.17549e-38
size_t:     所占字节数:8    最大值:18446744073709551615    最小值:0
string:     所占字节数:24
type:         ************size**************

typedef声名

我们可以在编写程序的时候可以使用typedef来给我们的数据类型起一个新名字,下面我们使用typedef来定义一个新的类型:

typedef type newname;

以上代码中“ytpe”是你想转换的目标数据类型,newname是你想起的名字,来我们举个例子

typedef int csdn

那么这样后我们定义数据的时候可以直接使用csdn这个数据类型来代替int类型,来个例子:

#include<iostream>
using namespace std;
typedef int csdn;
    int main(){
    csdn a = 10;
    int b = 9;
    csdn s;
    s = a+b;
    cout << s <<endl;

    return 0;
}

结果就是:

由此呢我们可以知道我们可以使用typedef来定义一个我们想要的数据类型,这样更有利于帮助我们编程。 

类型转换

类型转换就是将一个数据类型转换为另一个数据类型的值,比如说我们将Int转换为float。

在C++中有四种数据类型转换:静态转换、动态转换、常量转换和重新解释转换。

静态转换:

静态转换就是将一种数据类型的值强制转换为另一种类型的值。

静态转换通常用于比较相似的两种数据类型,比如说将Int转为float或者double,如果将int转为char那么数据就会出现错误,所以进行静态转换的时候要慎重~!!!!!!

咱来个例子:

int i = 0;
float f = staic_cast<float>(i);//强制将int类型转为float;

动态转换:

动态转换通常用于将一个基类指针或引用装换位派生类指针或引用。动态转换在运行时进行类型检查,如果不能进行转换则返回空指针或报异常。

我们来个例子:

// 定义一个基类 Base,它目前是一个空类,没有成员变量和成员函数。
  
class Base{};  
  
// 定义一个派生类 Derived,它公有地继承自 Base 类。这意味着 Derived 类将包含 Base 类的所有公有和保护成员(尽管在这个例子中 Base 是空的)。  

class Derived : public Base{};  
  
// 使用 new 关键字动态分配了一个 Derived 类型的对象,并将返回的地址(一个指向 Derived 的指针)赋值给了一个指向 Base 类型的指针 ptr_base。  
// 这是因为 Derived 是 Base 的派生类,所以 Derived 类型的对象可以被视为 Base 类型的对象(这是多态的基础)。  
// 但是,重要的是要注意,ptr_base 指针只能安全地调用 Base 类中定义的成员(除非进行了适当的类型转换)。  

Base* ptr_base = new Derived;  
  
// 使用 dynamic_cast 将 ptr_base(指向 Base 的指针)转换为指向 Derived 的指针。  
// dynamic_cast 在运行时检查 ptr_base 是否确实指向一个 Derived 类型的对象(或其派生类的对象)。  
// 如果是,转换成功,ptr_derived 将指向原始的 Derived 对象;如果不是,转换失败,ptr_derived 将被设置为 nullptr(在 C++11 及以后版本中)。  
// 这个检查是基于类的继承关系的,并且需要 RTTI(运行时类型信息)的支持。  

Derived* ptr_derived = dynamic_cast<Derived*>(ptr_base);

常量转换:

常量转换用于将const类型的对象转换为非const类型的对象,他只用于转换点const属性,不能改变对象的类型

const int i = 10;
int& r = const_cast<int&>(i);//将const int 转化为Int类型

重新解释转换:

重新解释转换时间一个数据类型的值重新解释为另一个数据类型的值,通常我们用于在不同的数据类型之间进行转换。
举个例子:

int i = 10;
float f = reinterpret_cast<float&>(i);//重新解释将Int类型转化为float类型

下课!!!!!

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值