您好,登錄后才能下訂單哦!
1、友元函數(shù)
在類中一般的成員函數(shù)具備:
(1)、該函數(shù)能訪問(wèn)類中的私有部分
(2)、該函數(shù)位于類的作用域之中
(3)、該函數(shù)必須經(jīng)由一個(gè)對(duì)象去激活,也就是說(shuō)有一個(gè)this指針;
友元函數(shù)不同之處:
(1)、在#include<iostream> using namespace std;的前提下,必須的類外先聲明;
class Test; void fun(); //有元函數(shù)的聲明
但是在#include<iostream.h>中不用這個(gè)有元函數(shù)的聲明;這是早期頭文件的包含。
(2)、在類內(nèi)必須聲明是朋友,friend(關(guān)鍵字),函數(shù)聲明
class Test{ friend void fun(); //朋友沒有公有,私有,保護(hù)之分,所以限定修飾符在這不起任何作用 };
(3)、在類外實(shí)現(xiàn)方法的定義,此時(shí)不用再加friend了;
void fun(){ .......... }
有元函數(shù)具有的特性:該函數(shù)能訪問(wèn)類的私有數(shù)據(jù);不是該類的成員函數(shù),也不需要對(duì)象驅(qū)動(dòng);
還有友元類,跟其使用一樣,類中所有函數(shù)均可調(diào)用其朋友的私有數(shù)據(jù);
2、運(yùn)算符的重載(個(gè)人認(rèn)為C++中運(yùn)算符的重載很有用,也很重要)
相當(dāng)于函數(shù)的調(diào)用,對(duì)其的另一種更加人文化的解釋,并告訴 C++編譯器,遇到該重載運(yùn)算符時(shí)調(diào)用此函數(shù)。
定義運(yùn)算符重載的一般格式:
返回值類型 類名::operator重載的運(yùn)算符(參數(shù)列表);
假如出現(xiàn)復(fù)數(shù)類:
Complex{ public: ..... private: int real; int p_w_picpath; };
//主函數(shù)中: Complex t1(1, 2); Complex t2(2, 3); Complex t3; t3 = t1 + t2;
此時(shí)t1和t2的類型不定,就得對(duì)+進(jìn)行運(yùn)算符的重載;使其更符合人文化,其本質(zhì)不變,t1.operator+(t2);
對(duì)象的類型不定,編譯器不知道怎么輸出,自己就得對(duì)<<和>>運(yùn)算符進(jìn)行重載,以達(dá)到輸出對(duì)象的目的。還必須是友元函數(shù);
class Complex; //這是第一步,類外聲明。 ostream& operator<<(ostream &out, const Complex &c); istream& operator>>(istream &in, Complex &c); class Complex{ //這是第二步,對(duì)其在類內(nèi)說(shuō)明是朋友。 friend ostream& operator<<(ostream &out, const Complex &c); friend istream& operator>>(istream &in, Complex &c); }; ostream& operator<<(ostream &out, const Complex &c){ out<<"("<<c.m_real<<","<<c.m_imag<<")"; return out; //這是第三步,在類外進(jìn)行定義。 } istream& operator>>(istream &in, Complex &c){ in>>c.m_real>>c.m_imag; return in; }
關(guān)于這個(gè)<<對(duì)應(yīng)一個(gè)要輸出的值,在定義時(shí),不用endl,回車。
對(duì)于Complate類的補(bǔ)充,t1 = t2 + 10; //成員方法重載即可。內(nèi)部有一個(gè)隱藏參數(shù)
t1 = 10 + t2; //沒有對(duì)象驅(qū)動(dòng),還要10.operator+(t2);是錯(cuò)的,所以此時(shí)用友元函數(shù),傳兩個(gè)參數(shù)就是有兩個(gè)參數(shù)。
運(yùn)算符的重載要注意:
(1)、operator后面是一個(gè)合法的運(yùn)算符,將右操作數(shù)做為函數(shù)的實(shí)參;
(2)、++ --前后的問(wèn)題,用函數(shù)重載區(qū)分,后加的(int)參數(shù);
(3)、?: . .* :: sizeof不能重載,其它的應(yīng)該都可以進(jìn)行重載;
例1:++i 和 i++有什么區(qū)別?
對(duì)其進(jìn)行重載,如下:
#include<iostream> using namespace std; class Int; ostream& operator<<(ostream &out, const Int &s); class Int{ friend ostream& operator<<(ostream &out, const Int &s); public: Int(int m = 0) : t(m){} ~Int(){} public: Int& operator++()//++t 先加1,再將此對(duì)象的引用返回 { t++; return *this; } Int operator++(int) //t++ 后加的要有一個(gè)參數(shù)int區(qū)分, { Int tmp(t); //創(chuàng)建一個(gè)臨時(shí)對(duì)象, t++; //原有對(duì)象加了一下, return tmp; //返回臨時(shí)對(duì)象,后加。 } private: int t; }; ostream& operator<<(ostream &out, const Int &s){ out<<s.t; return out; } int main(void){ Int t1(6); Int t2; Int t3; t2 = ++t1; cout<<"t1 = "<<t1<<", t2 = "<<t2<<endl; //對(duì)輸出運(yùn)算符重載了,才能輸出對(duì)象。 t3 = t1++; cout<<"t1 = "<<t1<<", t3 = "<<t3<<endl; return 0; }
運(yùn)行結(jié)果如下:
從結(jié)果可以看出,確是實(shí)現(xiàn)了,這塊的本質(zhì)是運(yùn)算符對(duì)++的重載。
前加,返回的是引用,后加還得調(diào)用構(gòu)造函數(shù)和拷貝構(gòu)造;所以前加++i;效率更高,運(yùn)行速度也更快。
例2、String類
#include<iostream> #include<string.h> #include<assert.h> using namespace std; class String; ostream& operator<<(ostream &out, const String &str); class String{ friend ostream& operator<<(ostream &out, const String &str); public: String(const char *str = ""){ if(str == NULL){ data = new char[1]; data[0] = 0; }else{ data = new char[strlen(str) + 1]; strcpy(data, str); } } //String(const String &str){} String& operator=(const String &str){ if(this != &str){ delete []data; data = new char[strlen(str.data) + 1]; strcpy(data, str.data); } return *this; } ~String(){ delete []data; } public: String operator+(const String &str){ char *tmp; tmp = new char[strlen(data) + strlen(str.data) + 1]; strcpy(tmp, data); strcat(tmp, str.data); return String(tmp); } void operator+=(const String &str){ char *new_data = new char[strlen(data) + strlen(str.data) + 1]; strcpy(new_data, data); strcat(new_data, str.data); delete []data; data = new_data; /* realloc(data, strlen(data) + strlen(str.data) + 1); //此時(shí)對(duì)原有的數(shù)組空間進(jìn)行動(dòng)態(tài)擴(kuò)長(zhǎng), // 給誰(shuí)擴(kuò)長(zhǎng) 加上原先,一共要有多長(zhǎng) 即原有值不變。 strcat(data, str.data); */ } char operator[](int pos){ assert(pos >= 0 && pos < strlen(data)); return data[pos]; } char operator*(){ return data[0]; } bool operator==(const String &str){ return strcmp(data, str.data) ? false : true; } bool operator!=(const String &str){ return !(*this == str); //調(diào)用==這個(gè)編寫過(guò)的函數(shù) } private: char *data; }; ostream& operator<<(ostream &out, const String &str){ out<<str.data; return out; } int main(void){ String s1("hello"); String s2("world"); String s3; s3 = s1 + s2; cout<<s3<<endl; s1 += s2; cout<<s1<<endl; cout<<s1[1]<<endl; cout<<*s1<<endl; if(s1 == s3){ cout<<"相等"<<endl; }else{ cout<<"不相等"<<endl; } if(s1 != s3){ cout<<"不相等"<<endl; }else{ cout<<"相等"<<endl; } return 0; }
對(duì)其+,+=,[],*,==,!=進(jìn)行了運(yùn)算符的重載,結(jié)果如下:
3、const和static
在C++中const是非常重要的,現(xiàn)在說(shuō)明常函數(shù)之間的關(guān)系:普通函數(shù)可以調(diào)用常函數(shù),常函數(shù)不能調(diào)用普通函數(shù)(因?yàn)闆]有this);
void fun()const{}
void fun(){} //一個(gè)有const,一個(gè)沒有,參數(shù)類型不一樣,所以這是函數(shù)的重載!
static在C++中有成員和方法,靜態(tài)成員的在類外初始化;類的靜態(tài)成員為所有對(duì)象所共享。
靜態(tài)方法: (1)、可以調(diào)用該類的私有成員,(2)、應(yīng)該位于類的作用域之中;(3)、類名直接調(diào)用,可以不用方法;
靜態(tài)方法只能調(diào)用靜態(tài)成員,和靜態(tài)函數(shù);非靜態(tài)的可以調(diào)用靜態(tài)成員或方法。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。