下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C+笔maruzhong/ruzhongmaCC++2.5 1.51.5I/O1天命名空间的概念和使用 简介和编程的基本变化C语言的简介1969年,使用汇编语言编写操作系统,发明了一门语言B语言,-在B语言的基础上NewB语言,后来改名为CC1972C++语言的简介在C语言的基础上,本-特(BjarneStroustrup)做了一定的扩展,起名NewC语言,后来改名C++,1983相关的历史事件 1998年,第一个ISOC++标准诞生(vc6.01998年诞生)C++0x部分编译器支持C++和C语言的比较vixxx.shdate;cal;ls-a;a=10;性//int*pimalloc(sizeof(int));1+1=2doublefloatC++的主要用途级编程的基本变化C语言中源文件:CC++xxx.hppC#include<stdio.h>C++中:#include#includecstdio>去掉.h,02myself.cpp文件2#include3#include4//#include<string.h>#include5usingnamespace7int8 //char string int cin>>name>> cout我是name出生于2014-age return18C语言中:gcc/ccxxx.cC++中:g++/c+xxx.cppgcc/ccxxx.cpp--c-S-o-Ousingnamespace命名空间的学习和使用2#include3usingnamespace6namespace7 intg_balance0;// voidsave(int g_balance+= voiddraw(int g_balance-= 1921namespace2223 intg_balance=0; voidsave(intmoney) g_balance+= voiddraw(int g_balance-= }}cout农行账户的余额是:g_balance{void3739int40 usingnamespace //usingnamespaceabc;两种选择,可以选择两个版本的同名函数和变量, 50 std::cout<<"账户余额是:g_balance="<<abc::g_balance<< using return58自定义命名空间{}使用名字空间的方式usingnamespace作用域限定符,相当于"的std::cout大家好才是真的好usingstd::cout;usingcout大家好才是真的好无名名字空间2#include3usingnamespace56intg_data=78namespace9 intg_data= void cout<<"g_data="<<g_data<< 1517int18 usingnamespace show200 //coutg_datag_dataendl23 cout<<"ns1中:g_data="<<ns1::g_data<<endl;//200 cout<<"全局变量中:g_data="<<::g_data<<endl;// return26扩12#include3usingnamespace5namespace6 intg_balance=89namespace10 voidsave(int g_balance+= 1516namespace17 voiddraw(int g_balance-= 22 25namespace26 intg_balance= 29 voiddraw(intmoney);30}32voidabc::save(int33 g_balance+=3536voidabc::draw(int37 g_balance-=3941int42 usingnamespace cout<<"g_balance="<<g_balance<< cout<<"g_balance="<<abc::g_balance<< return522#include3usingnamespace5namespace6 namespace namespace void cout<<"我们 是:没有蛀牙"<< 1719int20 namespacens1= return27结构体、联合、枚举的不同结构体中的不同 */{... 个完整的数据类型*/typedefstruct typedef structStudent{.[struct]Student员变量时不需要.->之类的(1)C语言中如果一个函数的形参表为空时,表示可以接(2)C++中如果一个函数的形参表中为空时,表示不接受2#include3#include4usingnamespace6int7 struct int void /*struct*/Students= cout<< 是:"<<<< 是:"<<s.age<< cout<<"------------------"<< Student*ps= cout<<"----------------"<< Students2= return32 为什么ss2show函数而打印出来的数据联合的不同之处[union]unu;12#include3#include4usingnamespace6int7 //union int char x= for(inti=0;i<4; returnreturnprintf("&x=%p,buf=}29枚举的不同之处09enum.cpp文件1C++2#include3usingnamespace5int6 enum /*enum*/COLORc= cout<<"c="<<c<<endl; //c0intCOLOR //cout<<"c="<<c<< intic;/COLORint cout<<"i="<<i<< i= cout<<"i="<<i<< return194.类型以及运算符的别4.1类bool#include<stdbool.h>:truefalse,110bool.cpp1C++bool2#include3usingnamespace5int6 boolgender= boolsex= cout<<(gender?"帅哥 ")<< cout<<"gender="<<gender<<",sex="<<sex<< boolalpha表示输出的格式:true 13 cout<<boolalpha<<"gender="<<gender<<",sex="<<sex<<endl; ="<<sizeof(gender)<< cout<<"-------------------"<< boolflag= cout<<"flag="<<flag<< flag= cout<<"flag="<<flag<< flag= cout<<"flag="<<flag<< flag=12> cout<<"flag="<<flag<< return264.2运算符别名&&&|strings; (4)贝尔B语言,在B语言的基础上修改和扩展,NewB语言,后来改名为C语言,出生于1972年在C语言的基础上,BS扩展C语言,NewC1998年出现C++的标准C+年C++98修订 年C++11标准C++兼容C,提供了的特性a.语言风格更加简洁d.e.f.头文件:.hpp.h#include<iostream>#include<cstdio>coutcing++/c++gcc/ccxxx.cpplstdc++usingnamespace{}usingnamespacestd::cout大家好才是真的好usingstd::cout;usingstd::endl;cout大家好才是真的好:truebool类型可以定义变量,作为函数的返回值和参数,也可&&and&|| |bitor(2)动态内存2#include3usingnamespace5intfoo(int6intfoo(void){7intfoo(inti,intj){9//intfoo(intj,int11//voidfoo(inti,int12intfoo(inti,doubled){}//1312intfoo(inti,doubled){}//15struct16 void1819struct20 void24int25 return272#include3usingnamespace5namespace6 voidfoo(int cout"ns1::fooii 1113namespace14 voidfoo(double cout"ns2::foodd 1921int22 usingnamespace usingnamespace using foo(66);// using return38ns1::foo中i=66ns2::food3.14ns1::foo中i=66ns1::foo中i=3ns1::foo中i=66ns2::food3.1403overload.cpp03overload.cpp2#include3usingnamespace5charint6voidbar(char*p,int7 coutbar(1)函数910char*constchar*11voidbar(constchar*p,char12 coutbar(2)函数1416shortchar17voidfun(char18 coutfun(1)函数2021shortint22voidfun(int23 coutfun(2)函数2526shortlonglong27voidfun(longlong28 coutfun(3)函数3032int->...33voidhum(double34 couthum(1)函数3637doubleint38voidhum(inti,int39 couthum(2)函数4143int44 //char* short return5312#include3usingnamespace5voidshow(inti,int6 89voidshow(inti,double10 coutshow(2)函数中,iidd1214int15 return19voidshow(inti,intj){}=>_Z4showiivoidshow(intj,int =>以满足C程序可以直接调用C++模块中的函数12#include3usingnamespace6extern"C"voidshow(inti,int78coutshow函数中:iij9}05main.c文件3//3//进行函数 4voidshow(inti,int5//void_Z4showii(inti,int7int8 return12voidfoo(inti,charc='A',char*p=NULL){...}foo(66,'B');//p= //c='A',p=voidfoo(inti,charc='A',char*p){}//errorvoidfoo(inti,charc='A',char*p=NULL);voidfoo(inti,charc,char*p){}112#include3usingnamespace5//函数 [1~3]6voidfoo(inti,charc='A',char*p=718voidfoo(int9 coutfoo函数中:ii1113int14 //foo(); return202223voidfoo(inti,charc,char*24p?p:"空指针26在有三个参数的foo函数中:i=66,c=B,p foo函数中:i66,cB,p空指针voidfnC语言中表示可以接受任意多个任意类型的实参;C++中表示不接受任何实参2#include5int6 return912int13 15fn();//errorfn(66);//ok类似于C语言中的宏函数,可以进行指令的替换,相对函数有09inline.cpp文件12#include3usingnamespace5inlineintsquare(int6 return810int11 for(inti=1;i<100000; return17分别使用inline修饰和不修饰去观察是否内联 10malloc.cpp2#include3#include4usingnamespace6int7 int*p= *p= cout<<"*p="<<*p<< p= p= for(inti=0;i<10; p[i]= cout<<"p["<<i<<"]="<<p[i]<< p= return23int*pi=newint;deletepi;pi=NULL;int*pi=newint(66);deletepi;pi=NULL;int*pi=newint[10];deletepipiNULL;int*pi=newint[10]{...};delete[]pi;pi=NULL;1//newdelete2#include3usingnamespace5int6 int*pi=new *pi= cout<<"*pi="<<*pi<< delete pi= pi=new cout<<"*pi="<<*pi<< delete pi= cout<<"-----------------"<< pi=new for(inti=0;i<5; pi[i]= cout<<"pi["<<i<<"]="<<pi[i]<< delete[] pi= cout<<"-------------"<< pi=new for(inti=0;i<5; cout<<"pi["<<i<<"]="<<pi[i]<< delete[] pi= //pi=new //int**ppi=new return40int*pi=newint;//okint*pi=newint[5];//okint*pi=newint[3][4];//errorint**pi=newint[3][4];int(*pi)[4]=newint[3][4];delete[]pi;pi=NULL;int(*pi)[3][4]=newint[2][3][4];delete[]pi;pi=NULL;1//newdelete2#include3usingnamespace5int6 int(*pi)[4]=new for(inti=0;i<3; for(intj=0;j<4; pi[i][j]= cout<<"pi["<<i<<"]["<<j<<"]="<<pi[i][j] delete[] pi= cout<<"----------------"<< pi=new for(inti=0;i<3; for(intj=0;j<4; cout<<"pi["<<i<<"]["<<j<<"]="<<pi[i][j] delete[] pi= cout<<"---------------"<< int(*qi)[3][4]=new delete[] qi= return3612#include3usingnamespace5int6 int*pi=new cout<<"*pi="<<*pi<< delete pi= shortbuf[24 int*q=new(buf) cout*qshowbase/*0x*hex/*十六进制**q<< cout<<buf[0]<<''<<buf[1]<< cout<<"q="<<q<<",buf="<<buf<< return18*pi=*q=0x 0x56780x1234q=0xbf8d72ec,buf=3.1并不是一种独立的数据类型,类似于Cinta=10;int&b=ab,bint&c==>int&c=a;c=int&a;//errorint*pa(2)不可以为空,指针可以为空int&a=NULL;//errorconstint&a=NULL常constint&a=11;//okint*pi=NULL;//空指针inta=10;int&b=a;intm=bmam;int*pi=&a;pi=&b;int&arr[10];//errorint*arr[10];//okintarr[10];//okint(*arr)[10];在C++程序尽量去使用,而少使用指针,因为指针容易出现野指针,typedefvoidvoidfn(inti,charc='A',char*p=intC语言中:malloc/calloc/realloc/free除了对Cint*pi=newint;deletepi;pi=NULL;int*pi=newint(66);deletepi;pi=NULL;int*pi=newint[5];delete[]pi;pi=NULL;int*pi=newint[5]{...};delete[]pi;pi=NULL;申请N维数组返回N-1维的数组指针int(*pi)[4]=newnew指针数据类型(初始值|inta=int&b=a;//a起别名bint&cbctypedefvoidtypedefvoid(*)(int)void(*)(int)signal(intsignum,void(*)(int)handler);void(*signal(intsignum,void(*handler)(int)))(int);针int*pi;int*int*pi;int&b=a;int&b=a;int&ba;//效果一样int&b=a;int&ma,&nb;//(3起别名的方式去使用typedefint*PINT;typedefint&RINT;PINTi,jRINTma,nb两个inta=int*pa=&a;//一级指针int**ppa&pa;//二级指针int&b=a;//a起别名为bint&*pb=&b;//error可以定义指针的,不可以定义的inta=10;int*pa=&a;int*&ra=pa;//okint&b=a;int&&rb=b;//int*pa[5]指针数组int&pa[5];//errorint(&pa)[5];//数组01reference.cpp文件1 2#include3usingnamespace5值传递发生数据的拷贝6voidfn(int7 num=911voidfn2(int*12 *pn=15//通15//通 16voidfn3(int&17 rn=1922voidbar(char*23 ps=2527voidbar2(char**28 *pps=31 31 32voidbar3(char*&33 rps=3537int38 intnum= cout<<"num="<<num<< char*ps= cout<<"ps="<<ps<< return47 2#include3usingnamespace56voidswap1(inti,int7 inttemp= i= j=1113voidswap2(int*pi,int*14 inttemp= *pi= *pj=1820voidswap3(int&ri,int&21 inttemp= ri= rj=252728voidprint(constint&i,constint&29 cout<<"i="<<i<<",j="<<j<<3133int34 inti=10,j= cout<<"i="<<i<<",j="<<j<< cout<<"-----------------"<< print(i,j);//20, return45不要返回对局部变量(包括函数的形参)的,可以返回静态局部变量/全局变量/动态内存/实参的/成员变量的,这些返回值都是安全的1 1 2#include3usingnamespace45int&6 intnum= //cout<<"&num="<<&num<< return1012void13 inta= intb= //cout<<"&a="<<&a<<",&b="<<&b<<1719int&20 staticintnum= return2426intg_num=27int&28 return3032int&33 int*pi=new return3638int&foo5(int&39 return4143struct44 int int& return 5052int53 //int&rn= //cout<<"&rn="<<&rn<< //cout<<"rn="<<rn<< intnum= int&rn= cout<<"rn="<<rn<< S s.n= int&rm= cout<<"rm="<<rm<< return71charcintiint)cCintiint(cC++主要用于具有多态特性的父子类指针/之间的转换(以后讲到)04cast.cpp文件2#include3usingnamespace5int6char*char*void*void*pv=//pc=//void*->char*pc=//char*->void*pv= int* int*char //charc= char charint* //pi= //c=static_cast<char>(pi); //pi=static_cast<int*>(c); volatile/*易变的*constintnum //num=100; constint*int* //int*pn= int*pn= *pn= 100100 cout<<"num="<<num<<",*pn="<<*pn<< intn= //int*-> char*ps= cout<<"*ps="<<*ps<<endl; return40constenum(枚举)inline(内联)之#definePIconstdoublepi=#defineMAX(x,y)(x)>(y)?(x):(y)inlineintmax(intx,inty){return}05string.cpp1/string2#include3#include4usingnamespace6int7 //char*ps= strings= cout<<"s="<<s<< string cout<<"s2="<<s2<< strings3= cout<<"s3="<<s3<< s3= cout<<"s3="<<s3<< s=s+ cout<<"s="<<s<< s2+= cout<<"s2="<<s2<< if(s> cout字符串s比较大 cout<<"s[0]="<<s[0]<< cout<<"s[1]="<<s[1]<< c_str()const coutC风格字符串是:s.c_str couts的长度是:s.length char*ps= s= cout<<"s="<<s<< return40面向对象->大处着眼属性:学号为:吃饭学习属性:为:吃饭授课class{class{stringm_name;//intm_age;//{{}class{intm_age;voidlearn(conststring&lesson){...private{{}06class.cpp2#include3#include4usingnamespace6class78 int 12 voidlearn(conststring& 16 cout我是m_name今年m_age岁了,正在学习"<<lesson<<endl; boolsetName(conststring& if(0==returnreturn m_name= return boolsetAge(int if(age<0||age> return return m_age=}return string return int return Student(conststring&name,int m_name= m_age= 5658int59 //s.m_name= //s.m_age= cout<<"设 失败"<< return- cout<<"设 失败"<< return- cout<<s.getName()<<','<<s.getAge()<< cout<<"--------------"<< //string Student cout<<s2.getName()<<','<<s2.getAge()<< return85 2#include3#include4usingnamespace6class78 int 12 Girl(conststring&name,intage,bool m_name= m_age= m_bf= void 24 cout<<"我是"<<m_name<<",今年芳龄是"<<m_age<<","<<(m_bf ?"已经名花有主了":"还没有男朋友")<<endl; 2628void2930 cout我是m_name今年芳龄是m_age 花有主了":"还没有男朋友3133int34GirlGirl Girl return40int*pi;intint&ra=int&raa,&rbb;typedefint*PINT;typedefint&可以指针的,不可以的const修饰作为函数,避免了实参在函数中被修改,并且不要返回局部变量的,可以返回静态局部变量/reinterpret_cast主要用于任意类型的指针/之间的转换,{{{}控制符访控属性本类子类类外 protected保护的 okno ok no{{}{{}12#include3#include4usingnamespace6class78 string int11 void 14 cout我是m_name今年m_age岁了"<<endl; /*Student(conststring&name无名",intage m_name= m_age= 23 Student(conststring&name="",intage= m_name没名 m_age= 3133int34 Student Students2= Studentss[3]={Student",28),Student cout<<"---------------"<< Student*ps=new deleteps;ps= //ps=new ps=new delete[]ps;ps= cout<<"--------------"<< psnewStudent();// //psnewStudent deleteps;ps= return651 定义初始化的比较2#include3usingnamespace5class67 coutA被构造了 1315class1617 19 coutB 2426int27 B return30{{}class int string Dumy(conststring&intnum=10;自动调用构造函数进行初始化->执行构造函数体;2#include3usingnamespace5//intg_num=6class78 constintm_id;200// int&m_age;//=11 User(intid,int //m_age= void 19 cout<<"m_id="<<m_id<<",m_age="<<m_age<< 2123int24 User return28目标类::目标类(const源类&源类对象2#include3usingnamespace5 类6class9class1011 intintintint1516 explicitPoint3d(intx,inty=0,intz= void cout<<'('<<m_x<<','<<m_y<<','<<m_z<<<< 24};2627class2829 int int32 Point2d(intx,int void cout<<'('<<m_x<<','<<m_y<<')'<< friendclass42 46int47 Point3d Point2d cout<<"-------------"<< //Point3dPoint3d= //Point3dpt5100 Point3dpt5= return62classA{voidshow(Bint{BAa;}1//this2#include3#include4usingnamespace6class78 string int11 //Student(conststring&name,int Student(conststring&m_name,int //(*this).m_name=coutcout我是m_name今年m_age{void}cout在构造函数中:thisthisthis->m_age=this->m_name=//s.m_name="<< coutshow函数中:thisthis 2830int31 Student cout在主函数中:&s&s cout<<"-------------------"<< Student cout在主函数中:&s2&s2 return41保存调用对象/正在构造对象的地址11//this指针用于返回 2#include3usingnamespace6class78 10}}<<cout{void}return{Counter&}return{Counter&Counter(intcn=3032int33 Counter c.print();// return432#include3#include4usingnamespace667class10class1112 14 voideducate(Student* cout<<m_answer<< voidreplay(conststring& m_answer= 2931class3233 voidask(conststring&question,Teacher* cout<<question<< 3941voidTeacher::educate(Student*42 cout<<m_answer<<4749int50 Teacher Studentreturnreturn552#include3#include4usingnamespace6class78 string mutableint11 void 17 cout我是m_name今年m_age岁了"<<endl; voidsetName(conststring& m_name= m_age= // coutconst修饰的成员函数return string{cout普通成员函数return 3840int41 Student cout<<"获取到 是:"<<s.getName()<< cout<<"-------------"<< constStudent&rs= cout<< 获取 是:"<<rs.getName()<< constStudent*ps= cout<<"指针获取 是:"<<ps->getName()<< return55constint*pi&i*pi不可变intconst*pi&i*pi不可变int*constpi=&i;//pi不可变constint*constpi;pi*pi ../day05/ 2#include3#include4#include5#include6usingnamespace8class910 int int int14 time_tt= structtm*pt= m_hour=pt-> m_min=pt->22 m_sec=pt->tm_sec; void if(60== m_sec= if(60== m_min= if(24== m_hour= void coutr'/*回车不换行*/<<setfill('0')/*0* <<flush/*刷新 / void 5860int61 Clock return65{{}{{}执行作业 2#include3#include4#include5#include6usingnamespace8class910 int int int14 time_tt= structtm*pt= m_hour=pt- m_minm_min=pt-m_sec=pt-m_sec=pt-} voidvoid{if(60=={m_sec=if(60=={m_min=if(24=={if(60=={m_sec=if(60=={m_min=if(24=={m_hour=}}}}void{cout<<'\r'/*回车不换行*/<<setfill('0')/*空位填0* setw(2m_secflush/*刷新 void 5557int58 Clock return62{{}12#include3usingnamespace5class67 coutA被构造了 coutA 1820//A22int23 cout主函数开始 //A Areturnreturncout主函数结束pa=delete[]A*pa=newpa=delete/*A*pa=new39调用析构函数;而对于堆区的对象来说,当delete时会自动的资源不仅仅包括动态内存,还包括文件的关闭,socket的关闭等等03destruction.cpp文件12#include3usingnamespace5class67 coutA被构造了 coutA 1618class1920 A*22}}m_a=deletecoutB{}coutB被构造了{3335int36 //ABB B return40B被构造了析构B{{}StudentStudents2(s)调用拷贝构造Students3=s;//同上Student*psnew 2#include3#include4usingnamespace6class78 string int11 void 15 cout我是m_name今年m_age岁了"<<endl; cout自定义拷贝构造函数 2426int27 Student cout<<"------------------"<< Student Students3=s;//Student Student*ps=new deleteps;ps= // //s2= return43我我 我 我 a.b.2#include3#include4usingnamespace6class78 int11 m_pName=new delete[] m_pName= void cout我是m_pName今年m_age了 Student(constStudent& m_pName=new 3537int38 Student Student return4506copyConstruction.cpp12#include3usingnamespace5class67 cout自定义构造函数 A(constA& cout自定义拷贝构造函数 1618voidfoo(Aa){Aaa119Afoo(Aareturna;20/A&foo(Aa){returna;21//A&foo(A&a){returna;022因为局部变量生命周期短暂,构造完毕之后还得销毁,于是编译器优化,直接构造新23A24 A return2729int30 Aa1 A Aa3= A*pa=new cout<<"---------------"<< return39StudentStudents2s;//=>s2.operator=Student&operator=(constStudent&{return*this;}一个缺省拷贝赋值运算符函数,本质就是按照字节进行12#include3#include4usingnamespace6class78 int11 m_pName=new delete[] m_pName= void cout我是m_pName今年m_age了 Student(constStudent& m_pName=new Student&operator=(constStudent& if(this!=m_age=m_age=m_pName=newm_pName=delete[] return 5254int55 Student Student cout<<"----------------"<< Student s3= cout<<"---------------"<< intinta=10,b=20,c=(a=(a=b)=cout<<"a="<<a<< // (s4=s3)= return78static修饰局部变量:生命周期变长static修饰函数:防止其他文件使用{static数据类型静态成员变量名数据类型类名::静态成员变量名初始值08static.cpp08static.cpp2#include3usingnamespace5class67 int9 staticint12 void cout<<"m_data="<<m_data<< cout<<"s_data="<<s_data<< 22 staticint //cout<<"m_data="<<m_data<< return 3033intA::s_data=35int36 A cout<<"A::s_data="<<A::s_data<< cout<<"a.s_data="<<a.s_data<< a.s_data= A cout<<"a2.s_data="<<a2.s_data<< //Aa;sizeof(A)= cout<<"sizeof(A)="<<sizeof(A)<<endl;// cout<<"--------------------"<< cout获取的数据是:A::getData cout使用对象获取的数据是:a.getData return53{static返回值类型函数名( {}09static.cpp23#include4usingnamespace6class78 Singleton&operator=(constSingleton&15 staticSingleton19 staticSingleton& return 2427Singleton29int30 //Singletons1; Singleton&s1= cout<<"&s1="<<&s1<< Singleton&s2= cout<<"&s2="<<&s2<< Singleton&s3= cout<<"&s3="<<&s3<< //s1=s2; //cout<<"&s1="<<&s1<< return43Students;string*ps&类名::成员变量名返回值类型(类名::*指针名)(形参 &类名::成员函数名2#include3#include4usingnamespace6class7 voidsetName(conststring& m_name= 1517int18 string pName= Student //s.*(&m_name)=> s.*pName= cout<<"s.m_name="<<s.m_name<< cout<<"------------"<< Student s2.*pName= cout<<"s2.m_name="<<s2.m_name<< cout<<"-----------"<< 36 void(Student::*pSetName)(conststring&name)= //*pSetName=>*(&setName)=> // cout<<"s.m_name="<<s.m_name<< cout<<"s2.m_name="<<s2.m_name<< return45(2)双目运算符重载(3)单目运算符重载(4)不{{}{{}{ 象名{}{数据类型类名::变量名=初始值;{static返回值类型函数名( 参表{}&类名::成员变量名;(3)使用/解&类名::成员函数名(3)双目运算符重载(4)单目运算符重载(5)不intnum=cout<<num<<endl;//10charc='A';cout<<c<<endl;doubled=cout<<d<<endl;//3.14Students;cout<<s<<12#include3usingnamespace5classComplex//复数类67 intm_r 10 Complex(intr=0,inti= friendostream&operator<<(ostream&os,constComplex& friendistream& plex& is>>c.m_r>> return 2124ostream&operator<<(ostream&os,constComplex&25 os<<c.m_r<<'+'<<c.m_i<< return2830int31 Complex // // cout<<c1<< Complex cout<<"c2="<<c2<< coutostream cinistream // cin>> cout输入之后:c2c2 return472/32#include3usingnamespace5class67 int int10 F(ints=0,intm= friendostream&operator<<(ostream&os,constF& // // os<<f.m_s<<'/'<< return friendistream&operator>>(istream&is,F& is>>f.m_s>> return 2628int29 F cout<<"f="<<f<< cin>> cout<<"f="<<f<< return36-12#include3usingnamespace5classComplex//复数类67 intm_r 10 Complex(intr=0,inti= friendostream&operator<<(ostream&os,constComplex& friendistream& plex& is>>c.m_r>> return 22 Complexoperator+(constComplex&c)const 对象返 return Complexoperator-(constComplex&c) Complex&operator+=(constComplex& /*m_r+= m_i+= return return*this=*this+ Complex&operator-=(constComplex& /*m_r-=44 m_i-=c.m_i; return*this=*this-} cout非成员形式的重载 return //int类型之间的+ Complexoperator+(int return 6063ostream&operator<<(ostream&os,constComplex&64 os<<c.m_r<<'+'<<c.m_i<<66 67}69int70 Complex // // cout<<c1<< Complex cout<<"c2="<<c2<< coutostream cinistream // cin>> cout输入之后:c2c2 cout<<"-------------------"<< //Complexc3=88 //Complexc3=operator+(c1,c2); //c3.operator=(c1.operator+(c2)); Complexc3=c1+ cout<<"c3="<<c3<< cout<<"c3-c2="<<c3-c2<< cout<<"------------------"<< inta=10,b= (a+=b)+= cout<<"a="<<a<< cout<<"c1="<<c1<< cout<<"c2="<<c2<<//(c1+=c2)+=cout<<"c1="<<c1<<(c1-=c2)-=cout<<"c1="<<c1<<endl;cout<<"-----------------"<<cout<<"c1="<<c1<<//cout<<"c1+100="<<c1+100<<return111L/*左操作数*/*运算符*R(右操作数s+=>=>运算符重载1/2+1/3=3/6+2/6=5/62#include3usingnamespace5class67 int int10 F(ints=0,intm= friendostream&operator<<(ostream&os,constF& // // os<<f.m_s<<'/'<< return friendistream&operator>>(istream&is,F& is>>f.m_s>> return Foperator+(constF&f) return Foperator-(constF&f) F&operator+=(constF& return*this=*this+ F&operator-=(constF& return*this=*this- 4446int47 Fcoutcout<<"f2="<<f2<<cin>>Fcout<<"f="<<f<< cout<<"---------------"<< // cout<<"f+f2="<<f+f2<< cout<<"f-f2="<<f-f2<< // coutff2的结果:ff2 coutff2的结果是:ff2 return63=>operator#(O);12#include3usingnamespace5class67 int int10 Complex(intr=0,inti= friendostream&operator<<(ostream&os,constComplex& returnos<<c.m_r<<'+'<<c.m_i<<}}{} Complexc= return friendconstComplexoperator++(Complex& Complexc1= return Complex& 44 constComplexoperator-- Complexc= return return 6163int64 Complex66 cout<<"c1="<<c1<<endl; //c1.operator-(); cout<<"-c1="<<-c1<< cout<<"--------------------"<< intm= cout<<"m++="<<m++<< cout<<"m="<<m<<75 //(m++)++;//m++返回的是++之前的临时值,并不是m本身,临时值不能再 //cout<<"m="<< intn= cout<<"++n="<<++n<< cout<<"n="<<n<<endl;// C++++n cout<<"n="<<n<< cout<<"--------------------"<< cout<<"c1="<<c1<<87 //c1.operator++(int) //operator++(c1,int); cout<<"c1++="<<c1++<< cout<<"c1="<<c1<< //(c1++)++; //cout<<"c1="<<c1<< // cout<<"++c1="<<++c1<< cout<<"c1="<<c1<< cout<<"c1="<<c1<<cout<<"-----------------"<<cout<<"c1--="<<c1--<<cout<<"c1="<<c1<< cout<<"--c1="<<--c1<<cout<<"c1="<<c1<< cout<<"c1="<<c1<<return109数只有一个参数-操作数符函数有两个参数,第一个是操作数,第二个是int类型的哑元2#include3usingnamespace5class67 int int10 F(ints=0,intm= friendostream&operator<<(ostream&os,constF& returnos<<f.m_s<<'/'<< // returnF(- F& return constF Ff= return 3638int39 F cout<<"f="<<f<< cout<<"-f="<<-f<< cout<<"f++="<<f++<< cout<<"f="<<f<< cout<<"++f="<<++f<< return47{}07conversion.cpp2#include3usingnamespace5class7class89 int int int13 Point3d(intx,inty,int returnos<<'('<<pt3.m_x<<','<<pt3.m_y<<<<pt3.m_z<< 2325class2627 int int30 Point2d(intx,int operator cout类型转换运算符函数 return 43 friendclassPoint3d;44}; 48int49 Point3d cout<<"pt3="<<pt3<< Point2d cout<<"pt2="<<pt2<< //pt2.operator //pt3pt2 //pt3= cout<<"pt3="<<pt3<< return61-.-成员操作.*如:*push在数组的尾部插入一个元素pop()在数组的尾部弹出一个元素get()根据下标获取一个元素的值set() 12#include3usingnamespace5class67 intm_len intm_pos11 IntArray(intlen):m_arr(newint[m_len= delete[] m_arr= boolpush(int if(m_pos== return return m_arr[m_pos++]= return boolpop(int& if(0== return return num=m_arr[-- return boolget(intpos,int& }}cout<<}cout<<m_arr[i]<<'{for(inti=0;i<m_pos;{void}returnm_arr[pos]=}return{if(pos<0||pos>={boolset(intpos,int}returnnum=}return{if(pos<0||pos>=7173int74 IntArray for(inti=0;i<10; cout<<boolalpha<<ia.push(10)<< cout<<"--------------"<< intnum= cout<<"num="<<num<<cout<<"num="<<num<< cout<<"-------------"<< cout96 for(inti=0;i<10;i++) cout<<num<<' cout<<cout<<ia.pop(num)<<return104(2)new/delete操作符的重载intnum=//cout.operator<<(int);cout<<num<<endl;Students;////cout<<s<<friendoperator<<(ostream&os,constStudent&////cinistreamcin>>friendopertor>>(istream&is,Student&L.operator#(R);operator#(L,R);Complex////operator+(c1,c2);cout<<c1+c2<<endl;operator#(O);Complex//operator-(c);cout<<-c<<endl;数,第二个是int类型的哑元{}::..*?:sizeof(2)new/delete操作符的重载2#include3usingnamespace5class67 int* int int11 IntArray(intlen):m_arr(newint[m_len= }}return{intoperator[](int}cout<<}cout<<m_arr[i]<<'{for(inti=0;i<m_pos;{void}returnm_arr[m_pos++]=}return{if(m_pos=={boolpush(int}m_arr=delete[]4143int44 IntArray for(inti=0;i<5; 48 cout<<"---------------"<< for(inti=0;i<5; // cout<<ia[i]<<' cout<< return601函数操作符()2#include3usingnamespace5class67 intoperator()(int return doubleoperator()(double return 1719int20 Square // cout计算的结果是:s(1024 cout<<"计算的结果是:"<<s(3.14)<<endl; return0;26*1*2#include3usingnamespace5class67 coutA被构造了 cout析构A void coutfoo()函数调用了 2022class2324 A*26 PA(A* delete m_pa= return A& return 4446int4748 //A*pa=newA; //delete //pa= PApa(new //pa.operator->()- //pa.operator*() return581//newdelete2#include3#include4usingnamespace6class79 int11 coutA被构造了 cout析构A staticvoid*operatornew(size_t void*p= << return staticvoidoperatordelete(void* coutDelete中:pp p= staticvoid*operatornew[](size_t void*p= << return staticvoidoperatordelete[](void* coutdelete[]中:pp}}p=4749class5051 coutB被构造了 cout析构B 6062int63 A*pa=new delete pa= cout<<"---------------"<< //A*pa2= << delete[] pa= cout<<"------------------"<< B*pb=new delete pb= return84可以通过new/delete运算符重载的函数来实现publicprotectedprivate属性:学行为:吃饭学习属性:行为:吃饭授课属性: 12#include3#include4usingnamespace6 78string911 15 cout<<"我是"<<m_name<<",今年"<<m_age<<"岁了"<<endl; voideat(conststring& coutfood真好吃 string return 29 32classStudent: 3334 cout我是学生,我的学号是:m_id voidlearn(conststring& cout正在学习lesson //cout学号是:m_id void //cout<<"m_name="<<m_name<< cout是:getName{int//inteat(conststring&//voideat(conststring&}吃 6264int65 Student /*cout<<"------------------"<< 72 *p=newStudent(" //p cout<<"----------------"<< cout<<"--------------"<< return88{public:公有方式继承private:私有方式继承12#include3#include4usingnamespace6 78 string int11 14 cout<<"我是"<<m_name<<",今年"<<m_age<<"岁了"<<endl; voideat(conststring& coutfood真好吃 2022classTeacher: 2324 string2627 Teacher(conststring&name,intage,conststring& cout<<"我是教师 是:"<<m_job<< voideducate(conststring& cout我在教lesson 3537int38 Teacher return43控class{void classB:private{void{}class{2#include3usingnamespace5class67 coutA被构造了 coutA 1618classB:public1920 coutB被构造了 coutB 2931int32 ABB析构 B*pb=new delete pb= cout<<"-------------------"<< AB A*pa=new delete pa= return44(1)[(3)*void*operatornew(size_t{}voidoperatordelete(void*{}void*operatornew[](size_t{ 信}voidoperatordelete[](void*{}/StudentTeacher(子类/派生类{}2#include3#include4usingnamespace6 78 string int11 15 cout我是m_name今年m_age岁了"<<endl; (const & cout 自定义拷贝构造函数 2224classTeacher:public 25{26 28 30 Teacher(conststring&name,intage,conststring& cout<<"我是教师,我 是:"<<m_job<< Teacher(constTeacher& cout自定义拷贝构造函数 3941int42 Teacher return46{ 2#include3usingnamespace5class67 int9 A(intn= coutA 1518classB:virtualpublic1920 B(intn= 24 cout<<"构造B"<<endl; voidset(int m_n=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二四年度版权评估合同版权价值评估与交易建议2篇
- 2024年墙纸供需合同样本2篇
- 2024年版电子商务平台技术服务合同2篇
- 2024年度彩色宝石交易合同3篇
- 2024年共谋发展:企业经营权合作合同3篇
- 二零二四年度品牌合作合同(体育赛事)3篇
- 全额支付房产买卖契约20243篇
- 2024年度演艺经纪合同:演艺公司与艺人之间的经纪合作协议2篇
- 2024年度商砼工程质量保修合同2篇
- 2024年版贷款中介服务合同2篇
- 感知觉障碍完整版本
- 2023年上海市中考英语试题及参考答案(word解析版)
- 《刑罚的体系和种类》课件
- 《杰出的科学家》课件
- 青岛版五四制五年级上册数学单元测试卷第三单元 包装盒-长方体和正方体(含答案)
- 井下电气安全培训课件
- 仓库降本增效方案培训课件
- 北京市西城区五年级数学(上)期末试卷(含答案)
- 脑血管病的一级预防
- (完整)中小学教师职称评定答辩题
- 沈从文先生在西南联大全文
评论
0/150
提交评论