编程练习(2)

#include <iostream>
#include <vector>
#include <map>
#include <Windows.h>

//  BYTE=unsigned char
//   WORD=unsigned short
//   DWORD=unsigned long
using namespace std;

void main()
{
 int i;
 double a;
 float b;
 WORD e;
 DWORD c;
 vector<int> pvector;
 vector<DWORD> Dvector;
 map<DWORD,DWORD> DMap;
 map<int,int>iMap;

 cout<<"32位系统的各个类型的字节数"<<endl;
 cout<<"int类型:"<<sizeof(i)<<endl;
 cout<<"double类型:"<<sizeof(a)<<endl;
 cout<<"float类型:"<<sizeof(b)<<endl;
 cout<<"WORD类型:"<<sizeof(e)<<endl;
 cout<<"DWORD类型:"<<sizeof(c)<<endl;
 cout<<"vector<int> pvector类型:"<<sizeof(pvector)<<endl;
 pvector.resize(100);
 cout<<"执行pvector.resize(100)后,vector<int> pvector类型:"<<sizeof(pvector)<<endl;
 cout<<"vector<DWORD> Dvector类型:"<<sizeof(Dvector)<<endl;
 cout<<"map<DWORD,DWORD>类型:"<<sizeof(map<DWORD,DWORD>)<<endl;
 cout<<"map<DWORD,DWORD> DMap类型:"<<sizeof(DMap)<<endl;

 cout<<"map<int,int>类型:"<<sizeof(iMap)<<endl;
 system("pause");
 //return 0;
 
}

运行结果:

 


2\

/*
******************************************************************
* 一元操作符与二元操作符
* 当一元运算符被定义为成员函数时,没有参数,成员函数会被运算符右侧的对象调用;
* 当二元运算符被定义为成员函数时,只含有一个参数,成员函数会被运算符左侧的对象调用。
* C++标准中有这样详细的阐述:
* A binary operator shall be implemented either by a non-static member function with
* one parameter or by a non-member function with two parameters
******************************************************************
*/
#include <iostream>
using namespace std;
class A
{
private:
 int a;
public:
 A(){a = 0;}
 void operator++()
 {
  a+=1;

 }
 void operator++(int)
 {
  a += 2;
 }
 friend void print(const A &c);
};

void print(const A &c)
{
 cout<<c.a<<endl;
}

void main()
{
 A classa;
 print(classa);
 ++classa;//调用一元操作

 print(classa);
 classa++;//调用二元操作
 print(classa);
 system("pause");
}

运行结果:

 

3\
#include <stdio.h>
int main()
{
 double a =8;
 float b = 2;
 printf("%d\n",5.01);//输出结果会让你大吃一惊,是一个大数字
 printf("%f\n",5);//更不可思议,在printf函数里,float会自动转换成double。因此4字节(32位平台)的int类型的值会被当成8个字节的double型来读取。
 //结果可想而知,内存访问越界,程序运行结果是0.000000(小数点后面跟了6个0)
 printf("%lf\n",a);
 printf("%f\n",b);//%lf,%f都是按照double类型的数字来输出。

 return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值