幾個(gè)典型運(yùn)算符重載(C++)(四)_第1頁(yè)
幾個(gè)典型運(yùn)算符重載(C++)(四)_第2頁(yè)
幾個(gè)典型運(yùn)算符重載(C++)(四)_第3頁(yè)
幾個(gè)典型運(yùn)算符重載(C++)(四)_第4頁(yè)
幾個(gè)典型運(yùn)算符重載(C++)(四)_第5頁(yè)
已閱讀5頁(yè),還剩50頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

7.3幾個(gè)典型運(yùn)算符重載2023/1/131編輯課件7.3幾個(gè)典型運(yùn)算符重載數(shù)學(xué)類(lèi)中常用的幾個(gè)運(yùn)算重載的特點(diǎn)和應(yīng)用

6.3幾個(gè)典型運(yùn)算符重載2023/1/132編輯課件7.3.1重載++

與--

設(shè) AAobject;運(yùn)算符++和--有兩種方式:前置方式:

++Aobject --Aobject后置方式:

Aobject

++ Aobject--成員函數(shù)

重載 A::Aoperator++();

解釋為: Aobject.operator++();

友員函數(shù)

重載 friendAoperator++(A&);

解釋為: operator++(Aobject);

成員函數(shù)

重載 A::Aoperator++(int);

解釋為: Aobject.operator++(0);

友員函數(shù)

重載: friendAoperator++(A&,int);

解釋為: operator++(Aobject,0)偽參數(shù)6.3.1重載++與--2023/1/133編輯課件成員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};Increaseoperator++(); //前置

Increaseoperator++(int); //后置

private:unsignedvalue;};IncreaseIncrease::operator++() {value++;return*this;} IncreaseIncrease::operator++(int) {Increasetemp;temp.value=value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}6.3.1重載++與--2023/1/134編輯課件成員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};

Increaseoperator++();

//前置

Increaseoperator++(int); //后置

private:unsignedvalue;};IncreaseIncrease::operator++() {value++;return*this;}

IncreaseIncrease::operator++(int) {Increasetemp;temp.value=value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}Increaseoperator++();IncreaseIncrease::operator++(){value++;return*this;}++n;預(yù)定義版本6.3.1重載++與--2023/1/135編輯課件成員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};

Increaseoperator++();

//前置

Increaseoperator++(int); //后置

private:unsignedvalue;};IncreaseIncrease::operator++() {value++;return*this;}

IncreaseIncrease::operator++(int) {Increasetemp;temp.value=value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}Increaseoperator++();IncreaseIncrease::operator++(){value++;return*this;}++n;重載版本6.3.1重載++與--2023/1/136編輯課件成員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};Increaseoperator++(); //前置

Increaseoperator++(int);//后置

private:unsignedvalue;};IncreaseIncrease::operator++() {value++;return*this;} IncreaseIncrease::operator++(int) {Increasetemp;temp.value=value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}Increaseoperator++(int);IncreaseIncrease::operator++(int) {Increasetemp;temp.value=value++;returntemp;}n++;預(yù)定義版本6.3.1重載++與--2023/1/137編輯課件成員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};Increaseoperator++(); //前置

Increaseoperator++(int);//后置

private:unsignedvalue;};IncreaseIncrease::operator++() {value++;return*this;} IncreaseIncrease::operator++(int) {Increasetemp;temp.value=value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}Increaseoperator++(int);IncreaseIncrease::operator++(int) {Increasetemp;temp.value=value++;returntemp;}n++;重載版本6.3.1重載++與--2023/1/138編輯課件成員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};Increaseoperator++(); //前置

Increaseoperator++(int);//后置

private:unsignedvalue;};IncreaseIncrease::operator++() {value++;return*this;} IncreaseIncrease::operator++(int) {Increasetemp;temp.value=value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}6.3.1重載++與--2023/1/139編輯課件友員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};friendIncreaseoperator++(Increase&); //前置

friendIncreaseoperator++(Increase&,int); //后置

private:unsignedvalue;};Increaseoperator++(Increase&a){a.value++;returna;} Increaseoperator++(Increase&a,int){Increasetemp(a);a.value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}6.3.1重載++與--2023/1/1310編輯課件友員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};

friendIncreaseoperator++(Increase&);

//前置

friendIncreaseoperator++(Increase&,int); //后置

private:unsignedvalue;};Increaseoperator++(Increase&a){a.value++;returna;}

