网站开发可以开发哪些,做网站最专业的公司,网站升级建设方案,qq浏览器官网【泛型编程】
若多组类型不同的数据需要使用相同的代码处理#xff0c;在C语言中需要编写多组代码分别处理#xff0c;这样做显然太过繁琐#xff0c;C增加了虚拟类型#xff0c;使用虚拟类型可以实现一组代码处理多种类型的数据。
虚拟类型是暂时不确定的数据类型#… 【泛型编程】
若多组类型不同的数据需要使用相同的代码处理在C语言中需要编写多组代码分别处理这样做显然太过繁琐C增加了虚拟类型使用虚拟类型可以实现一组代码处理多种类型的数据。
虚拟类型是暂时不确定的数据类型它在定义时不指定具体类型而是在使用时指定类型可以用于单个数据类型也可以用于数组、结构体使用虚拟类型编程也称为泛型编程意为广泛类型。
使用虚拟类型的代码称为模板但是全局数据不能使用虚拟类型全局数据也无需定义为模板。
全局函数模板
函数模板的参数、返回值可以使用虚拟类型参数与返回值的数量必须相同只是类型不同。
#include iostream
template typename T //使用template关键词定义虚拟类型T为虚拟类型名称
T add(T t1, T t2)
{return t1t2;
}
int main()
{printf(%d\n, add(1, 2)); //T指定为int类型printf(%f\n, add(0.1f, 0.2f)); //T指定为float类型return 0;
}
虚拟类型使用template关键词定义也可以使用class定义可以在符号内定义多个虚拟类型不同虚拟类型使用,符号隔开但是每个定义的虚拟类型都必须使用否则编译报错。
函数模板内可以使用多个虚拟类型执行函数模板时需要为其内部虚拟类型指定具体类型当多次执行函数模板并为虚拟类型设置不同的具体类型时编译器会将函数模板编译为多个函数分别使用不同的指令处理数据上述代码中的add函数实际上会被编译器编译成两个函数分别使用整数运算指令和浮点数运算指令处理数据模板只是简化了程序员的工作量并没有减少程序编译后的代码量。
成员函数模板
#include iostream
class math
{
public:template typename T //定义虚拟类型T add(T t1, T t2) const{return t1t2;}
};
int main()
{math math1;printf(%d\n, math1.add(1, 2));printf(%f\n, math1.add(0.1f, 0.2f));return 0;
}
类模板
成员数据使用虚拟类型的类称为类模板。
#include iostream
template typename T
class math
{
private:T a,b;public:math(T t1, T t2){a t1;b t2;}T add() const{return ab;}
};
int main()
{mathint math1(1, 2); //创建对象时需要使用符号设置数据类型printf(%d\n, math1.add());mathfloat math2(0.1, 0.2);printf(%f\n, math2.add());return 0;
}
成员数组模板
虚拟类型用于类成员数组时可以额外定义一个变量此变量用于设置数组模板的长度从而实现数组的类型、长度都在定义时临时确定。
#include iostream
template typename T, int i //变量i设置数组长度
class array
{
private:T a[i];public://......
};
int main()
{arrayint, 5 array1; //成员数组类型为int包含5个元素arrayfloat, 6 array2; //成员数组类型为float包含6个元素return 0;
} 【符号重载】
在C中可以借助operator关键词将语法中的某些符号、关键词当做函数名从而重新定义此符号的功能这种函数称为符号函数调用符号函数执行时可以无需指定operator关键词直接使用重载的符号即可全局函数和成员函数都可以设置为符号函数。
使用符号函数会让代码更简洁比如连接字符串函数使用符号作为函数名即可通过符号连接两个字符串。 可重载的符号如下 - * / -- % || ! | ~ ^! - * / % ^ |
, () []
- -*
new delete new[] delete[] 符号重载注意事项 1.只能使用C语法中原有符号不能使用C语法没有的符号。 2. () [] - 这四种符号只能用于成员函数不能用于全局函数。 3.重载不同符号时函数可以设置的参数个数不同。 4.重载相同符号时全局函数与成员函数可以设置的参数个数不同。 5.重载不同符号有不同的限制比如重载-符号不能计算两个double数据相加、重载%符号不能只有一个操作数、重载new的函数返回值只能是void类型指针。 重载 符号
重载符号连接固定长度字符串对象并返回连接结果。
#include iostream
class string
{
public:char strvar[100] {0};/* operator符号函数连接string对象 */string operator(const string conobj) const{string result; //存储连接结果unsigned int strlen 0; //存储本类strvar空字符下标unsigned int conlen 0; //存储conobj.strvar空字符下标result.strvar[0] 0; //初始空字符/* 查询本类strvar空字符下标若没有空字符则当做不合规string对象处理strlen 0 */for(int i 0; i 100; i){if(strvar[i] 0){strlen i;break;}}/* 查询conobj.strvar空字符下标 */for(int i 0; i 100; i){if(conobj.strvar[i] 0){conlen i;break;}}/* result连接本类strvar */if(strlen ! 0){result *this;}/* result连接conobj.strvar */if(conlen ! 0){if(strlen 0){result conobj;}else{for(int i 0; strlen99 iconlen; i){result.strvar[strlen] conobj.strvar[i];strlen;}result.strvar[99] 0; //确保末尾元素是空字符}}return result;}/* 重载operator符号函数连接字符串 */string operator(const char * conobj) const{string result;unsigned int strlen 0;unsigned int conlen 98; //若字符串长度超标则最多使用99个字符result.strvar[0] 0;/* 查询本类strvar末尾下标 */for(int i 0; i 100; i){if(strvar[i] 0){strlen i;break;}}/* 查询conobj末尾下标只需查询99个字节 */for(int i 0; i 99; i){if(conobj[i] 0){conlen i;break;}}/* 连接本类strvar */if(strlen ! 0){result *this;}/* 连接conobj */if(conlen ! 0){for(int i 0; strlen99 iconlen; i){result.strvar[strlen] conobj[i];strlen;}result.strvar[99] 0;}return result;}
};
int main()
{string ali {阿狸};string taozi {桃子};string zoo ali taozi; //调用ali.operator函数连接string对象printf(%s\n, zoo.strvar);zoo zoo 喜羊羊美羊羊; //调用zoo.operator函数连接字符串printf(%s\n, zoo.strvar);return 0;
} 重载 符号
重载符号修改动态长度字符串对象字符串成员定义为私有只能通过operator符号函数修改。
#include iostream
#include string.h
class string
{
private:char * strvar;public:string(){strvar new char;*strvar 0;}string(const char * assobj){size_t asslen strlen(assobj); strvar new char[asslen1];strcpy(strvar, assobj);strvar[asslen1] 0; //末尾空字符}~string(){delete [] strvar;}/* 返回字符串地址 */const char * get() const{return strvar;}void operator(const char *assobj){size_t asslen strlen(assobj);/* 释放旧内存 */delete [] strvar;/* 申请新内存 */strvar new char[asslen1];/* 内存赋值 */strcpy(strvar, assobj);strvar[asslen1] 0;}
};int main()
{string zoo(阿狸);printf(%s\n, zoo.get());zoo 喜羊羊;printf(%s\n, zoo.get());return 0;
} 文章转载自: http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.llyjx.cn.gov.cn.llyjx.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.kchwr.cn.gov.cn.kchwr.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.sbyhj.cn.gov.cn.sbyhj.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.mfbcs.cn.gov.cn.mfbcs.cn http://www.morning.bbmx.cn.gov.cn.bbmx.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.ljngm.cn.gov.cn.ljngm.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.skrcn.cn.gov.cn.skrcn.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.hsrpc.cn.gov.cn.hsrpc.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.pdghl.cn.gov.cn.pdghl.cn