首页 C++_类型转换

C++_类型转换

举报
开通vip

C++_类型转换类型转换:将一种类型的值转换为另一种类型的值类类型:class、stuct、union类型标准类型:除类类型外的所有类型,如int类型种类:类型转换有4种:1)标准类型->标准类型 2)标准类型->类类型3)类类型->标准类型4)类类型->类类型一、标准类型转变为标准类型方法:1)隐式转换         使用场合:程序未显式类型转换,但是含有混合运算,赋值语句,函数返回值,形参和实参转换2)...

C++_类型转换
类型转换:将一种类型的值转换为另一种类型的值类类型:class、stuct、union类型 标准 excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载 类型:除类类型外的所有类型,如int类型种类:类型转换有4种:1)标准类型->标准类型 2)标准类型->类类型3)类类型->标准类型4)类类型->类类型一、标准类型转变为标准类型 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 :1)隐式转换         使用场合:程序未显式类型转换,但是含有混合运算,赋值语句,函数返回值,形参和实参转换2)显式转换        使用场合:程序显式类型转换        方法:C语言的强制法(int)a和C++的函数法int(a)         注意:当类型名有两个以上时(unsignint),不能使用C++的函数法,但是可以使用C语言的强制法,所以以后可以直接使用C的强制法,不会错二、标准类型转化成类类型方法:构造函数和自定义的重载赋值号=的函数必备参数:它们都需要有标准类型的参数(这个标准类型就是等号右边的值类型)代码:[cpp]viewplaincopyprint?1.#include <iostream>  2.using namespace std;  3.class String  4.{  5.private:  6.    int len;  7.    char* strContent;  8.public:  9.    String(int i);  10.    String(char* p);  11.    String& operator=(int i);  12.    String& operator=(char* p);  13.    void show();  14.};  15.String::String(int i)  16.{  17.    len=i;  18.    strContent = new char[len];  19.    memcpy(strContent,"",len);  20.}  21.String::String(char* p)  22.{  23.    len=strlen(p)+1;  24.    strContent = new char[len];  25.    memcpy(strContent,p,len);  26.}  27.String& String::operator=(int i)  28.{  29.    len=i;  30.    if (strContent!=NULL)  31.    {  32.        delete []strContent;  33.    }  34.    strContent = new char[len];  35.    memcpy(strContent,"",len);  36.    return *this;  37.}  38.  39.String& String::operator=(char* p)  40.{  41.    len=strlen(p)+1;  42.    if (strContent!=NULL)  43.    {  44.        delete []strContent;  45.    }  46.    strContent = new char[len];  47.    memcpy(strContent,p,len);  48.    return *this;  49.}  50.  51.  52.void String::show()  53.{  54.    cout<<"字符串长度为:"<<len<<endl;  55.    cout<<"字符串内容为:"<<strContent<<endl;  56.}  57.  58.void main()  59.{  60.      61.    String a=1;//调用构造函数  62.    a.show();  63.    String b="123";//调用构造函数  64.    b.show();  65.    b=6;     //调用重载运算符=  66.    b.show();  67.    b="456";//调用重载运算符=  68.    b.show();  69.    system("pause");  70.}  #include<iostream>usingnamespacestd;classString{private: intlen; char*strContent;public: String(inti); String(char*p); String&operator=(inti); String&operator=(char*p); voidshow();};String::String(inti){ len=i; strContent=newchar[len]; memcpy(strContent,"",len);}String::String(char*p){ len=strlen(p)+1; strContent=newchar[len]; memcpy(strContent,p,len);}String&String::operator=(inti){ len=i; if(strContent!=NULL) { delete[]strContent; } strContent=newchar[len]; memcpy(strContent,"",len); return*this;}String&String::operator=(char*p){ len=strlen(p)+1; if(strContent!=NULL) { delete[]strContent; } strContent=newchar[len]; memcpy(strContent,p,len); return*this;}voidString::show(){ cout<<"字符串长度为:"<<len<<endl; cout<<"字符串内容为:"<<strContent<<endl;}voidmain(){ Stringa=1;//调用构造函数 a.show(); Stringb="123";//调用构造函数 b.show(); b=6;//调用重载运算符= b.show(); b="456";//调用重载运算符= b.show(); system("pause");}注意:为了避免标准类型隐式转换为类类型(OBJobj=20),可以把构造函数放在私有段,在创建对象的时候就不能直接调用构造函数了,           这时创建对象可以通过定义一个静态成员函数或一个友元函数来间接调用构造函数来代码:[cpp]viewplaincopyprint?1.#include <iostream>  2.using namespace std;  3.class String  4.{  5.private:  6.    int len;  7.    char* strContent;  8.    String(char* p);  9.public:  10.    static String make(char* p);  11.    void show();  12.};  13.String::String(char* p)  14.{  15.    len=strlen(p)+1;  16.    strContent = new char[len];  17.    memcpy(strContent,p,len);  18.}  19.  20.String String::make(char* p)  21.{  22.    String a=p;//创建一个类,并调用构造函数  23.    return a;  24.}  25.  26.void String::show()  27.{  28.    cout<<"字符串长度为:"<<len<<endl;  29.    cout<<"字符串内容为:"<<strContent<<endl;  30.}  31.  32.void main()  33.{  34.         String a="123";//错误  35.    String a = String::make("123456");  36.    a.show();  37.    system("pause");  38.}  #include<iostream>usingnamespacestd;classString{private: intlen; char*strContent; String(char*p);public: staticStringmake(char*p); voidshow();};String::String(char*p){ len=strlen(p)+1; strContent=newchar[len]; memcpy(strContent,p,len);}StringString::make(char*p){ Stringa=p;//创建一个类,并调用构造函数 returna;}voidString::show(){ cout<<"字符串长度为:"<<len<<endl; cout<<"字符串内容为:"<<strContent<<endl;}voidmain(){Stringa="123";//错误 Stringa=String::make("123456"); a.show(); system("pause");}三、类类型->标准类型和类类型->类类型方法:引入特殊的成员函数---类型转换函数类型转换函数:在类对象之间提供一种类似显式类型转换的 机制 综治信访维稳工作机制反恐怖工作机制企业员工晋升机制公司员工晋升机制员工晋升机制图 。语法:[cpp]viewplaincopyprint?1.Class 源类类名 //在此类中定义  2.{  3.    Operator 目的类型()  4.    {  5.        Return 目的类型的数据;  6.    }  7.}  Class源类类名//在此类中定义{ Operator目的类型() { Return目的类型的数据; }}作用:将对象转换成目的类型的变量,目的类型如果是标准类型也可以是类类型注意一:类型转换函数没有参数、没有返回类型、但是有返回值(一个type的实例)。注意二、类型转换函数只能定义为类的成员函数,而不能是友元函数(不允许有参数)注意三、类型转换函数不可以被超载,因为没有参数代码:[cpp]viewplaincopyprint?1.#include <iostream>  2.using namespace std;  3.class point  4.{  5.private:  6.    int x;  7.public:  8.    point()  9.    {  10.        x=10;  11.    }  12.    operator int() //类型转换函数,转换为整型  13.    {  14.        return x;  15.    }  16.};  17.void main()  18.{  19.    point a;  20.    int t=a;//隐式调用 类型转换函数  21.    cout<<t<<endl;  22.    int z = a.operator int();//显示调用 类型转换函数  23.    cout<<z<<endl;  24.    system("pause");  25.}  #include<iostream>usingnamespacestd;classpoint{private: intx;public: point() { x=10; } operatorint()//类型转换函数,转换为整型 { returnx; }};voidmain(){ pointa; intt=a;//隐式调用类型转换函数 cout<<t<<endl; intz=a.operatorint();//显示调用类型转换函数 cout<<z<<endl; system("pause");}注意一:一般使用隐式方式,当需要明确指出用哪一个类型转换函数时,才使用显式方式注意二:注意下面两种转换方式:[cpp]viewplaincopyprint?1.int a = obj;//使用类型转换函数进行转换  2.OBJ obj=a;  //使用构造函数进行转换  inta=obj;//使用类型转换函数进行转换OBJobj=a;//使用构造函数进行转换注意三、当一个类既有用于转换的构造函数,又拥有类型转换函数如:构造函数:A(inta);类型转换函数:operatorint()则,obj=obj+n就有两种解释:类和标准类型可以互相转化,隐式转换有二义性解释一:可以先把对象转换为数,再两个数相加解释二:可以先把数转换成类,再相加解决方法:这是需要显示使用类型转换函数:方法一:先把数变对象,两个对象再相加              INTobj1=n;Obj=obj+obj1两个对象相加方法二:先把对象变数,两个整数再相加             Obj=(int)obj+n;即:用户类型定义的类型转换函数,只有无二义性的时候,才能使用隐式转换
本文档为【C++_类型转换】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_083055
暂无简介~
格式:doc
大小:28KB
软件:Word
页数:0
分类:互联网
上传时间:2013-05-23
浏览量:25