Increaseoperator++(Increase&a,int){Increasetemp(a);a.value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}friendIncreaseoperator++(Increase&);Increaseoperator++(Increase&a){a.value++;returna;}++n;通過(guò)引用參數(shù)操作對(duì)象6.3.1重載++與--2023/1/1311編輯課件友員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};friendIncreaseoperator++(Increase&); //前置

friendIncreaseoperator++(Increase&,int);

//后置

private:unsignedvalue;};Increaseoperator++(Increase&a){a.value++;returna;} Increaseoperator++(Increase&a,int){Increasetemp(a);a.value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}friendIncreaseoperator++(Increase&,int);Increaseoperator++(Increase&a,int){Increasetemp(a);a.value++;returntemp;}n++;復(fù)制構(gòu)造局部對(duì)象6.3.1重載++與--2023/1/1312編輯課件友員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};friendIncreaseoperator++(Increase&); //前置

friendIncreaseoperator++(Increase&,int);

//后置

private:unsignedvalue;};Increaseoperator++(Increase&a){a.value++;returna;} Increaseoperator++(Increase&a,int){Increasetemp(a);a.value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}friendIncreaseoperator++(Increase&,int);Increaseoperator++(Increase&a,int){Increasetemp(a);a.value++;returntemp;}n++;偽參數(shù)6.3.1重載++與--2023/1/1313編輯課件友員函數(shù)重載++#include<iostream.h>classIncrease{public:Increase(){value=0;}voiddisplay()const{cout<<value<<'\n';};friendIncreaseoperator++(Increase&); //前置

friendIncreaseoperator++(Increase&,int); //后置

private:unsignedvalue;};Increaseoperator++(Increase&a){a.value++;returna;} Increaseoperator++(Increase&a,int){Increasetemp(a);a.value++;returntemp;}voidmain(){Increasea,b,n;for(inti=0;i<10;i++)a=n++;cout<<"n=";n.display();cout<<"a=";a.display();for(i=0;i<10;i++)b=++n;cout<<"n=";n.display();cout<<"b=";b.display();}6.3.1重載++與--2023/1/1314編輯課件7.3.2重載賦值運(yùn)算符賦值運(yùn)算符重載用于對(duì)象數(shù)據(jù)的復(fù)制

operator=必須重載為成員函數(shù)重載函數(shù)原型為:

類(lèi)型

&類(lèi)名

::operator=(const類(lèi)名

&);

6.3.2重載賦值運(yùn)算符2023/1/1315編輯課件#include<iostream.h>#include<string.h>className{public:Name(char*pN);Name(constName&); //復(fù)制構(gòu)造函數(shù)

Name&operator=(constName&); //重載賦值算符

~Name();protected:char*pName;intsize;};voidmain(){NameObj1("ZhangSan");NameObj2=Obj1; //調(diào)用復(fù)制構(gòu)造函數(shù)

NameObj3("NoName");Obj3=Obj2=Obj1; //調(diào)用重載賦值算符函數(shù)

}定義Name類(lèi)的重載賦值函數(shù)6.3.2重載賦值運(yùn)算符2023/1/1316編輯課件#include<iostream.h>#include<string.h>className{public:Name(char*pN);

Name(constName&);

//復(fù)制構(gòu)造函數(shù)

Name&operator=(constName&); //重載賦值算符

~Name();protected:char*pName;intsize;};voidmain(){NameObj1("ZhangSan");

NameObj2=Obj1;

//調(diào)用復(fù)制構(gòu)造函數(shù)

NameObj3("NoName");Obj3=Obj2=Obj1; //調(diào)用重載賦值算符函數(shù)

}定義Name類(lèi)的重載賦值函數(shù)初始化對(duì)象時(shí)調(diào)用復(fù)制構(gòu)造函數(shù)6.3.2重載賦值運(yùn)算符2023/1/1317編輯課件#include<iostream.h>#include<string.h>className{public:Name(char*pN);Name(constName&); //復(fù)制構(gòu)造函數(shù)

Name&operator=(constName&);

//重載賦值算符

