注:參考東南大學《C++程序設計教程》視頻,主講老師:何潔月。此內容爲自學時所記筆記
多態性:指發出同樣的消息被不同類型的對象接受時導致完全不同的行爲。
消息:主要指對類的成員函數的調用
實現:函數重載------靜態
運算符重載----靜 ---編譯時
虛函數--------動態---運行時
聯編:計算機程序自身彼此關聯的過程
運算符重載:對已有的運算符賦予多重含義
實現機制:運算表達式->運算符函數的調用,運算符選擇遵循函數重載的選擇原則。
規則:
只能重載,不改變原運算符的優先級和結合性。
不改變操作數個數,只能根據類型判別。
聲明形式:
函數類型 operator 運算符(形參)
{
......
}
(1)類成員函數
(2)友元函數。
雙目運算符
#includeusing namespace std;
class complex
{
public :
complex(){}
complex(double r,double i);
complex operator +(complex c);
complex operator -(complex c);
void print();
private:
double real,image;
};
complex::complex(double r,double i){
real=r;
image=i;
}
complex complex::operator+(complex c){
complex cc;
cc.image=c.image+image; //image real 省略了this.
cc.real=c.real+real; //this指向調用對象的地址
return complex(cc.real,cc.image);
}
void complex::print(){
cout<<real<<"+i*"<<image<<endl;
}
int main()
{
complex c1(1,2);
complex c2(3,4);
complex c3;
c3=c1+c2;
c1.print();
c2.print();
c3.print();
return 0;
}
單目運算符:
oprd++ void operator ++ (int );
++oprd void operator ++ ();
#includeusing namespace std;
class complex
{
public :
complex(){}
complex(double r,double i);
complex operator +(complex c);
void operator ++(); //無返回值
void operator ++(int ); //後加加
complex operator -(complex c);
void print();
private:
double real,image;
};
complex::complex(double r,double i){
real=r;
image=i;
}
complex complex::operator+(complex c){
complex cc;
cc.image=c.image+image; //image real 省略了this.
cc.real=c.real+real; //this指向調用對象的地址
return complex(cc.real,cc.image);
}
void complex::operator ++(){
real++;
image++;
}
void complex::operator ++(int){
real++;
image++;
cout<<"調用的是a++"<<endl;
}
void complex::print(){
cout<<real<<"+i*"<<image<<endl;
}
int main()
{
complex c3(1,2);
c3.print();
++c3;
c3.print();
c3++;
c3.print();
return 0;
}
運算符友元函數的設計
對於外部函數,可以操作對象的私有成員,要將其重載爲該類的友元函數即可
形參爲從左之右的各操作數,主函數不變
友元函數的作用:破壞私有性
#includeusing namespace std;
class complex
{
public :
complex(){}
complex(double r,double i);
friend complex operator +(complex c1,complex c2);
friend complex operator -(complex c1,complex c2);
void print();
private:
double real,image;
};
complex::complex(double r,double i){
real=r;
image=i;
}
complex operator +(complex c1,complex c2){ //直接的函數,注意區別
//友元函數,可以直接使c1,c2的私有成員
return complex(c1.real+c2.real,c1.image+c2.image);
}
void complex::print(){
cout<<real<<"+i*"<<image<<endl;
}
int main()
{
complex c1(1,2);
complex c2(3,4);
complex c3;
c3=c1+c2;
c1.print();
c2.print();
c3.print();
return 0;
}
靜態聯編:編譯階段就已經確定操作調用
動態聯編:程序運行時確定要調用的函數
靜態:
#includeusing namespace std;
class Point
{
public :
Point(float x,float y){X=x;Y=y;}
void Move() const {cout<<"Point"<<endl;}
private :
float X,Y;
};
class Rectangle:public Point
{
public :
Rectangle(float x,float y,float w,float h);
void Move() const {cout<<"Rectangle"<<endl;}
private:
float W,H;
};
Rectangle::Rectangle(float x,float y,float w,float h):Point(x,y){
W=w;H=h;
}
void fun(Point &p){ //編譯時系統直接將p.move調用Point的函數
p.Move();
}
int main()
{
Rectangle r(1,2,3,4);
fun(r);
return 0;
}
動態:
只要在函數之前加上virtual
虛函數:非靜態的成員函數
原型之間寫virtual,virtual,只用來說明原型,不能用在函數實現
基類聲明後,派生類自動爲虛函數
調用方式:基類指針或引用,執行時根據指針指向的對象的類決定調用
8.4抽象類
帶有純虛函數的類稱爲抽象類
抽象類不能聲明對象。
class 類名
{
virtual 類型 函數名(參數表)=0; //虛函數
...
}
抽象類爲抽象和設計的目的而建立
成員函數沒有具體實現,在派生類具體實現
只能作爲基類實現
抽象類不可以聲明對象,可以聲明指針
#includeusing namespace std;
class Point
{
public :
Point (){cout<<"Point"<<endl;}
virtual ~Point(){cout<<"xigou Point "<<endl;}
virtual void Move() const {cout<<"Point move"<<endl;} //純虛函數
private :
};
class Rectangle:public Point
{
public :
Rectangle(){cout<<"Rectangle"<<endl;}
virtual ~Rectangle(){cout<<"xigou Rectangle"<<endl;}
virtual void Move() const {cout<<"Rectangle move"<<endl;}
private:
};
void fun(Point &p){ //編譯時系統直接將p.move調用Point的函數
p.Move();
}
void test(Point *p){ //動態析構
delete p;
}
int main()
{
//Rectangle r;
//fun(r);
Rectangle *p=new Rectangle;
test(p);
return 0;
}
開發實例
沿用繼承那一章的例子,我們對employee基類變爲抽象類