类型转换

一、数据类型间转换

1、隐式转换

当char,short与int类型运算时,将char,short转换成int类型;

级别低的自动转换成级别高的;

当a=b,a,b不同类型时,b先转换成a类型后进行赋值。

 

2、显式转换

1)强制类型转换

int i;

cout<<(float)i;

2)函数法

int i;

cout<<float(i);

 

二、数据类型向类类型转换

1、隐式

class example

{

//...

public:

example(int);

example(const char * ,int=0 );

};

main()

{

example a=example(3);

example b=5;

example c="windows";

}

2、保护转换隐式

class example

{

example(int s);

public:

example make(int s)

{return s; }

};

main()

{

example a;

a=a.make(6);

}

 

三、类类型向数据类型转换

class example

{

int val;

public:

example()

{val=0;}

example(int x )

{val=x;}

operator float();

operator int();

};

example::operator float()

{return val; }

example::operator float()

{return (float)val; }

main()

{

example a(4),b(6);

cout<<int(a)+3<<","<<float(b)+4;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值