~Name();protected:char*pName;intsize;};voidmain(){NameObj1("ZhangSan");NameObj2=Obj1; //調(diào)用復(fù)制構(gòu)造函數(shù)

NameObj3("NoName");

Obj3=Obj2=Obj1;

//調(diào)用重載賦值算符函數(shù)

}定義Name類(lèi)的重載賦值函數(shù)修改對(duì)象時(shí)調(diào)用重載賦值運(yùn)算符函數(shù)6.3.2重載賦值運(yùn)算符2023/1/1318編輯課件定義Name類(lèi)的重載賦值函數(shù)Name::Name(char*pN){cout<<"Constructing"<<pN<<endl;pName=newchar[strlen(pN)+1];if(pName!=0)strcpy(pName,pN);size=strlen(pN);}Name::Name(constName&Obj) //復(fù)制構(gòu)造函數(shù){cout<<"Copying"<<Obj.pName<<"intoitsownblock\n";pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;}Name&Name::operator=(constName&Obj) //重載賦值算符{delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}Name::~Name(){cout<<"Destructing"<<pName<<endl;pName[0]='\0';delete[]pName;size=0;}Name&Name::operator=(constName&Obj){delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}ChengMingthis10this->sizeLiHaiObj6Obj.size6.3.2重載賦值運(yùn)算符2023/1/1319編輯課件Name::Name(char*pN){cout<<"Constructing"<<pN<<endl;pName=newchar[strlen(pN)+1];if(pName!=0)strcpy(pName,pN);size=strlen(pN);}Name::Name(constName&Obj) //復(fù)制構(gòu)造函數(shù){cout<<"Copying"<<Obj.pName<<"intoitsownblock\n";pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;}Name&Name::operator=(constName&Obj) //重載賦值算符{delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}Name::~Name(){cout<<"Destructing"<<pName<<endl;pName[0]='\0';delete[]pName;size=0;}定義Name類(lèi)的重載賦值函數(shù)Name&Name::operator=(constName&Obj){delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}ChengMingthisLiHaiObj10this->size6Obj.size6.3.2重載賦值運(yùn)算符2023/1/1320編輯課件Name::Name(char*pN){cout<<"Constructing"<<pN<<endl;pName=newchar[strlen(pN)+1];if(pName!=0)strcpy(pName,pN);size=strlen(pN);}Name::Name(constName&Obj) //復(fù)制構(gòu)造函數(shù){cout<<"Copying"<<Obj.pName<<"intoitsownblock\n";pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;}Name&Name::operator=(constName&Obj) //重載賦值算符{delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}Name::~Name(){cout<<"Destructing"<<pName<<endl;pName[0]='\0';delete[]pName;size=0;}定義Name類(lèi)的重載賦值函數(shù)Name&Name::operator=(constName&Obj){delete[]pName;

pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}thisLiHaiObj10this->size6Obj.size6.3.2重載賦值運(yùn)算符2023/1/1321編輯課件Name::Name(char*pN){cout<<"Constructing"<<pN<<endl;pName=newchar[strlen(pN)+1];if(pName!=0)strcpy(pName,pN);size=strlen(pN);}Name::Name(constName&Obj) //復(fù)制構(gòu)造函數(shù){cout<<"Copying"<<Obj.pName<<"intoitsownblock\n";pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;}Name&Name::operator=(constName&Obj) //重載賦值算符{delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}Name::~Name(){cout<<"Destructing"<<pName<<endl;pName[0]='\0';delete[]pName;size=0;}定義Name類(lèi)的重載賦值函數(shù)Name&Name::operator=(constName&Obj){delete[]pName;pName=newchar[strlen(Obj.pName)+1];

if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}thisLiHaiObjLiHai10this->size6Obj.size6.3.2重載賦值運(yùn)算符2023/1/1322編輯課件Name::Name(char*pN){cout<<"Constructing"<<pN<<endl;pName=newchar[strlen(pN)+1];if(pName!=0)strcpy(pName,pN);size=strlen(pN);}Name::Name(constName&Obj) //復(fù)制構(gòu)造函數(shù){cout<<"Copying"<<Obj.pName<<"intoitsownblock\n";pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;}Name&Name::operator=(constName&Obj) //重載賦值算符{delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}Name::~Name(){cout<<"Destructing"<<pName<<endl;pName[0]='\0';delete[]pName;size=0;}定義Name類(lèi)的重載賦值函數(shù)Name&Name::operator=(constName&Obj){delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);

size=Obj.size;return*this;}thisLiHaiObjLiHai10this->size6Obj.size66.3.2重載賦值運(yùn)算符2023/1/1323編輯課件Name::Name(char*pN){cout<<"Constructing"<<pN<<endl;pName=newchar[strlen(pN)+1];if(pName!=0)strcpy(pName,pN);size=strlen(pN);}Name::Name(constName&Obj) //復(fù)制構(gòu)造函數(shù){cout<<"Copying"<<Obj.pName<<"intoitsownblock\n";pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;}Name&Name::operator=(constName&Obj) //重載賦值算符{delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;return*this;}Name::~Name(){cout<<"Destructing"<<pName<<endl;pName[0]='\0';delete[]pName;size=0;}定義Name類(lèi)的重載賦值函數(shù)Name&Name::operator=(constName&Obj){delete[]pName;pName=newchar[strlen(Obj.pName)+1];if(pName!=0)strcpy(pName,Obj.pName);size=Obj.size;

return*this;}thisLiHaiObjLiHai6this->size6Obj.size6.3.2重載賦值運(yùn)算符2023/1/1324編輯課件#include<iostream.h>#include<string.h>className{public:Name(char*pN);Name(constName&); //復(fù)制構(gòu)造函數(shù)

Name&operator=(constName&); //重載賦值算符

~Name();protected:char*pName;intsize;};voidmain(){NameObj1("ZhangSan");NameObj2=Obj1; //調(diào)用復(fù)制構(gòu)造函數(shù)

NameObj3("NoName");Obj3=Obj2=Obj1; //調(diào)用重載賦值算符函數(shù)

}定義Name類(lèi)的重載賦值函數(shù)6.3.2重載賦值運(yùn)算符2023/1/1325編輯課件#include<iostream.h>#include<string.h>className{public:Name(char*pN);Name(constName&); //復(fù)制構(gòu)造函數(shù)

Name&operator=(constName&); //重載賦值算符

~Name();protected:char*pName;intsize;};voidmain(){NameObj1("ZhangSan");NameObj2=Obj1; //調(diào)用復(fù)制構(gòu)造函數(shù)

NameObj3("NoName");Obj3=Obj2=Obj1; //調(diào)用重載賦值算符函數(shù)

}定義Name類(lèi)的重載賦值函數(shù)構(gòu)造對(duì)象6.3.2重載賦值運(yùn)算符2023/1/1326編輯課件#include<iostream.h>#include<string.h>className{public:Name(char*pN);Name(constName&); //復(fù)制構(gòu)造函數(shù)

Name&operator=(constName&); //重載賦值算符

~Name();protected:char*pName;intsize;};voidmain(){NameObj1("ZhangSan");

NameObj2=Obj1;

//調(diào)用復(fù)制構(gòu)造函數(shù)

NameObj3("NoName");Obj3=Obj2=Obj1; //調(diào)用重載賦值算符函數(shù)

}定義Name類(lèi)的重載賦值函數(shù)復(fù)制構(gòu)造對(duì)象6.3.2重載賦值運(yùn)算符2023/1/1327編輯課件#include<iostream.h>#include<string.h>className{public:Name(char*pN);Name(constName&); //復(fù)制構(gòu)造函數(shù)

Name&operator=(constName&); //重載賦值算符

~Name();protected:char*pName;intsize;};voidmain(){NameObj1("ZhangSan");NameObj2=Obj1; //調(diào)用復(fù)制構(gòu)造函數(shù)

NameObj3("NoName");Obj3=Obj2=Obj1; //調(diào)用重載賦值算符函數(shù)

}定義Name類(lèi)的重載賦值函數(shù)構(gòu)造對(duì)象6.3.2重載賦值運(yùn)算符2023/1/1328編輯課件#include<iostream.h>#include<string.h>className{public:Name(char*pN);Name(constName&); //復(fù)制構(gòu)造函數(shù)

Name&operator=(constName&); //重載賦值算符

~Name();protected:char*pName;intsize;};voidmain(){NameObj1("ZhangSan");NameObj2=Obj1; //調(diào)用復(fù)制構(gòu)造函數(shù)

NameObj3("NoName");

Obj3=Obj2=Obj1;

//調(diào)用重載賦值算符函數(shù)

}定義Name類(lèi)的重載賦值函數(shù)析構(gòu)賦值操作后的對(duì)象6.3.2重載賦值運(yùn)算符2023/1/1329編輯課件7.3.3重載運(yùn)算符[]和()運(yùn)算符[]和()是二元運(yùn)算符

[]和()只能用成員函數(shù)重載,不能用友元函數(shù)重載6.3.3重載運(yùn)算符[]和()2023/1/1330編輯課件1.重載下標(biāo)算符[]

重載格式

類(lèi)

::類(lèi)型

operator[](類(lèi)型

);[]運(yùn)算符用于訪(fǎng)問(wèn)數(shù)據(jù)對(duì)象的元素6.3.3重載運(yùn)算符[]和()2023/1/1331編輯課件1.重載下標(biāo)算符[]

重載格式

類(lèi)

::類(lèi)型

operator[](類(lèi)型

);[]運(yùn)算符用于訪(fǎng)問(wèn)數(shù)據(jù)對(duì)象的元素定義重載函數(shù)的類(lèi)名6.3.3重載運(yùn)算符[]和()2023/1/1332編輯課件1.重載下標(biāo)算符[]

重載格式

類(lèi)

::類(lèi)型

operator[][類(lèi)型

];[]運(yùn)算符用于訪(fǎng)問(wèn)數(shù)據(jù)對(duì)象的元素函數(shù)返回類(lèi)型6.3.3重載運(yùn)算符[]和()2023/1/1333編輯課件1.重載下標(biāo)算符[]

重載格式

類(lèi)

::類(lèi)型

operator[](類(lèi)型

);[]運(yùn)算符用于訪(fǎng)問(wèn)數(shù)據(jù)對(duì)象的元素函數(shù)名6.3.3重載運(yùn)算符[]和()2023/1/1334編輯課件1.重載下標(biāo)算符[]

重載格式

類(lèi)

::類(lèi)型

operator[](類(lèi)型

);[]運(yùn)算符用于訪(fǎng)問(wèn)數(shù)據(jù)對(duì)象的元素右操作數(shù)為符合原語(yǔ)義,用int6.3.3重載運(yùn)算符[]和()2023/1/1335編輯課件1.重載下標(biāo)算符[]

重載格式

類(lèi)

::類(lèi)型

operator()[類(lèi)型

];[]運(yùn)算符用于訪(fǎng)問(wèn)數(shù)據(jù)對(duì)象的元素例 設(shè)x是類(lèi)X的一個(gè)對(duì)象,則表達(dá)式

x[y]

可被解釋為

x.operator[](y)顯式聲明一個(gè)參數(shù)6.3.3重載運(yùn)算符[]和()2023/1/1336編輯課件2.重載函數(shù)調(diào)用符()

//用重載()算符實(shí)現(xiàn)數(shù)學(xué)函數(shù)的抽象#include<iostream.h>classF{public:doubleoperator()(doublex,doubley);};doubleF::operator()(doublex,doubley){returnx*x+y*y;}voidmain() {Ff;cout<<f(5.2,2.5)<<endl;}6.3.3重載運(yùn)算符[]和()2023/1/1337編輯課件2.重載函數(shù)調(diào)用符()

//用重載()算符實(shí)現(xiàn)數(shù)學(xué)函數(shù)的抽象#include<iostream.h>classF{public:doubleoperator()(doublex,doubley);};doubleF::operator()(doublex,doubley){returnx*x+y*y;}voidmain() {Ff;cout<<f(5.2,2.5)<<endl;}f.operator()(5.2,2.5)6.3.3重載運(yùn)算符[]和()2023/1/1338編輯課件2.重載函數(shù)調(diào)用符()

//用重載()算符實(shí)現(xiàn)數(shù)學(xué)函數(shù)的抽象#include<iostream.h>classF{public:doubleoperator()(doublex,doubley);};doubleF::operator()(doublex,doubley){returnx*x+y*y;}voidmain() {Ff;cout<<f(5.2,2.5)<<endl;}memFunmemFunf.memFun(5.2,2.5)

比較定義普通成員函數(shù)6.3.3重載運(yùn)算符[]和()2023/1/1339編輯課件istream

和ostream

是C++的預(yù)定義流類(lèi)cin

是istream

的對(duì)象,cout

是ostream

的對(duì)象運(yùn)算符<<由ostream

重載為插入操作,用于輸出基本類(lèi)型數(shù)據(jù)運(yùn)算符>>由istream

重載為提取操作,用于輸入基本類(lèi)型數(shù)據(jù)程序員重載<<和>>,用于輸出和輸入用戶(hù)自定義的數(shù)據(jù)類(lèi)型7.3.4重載流插入和流提取運(yùn)算符6.3.4重載流插入和流提取運(yùn)算符2023/1/1340編輯課件#include<iostream.h>#include<stdlib.h>classvector{public:vector(intsize=1);~vector();int&operator[](inti);friendostream&operator<<(ostream&output,vector&);friendistream&operator>>(istream&input,vector&);private:int*v;intlen;};voidmain(){intk;cout<<"InputthelengthofvectorA:\n";cin>>k;vectorA(k);cout<<"InputtheelementsofvectorA:\n";cin>>A;cout<<"OutputtheelementsofvectorA:\n";cout<<A;}為vector類(lèi)重載流插入運(yùn)算符和提取運(yùn)算符6.3.4重載流插入和流提取運(yùn)算符2023/1/1341編輯課件#include<iostream.h>#include<stdlib.h>classvector{public:vector(intsize=1);~vector();int&operator[](inti);friendostream&operator<<(ostream&output,vector&);friendistream&operator>>(istream&input,vector&);private:int*v;intlen;};voidmain(){intk;cout<<"InputthelengthofvectorA:\n";cin>>k;vectorA(k);cout<<"InputtheelementsofvectorA:\n";cin>>A;cout<<"OutputtheelementsofvectorA:\n";cout<<A;}為vector類(lèi)重載流插入運(yùn)算符和提取運(yùn)算符重載幾個(gè)運(yùn)算符6.3.4重載流插入和流提取運(yùn)算符2023/1/1342編輯課件#include<iostream.h>#include<stdlib.h>classvector{public:vector(intsize=1);~vector();int&operator[](inti);friendostream&operator<<(ostream&output,vector&);friendistream&operator>>(istream&input,vector&);private:int*v;intlen;};voidmain(){intk;cout<<"InputthelengthofvectorA:\n";cin>>k;vectorA(k);cout<<"InputtheelementsofvectorA:\n";cin>>A;cout<<"OutputtheelementsofvectorA:\n";cout<<A;}為vector類(lèi)重載流插入運(yùn)算符和提取運(yùn)算符標(biāo)準(zhǔn)流類(lèi)6.3.4重載流插入和流提取運(yùn)算符2023/1/1343編輯課件#include<iostream.h>#include<stdlib.h>classvector{public:vector(intsize=1);~vector();int&operator[](inti);friendostream&operator<<(ostream&output,vector&);friendistream&operator>>(istream&input,vector&);private:int*v;intlen;};voidmain(){intk;cout<<"InputthelengthofvectorA:\n";cin>>k;vectorA(k);cout<<"InputtheelementsofvectorA:\n";cin>>A;cout<<"OutputtheelementsofvectorA:\n";cout<<A;}為vector類(lèi)重載流插入運(yùn)算符和提取運(yùn)算符使用預(yù)定義版本6.3.4重載流插入和流提取運(yùn)算符2023/1/1344編輯課件#include<iostream.h>#include<stdlib.h>classvector{public:vector(intsize=1);~vector();int&operator[](inti);friendostream&operator<<(ostream&output,vector&);friendistream&operator>>(istream&input,vector&);private:int*v;intlen;};voidmain(){intk;cout<<"InputthelengthofvectorA:\n";cin>>k;

vectorA(k);cout<<"InputtheelementsofvectorA:\n";cin>>A;cout<<"OutputtheelementsofvectorA:\n";

cout<<A;}為vector類(lèi)重載流插入運(yùn)算符和提取運(yùn)算符使用重載版本cin.operator>>(A)6.3.4重載流插入和流提取運(yùn)算符2023/1/1345編輯課件#include<iostream.h>#include<stdlib.h>classvector{public:vector(intsize=1);~vector();int&operator[](inti);friendostream&operator<<(ostream&output,vector&);friendistream&operator>>(istream&input,vector&);private:int*v;intlen;};voidmain(){intk;cout<<"InputthelengthofvectorA:\n";cin>>k;

vectorA(k);cout<<"InputtheelementsofvectorA:\n";cin>>A;cout<<"OutputtheelementsofvectorA:\n";

cout<<A;}為vector類(lèi)重載流插入運(yùn)算符和提取運(yùn)算符使用重載版本cout.operator<<(A)6.3.4重載流插入和流提取運(yùn)算符2023/1/1346編輯課件vector::vector(intsize){if(size<=0||size>100){cout<<"Thesizeof"<<size<<"isnull!\n";abort();

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論