C++程序设计教程第7版-2201-2420_第1页
C++程序设计教程第7版-2201-2420_第2页
C++程序设计教程第7版-2201-2420_第3页
C++程序设计教程第7版-2201-2420_第4页
C++程序设计教程第7版-2201-2420_第5页
已阅读5页,还剩215页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

int

Goods::total_weight=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods

*p=new

Goods(WD);p->next

=NIIII.if(f==NU

Goods::Goods(int

w){weight

=w;total_weight+=w;}else{r->next=p;r=r->next;}

调用构造函数累加货物重量rear

r98

1

70

31200total_weight549#include<iostream>using

nclass

G{publiGoC

front~GintstatiGooprivateintweight;{if(f==NULL){cout<<"No

any

Goods

*q=

f;f=f->

next;

delete

cout<<"saled.\n";华南理工大学计算机学院周霭如2025goods!\n";return;}q;50200例6-21int

Goods:total_weight=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods*p=newGoods(w);p->next

=NULL;if(f==NULL)f=r=p;else{r->next=p;r=r->next;}{if(f==NULL){cout<<"No

any

goods!\n";return;}

Goods

*q=

f;f=f->

next;

delete

q;#include<iostream>using

nclass

G{publiGoC

front~GintstatiGooprivateintweight;rear

r98

1

70

31total_weight例6-21200cout<<"saled.\n";华南理工大学计算机学院周霭如202550200int

Goods:total_weight=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods*p=newGoods(w);p->next

=NULL;if(f==NULL)f=r=p;else{r->next=p;r=r->next;}{if(f==NULL){cout<<"No

any

goods!\n";return;}

Goods

*q=

f;f=f->

next;

delete

q;#include<iostream>using

nclass

G{publiGoC

front~GintstatiGooprivateintweight;rear

r98

1

70

31total_weightcout<<"saled.\n";华南理工大学计算机学院周霭如202550200例6-21int

Goods:total_weight=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods*p=newGoods(w);p->next=NULL;if(f==NULL)f=r=p;else{

r->next=p;r=r->

next;}{if(f==NULL){cout<<"No

any

goods!\n";return;}

Goods

*q=

f;f=f->

next;

delete

q;#include<iostream>using

nclass

G{publiGoC

front~GintstatiGooprivateintweight;rear

r98

1

70

31total_weight

549例6-21200cout<<"saled.\n";华南理工大学计算机学院周霭如202550200int

Goods:total_weight=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods*p=newGoods(w);p->next=NULL;if(f==NULL)f=r=p;else{

r->next=p;r=r->

next;}{if(f==NULL){cout<<"No

any

goods!\n";return;}

Goods

*q=

f;f=f->

next;

delete

q;#include<iostream>using

n

例6-21class

G{publiGoC

front~GintstatiGooprivateintweight;98

1

70total_weight

549cout<<"saled.\n";华南理工大学计算机学院周霭如20255020020031int

Goods:total_weight=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods*p=newGoods(w);p->next=NULL;if(f==NULL)f=r=p;else{

r->next=p;r=r->

next;}{if(f==NULL){cout<<"No

any

goods!\n";return;}

Goods

*q=

f;f=f->

next;

delete

q;#include<iostream>using

n

例6-21class

G{publiGoC

front~GintstatiGooprivateintweight;98

170total_weight

549cout<<"saled.\n";华南理工大学计算机学院周霭如2025rear

r2005020031static

int

total_weight;};int

Goods:total_weight

=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods

*p=new

Goods(w);p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"No

any

goods!\n";return;}Goods*q=f;f=f->next;delete

q;cout<<"saled.\n";class

G{publiGocG

frontintstatiGooprivatint

weight;98

170total_weightL

549#include<iostream>using

n华南理工大学计算机学院周蔼如202531

200例6-2150~static

int

total_weight;};int

Goods:total_weight

=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods

*p=new

Goods(w);p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"No

any

goods!\n";return;}

Goods*q=f;f=f->next;delete

q;cout<<"saled.\n";class

G{publiGocG

frontintstatiGooprivatint

weight;98

170total_weightL

549#include<iostream>using

n华南理工大学计算机学院周蔼如202531

200例6-2150~class

G{publi

9GocG

front

50

98

170intstatiGoo

total_weight

549privatint

weight;static

int

total_weight;};int

Goods:total_weight

=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods

*p=new

Goods(w);p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"No

any

goods!\n";return;}Goods*q=f;f=f->next;delete

q;cout<<"saled.\n";#include<iostream>using

n华南理工大学计算机学院周蔼如202531

200例6-21~class

G{publi

9GocG

front

50

98

170intstatiGoo

total_weight

549privatint

weight;static

int

total_weight;};int

Goods:total_weight

=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods

*p=new

Goods(w);p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"No

any

goods!\n";return;}Goods*q=f;f=f->next;delete

q;cout<<"saled.\n";#include<iostream>using

n华南理工大学计算机学院周蔼如202531

200例6-21~class

G{publi

9GocG

front

50

98

170

31200intstatiGoo

total_weight

549privatint

weight;static

int

total_weight;};int

Goods:total_weight

=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods

*p=new

Goods(w);p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"Noany

goods!\n";return;}Goods*q=f;f=f->next;

deleteq;cout<<"saled.\n";#include<iostream>using

n华南理工大学计算机学院周蔼如2025例6-21~class

G{publi

9GocG

front

50

98

170

31200intstatiGoo

total_weight

549privatint

weight;static

int

total_weight;};int

Goods:total_weight

=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods

*p=new

Goods(w);p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"Noany

goods!\n";return;}Goods*q=f;f=f->next;

deleteq;cout<<"saled.\n";#include<iostream>using

n华南理工大学计算机学院周蔼如2025例6-21~class

G{publi

9GocG

front

5098

170

31intstatiGoo

total_weight[

549privatintweight;static

int

total_weight;int

Goods:total_weight

=0;Goods::~Goods(){total_weight

-=weight;}p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"No

any

goods!\n";return;}Goods*q=f;f=f->next

,delete

q;cout<<"saled.\n";200调用析造函数减去货物重量#include<iostream>using

n华南理工大学计算机学院周蔼如2025例6-21~class

G{publi

9GocG

front

50

98

170

31intstatiGoo

total_weight[

499privatintweight;static

int

total_weight;int

Goods:total_weight

=0;Goods::~Goods(){total_weight

-=weight;}p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"No

any

goods!\n";return;}Goods*q=f;f=f->next

,delete

q;cout<<"saled.\n";200调用析造函数减去货物重量#include<iostream>using

n华南理工大学计算机学院周蔼如2025例6-21~class

G{publidGoc~G

front

98

170

31

200intstatiGoo

total_weight

499privatint

weight;static

int

total_weight;};int

Goods:total_weight

=0;void

purchase(Goods*&f,Goods*&r,int

w){Goods

*p=new

Goods(w);p->next=NULL;void

sale(Goods*&f,Goods*&r){if(f==NULL){cout<<"No

any

goods!\n";return;}Goods*q=f;f=f->next;delete

q;cout<<"saled.\n";#include<iostream>using

n

例6-21华南理工大学计算机学院周蔼如2025int

main()

例6-21{Goods*front=NULL,*rear=NULL;int

w;int

choice;do{cout<<"Please

choice:\n";cout<<"Keyin

1ispurchase,\nKeyin2issale,\nKey

in

0

is

over.\n";{cout<<"Input

weight:";cin>>W;purchase(front,rear,w);//从表尾插入1个结点

break;}}cout<<"Nowtotalweightis:"<<Goods::TotalWeight()<<endl;

}while(choice);//键入2,售出1箱货物

//从表头删除1个结点

//键入0,结

束case

2:{sale(front,rear);break;}

case0:break;//

操作选择//键入1,购进1箱货物cin>>choice;switch(choice){case1:华南理工大学计算机学院周霭如20256.3.3友元友元是对类操作的辅助手段。友元能够引用类中本来被隐蔽的信息使用友元目的是基于程序的运行效率运算符重载的某些场合需要使用友元友元可以是函数,也可以是类友元关系是非传递的。即Y

是X

的友元,Z

是Y的友元,但Z

不一定是X的友元华南理工大学计算机学院周霭如20251.

友元函数class

A{private:int

i;friendvoid

FriendFun(A*,int);public:void

MemberFun(int);};void

FriendFun(A*ptr,int

x){ptr->i=x;};void

A::MemberFun(int

x){i=x;};华南理工大学计算机学院周霭如2025class

Afriendvoid

FriendFun(A*,int);public:void

MemberFun(int);};void

FriendFun(A*ptr,int

x){ptr->i=x;};void

A::MemberFun(int

x){i=x;};1.

友元函数说明语句位置与访问描述无关华南理工大学计算机学院周霭如2025class

A{private:int

i;friendvoidFriendFun(A*,int);

public:void

MemberFun(int);1.

友元函数友元函数通过对象参数访问私有数据成员voidFriendFun(A*ptr,int

x){

ptr->i=x;};void

A::MemberFun(int

x){i=x;};华南理工大学计算机学院周霭如2025class

A{private:int

i;friendvoid

FriendFun(A*,int);public:void

MemberFun(int);};voidFriendFun(A*ptr,intx){ptr->i=x;X;void

A::MemberFun(int

x)1.

友元函数成员函数通过this指针在对象上操作华南理工大学计算机学院周霭如2025class

A{private:int

i;friendvoid

FriendFun(A*,int);public:void

MemberFun(int);};···voidFriendFum(A*

ptr,

intx){ptr=>i

=x;8;void

A::MemberFun(int

x){

i

=x;};1.

友元函数AAobj;FriendFun(&Aobj

,5);Aobj.MemberFun(5);华南理工大学计算机学院周霭如2025#include<iostream>

例6-22

用友元函数计算两点之间的距离using

namespace

std;#include<math.h>class

Point{public:Point(double

xi,double

yi){X=xi;Y=yi;}double

GetX(){return

X;}double

GetY({returnY;}friend

double

Distance(Point

&a,Point

&b);private:double

X,Y;};double

Distance(Point&a,Point&b){double

dx=a.X-b.X;double

dy=a.Y-b.Y;return

sqrt(dx*dx+dy*dy);}int

main(){Pointp1(3.0,5.0),p2(4.0,6.0);doubled=Distance(p1,p2);cout<<"This

distance

is"<<d<<endl;华南理工大学计算机学院周霭如2025friend

double

Distance(Point&a,Point&b);private:double

X,Y;};double

Distance(Point&a,Point&b){double

dx=a.X-b.X;double

dy=a.Y-b.Y;return

sqrt(dx*dx+dy*dy);}int

main(){Pointp1(3.0,5.0),p2(4.0,6.0);doubled=Distance(p1,p2);cout<<"This

distance

is"<<d<<endl;#include<math.h>class

Point{public:Point(double

xi,double

yi){X=xi;Y=yi

doubleGetX(){returnX;double

GetY(){retrnY;}

1#include<iostream>

例6-22

用友元函数计算两点之间的距离using

namespace

std;说明函数Distance是类Point

的友元华南理工大学计算机学院周霭如2025#include<math.h>class

Point{public:Point(double

xi,double

yi){X=xi;Y=yi;}double

GetX(){return

X;}double

GetY({returnY;}

定义函数Distancefriend

double

Distance(Point&a,Point&b);private:double

X,Y;};

AKdoubleDistance(Point&a,Point&b)1doubledx=a.-b.x;double

dy=a.Y-b.Y;retuirnsar(d*d+dy*dy)}int

main(){Pointp1(3.0,5.0),p2(4.0,6.0);doubled=Distance(p1,p2);cout<<"This

distance

is"<<d<<endl;}#include<iostream>

例6-22

用友元函数计算两点之间的距离using

namespace

std;华南理工大学计算机学院周霭如2025#include<math.h>class

Point{public:Point(double

xi,double

yi){X=xi;Y=yi;}

double

GetX(){return

X;}double

GetY({returnY;}friend

double

Distance(Point

&a,Point

&b);private:doubleX,Y;};double

Distance(Point&a,Point&b{double

dx=a.X-b.

X;return

sqrt(dx*dx+dy*dy);}int

main(){Pointp1(3.0,5.0),p2(4.0,6.0);doubled=Distance(p1,p2);cout<<"This

distance

is"<<d<<endl;

}#include<iostream>

例6-22

用友元函数计算两点之间的距离using

namespace

std;通过对象参数访问私有成员华南理工大学计算机学院周霭如2025#include<math.h>class

Point{public:Point(double

xi,double

yi){X=xi;Y=yi;}double

GetX(){return

X;}double

GetY({returnY;}friend

double

Distance(Point

&a,Point

&b);

private:double

X,Y;};double

Distance(Point&a,Point

&b){double

dx=

a.X-b.X;

调用友元函数double

dy

=a.Y-b.Y;returnsqrt(dx*dx+dy*dy);int

main(){Point

p1(3.0,5.0),p2(4.0,6.0);double

d=

Distance(p1,p2

);cout<<"This

distance

is"<<d<<endl;#include<iostream>

例6-22

用友元函数计算两点之间的距离using

namespace

std;华南理工大学计算机学院周霭如2025#include<math.h>class

Point{public:Point(double

xi,double

yi){X=xi;Y=yi;}double

GetX(){return

X;}double

GetY({returnY;}friend

double

Distance(Point

&a,Point

&b);private:double

X,Y;};double

Distance(Point&a,Point&b){double

dx=a.X-b.X;double

dy=a.Y-b.Y;CN

C:\WINDOWSsystem32\cmd.exereturn

sqrt(dx*dx+dy*dy);int

main(){Pointp1(3.0,5.0),p2(4.0,6.0);doubled=Distance(p1,p2);cout<<"This

distance

is"<<d<<endl;#include<iostream>

例6-22

用友元函数计算两点之间的距离using

namespace

std;This

distance

is

1.41421

请按任意键继续..·华南理工大学计算机学院周霭如20252.

友元类若F类是A类的友元类,则F类的所有成员函数都是A类的友元函数友元类通常设计为一种对数据操作或类之间传递消息的

辅助类华南理工大学计算机学院周霭如2025#include<iostream>using

namespace

std;class

A{friendclassB;public:void

Display({cout<<x<<endl;};private:int

x;};class

B{public:void

Set(inti){Aobject.x=i;}void

Display({Aobject.Display();}

private:A

Aobject;};int

main(){B

Bobject;Bobject.Set(100);Bobject.Display();}例6-23

演示友元类华南理工大学计算机学院周霭如2025Display(){cout<<x<<endl;};x;

类B是类A的友元Set(inti){Aobject.x=i;}Display({Aobject.Display();}private:A

Aobject;

};int

main(){B

Bobject;Bobject.Set(100);

Bobject.Display();public:voidprivate:int};class

B{public:voidvoid#include<iostream>

例6-23

演示友元类using

namespace

std;class

A{

friend

class

B;华南理工大学计算机学院周霭如2025using

namespace

std;class

A{

friend

class

B;public:void

Display({cout<<x<<endl;};private:int

x;};

类B的A类数据成员class

B{public:void

Set(int

i){Aobject.x=i;}void

Display({Aobject.Display();}private:A

Aobject;};int

main(){B

Bobject;#include<iostream>

例6-23

演示友元类Bobject.Set(100);Bobject.Display();

}华南理工大学计算机学院周霭如2025using

namespace

std;class

A{

friend

class

B;public:void

Display({cout<<x<<endl;};private:int

x;};class

B{public:void

Set(inti){Aobject.x

=i;}void

Display(){Aobject.Display();}private:A

Aobject;};int

main(){B

Bobject;Bobject.Set(100);Bobject.Display();#include<iostream>

例6-23

演示友元类通过类成员访问A类的私有数据成员华南理工大学计算机学院周霭如2025using

namespace

std;class

A{

friend

class

B;public:void

Display({cout<<x<<endl;};private:int

x;};class

B{public:void

Set(int

i){Aobject.x=j;}voidDisplay(){

Aobject.Display

();}

private:A

Aobject;};int

main(){B

Bobject;Bobject.Set(100);Bobject.Display();#include<iostream>

例6-23

演示友元类通过类成员调用A类的成员函数华南理工大学计算机学院周霭如2025using

namespace

std;class

A{

friend

class

B;public:void

Display({cout<<x<<endl;};private:int

x;};class

B{public:void

Set

(A&Aobject

,int

i){Aobject.x=i;}void

Display(A&Aobject

){Aobject.Display();}};int

main(){B

Bobject;Bobject.Set(100);

Bobject.Display();#include<iostream>

例6-23

演示友元类通过对象参数访问友元的成员华南理工大学计算机学院周霭如2025using

namespace

std;class

A{friend

class

B;public:voidDisplay({cout<<x<<endl;private:intx;};class

B{

public:例6

23

演示友元类B类没有数据成员仅提供对A类的操作void

Set

(A&Aobject

,intvoid

Display(A&Aobject);int

main(){B

Bobject;Bobject.Set(100);Bobject.Display();i){Aobject.x

=i;}){

Aobject.Display();}NC:\WINDOW5\system32\cmd..

.-

100请按任意键继续..

·#include<iostream>华南理工大学计算机学院周霭如2025-华南理工大学计算机学院周霭如20256.4

类的包含类的包含(称为hasA)是程序设计中一种软件重用技术。即定义一个新的类时,通过编译器把另一个类“抄”进来。当一个类中含有已经定义的类类型成员,带参数的构造函数对数据

成员初始化,须使用初始化语法形式。构造函数(变元表):对象成员1(变元表),

,对象成员n(变元表)

;华南理工大学计算机学院周霭如2025//例6-23简单对象成员初始化#include<iostream>using

namespace

std;class

A{public:A(int

x):a(x){}int

a;};class

B{public:B(int

x,int

y):aa(x){b=y;}void

out(){cout<<"aa="<<aa.a<<endl<<"b="<<b<<endl;}private:int

b;A

aa;};int

main(){BobjB(3,5);objB.out();}华南理工大学计算机学院周霭如2025#include<iostream>using

namespace

std;class

A{

public:A(int

x):a(x){}int

a;};

类类型成员class

B{public:B(int

x,int

y):aa(x){b=y;}voidout(){coutk<"aa="<<aa.a<<endl<<"b="<<b<<endl;}private:intb;A

aa;//类类型成员};int

main(){B

objB(3,5);objB.out();//例6-23简单对象成员初始化华南理工大学计算机学院周霭如2025#include<iostream>using

namespace

std;class

A{public:A(int

x):a(x){}int

a;};

调用A类构造函数class

B{public:B(int

x,int

y):aa(x

){b=y;}//

用初始化式调用成员类构造函数voidout(){cout<<"aa="<<aa.a<<endl<<"b="<<b<<endl;}

private:int

b;Aaa;//类类型成员};int

main(){B

objB(3,5);objB.out();//例6-23简单对象成员初始化华南理工大学计算机学院周霭如2025#include<iostream>using

namespace

std;class

A{public:

A(intx):a(x){}

inta;};class

B{public:B(intx,inty):aa(x){b=y;}1

1

用初始化式[void

out(){cout<<"aa="<<aa.a<kendl<<"b='private:int

b;A

aa;

//类类型成员};int

main()调用构造函数,初始化objB.b

objB.aa.b{B

objB(3,5);//例6-23简单对象成员初始化华南理工大学计算机学院周霭如2025objB.out();#include<iostream>using

namespace

std;class

A{public:A(int

x):a(x){}int

a;};class

B{public:B(intx,inty):aa(x){b=y;}

//

用初始化式调用成员类构造函数void

out(){cout<<"aa="<<aa.a<<endl<<"b="<<b<<endl;}private:int

b;

C:\WINDOWs\system32\cmd.e...-A

aa;

//类类型成员

aa

=3};

b

=5int

main()

请按任意键继续

..

·

-{B

objB(3,5);objB.out();//例6-23简单对象成员初始化华南理工大学计算机学院周霭如2025//例6-24用类包含求计算两点之间的距离#include<cmath>#include<iostream>using

namespace

std;class

Point{public:Point(int

xi=0,int

yi=0){x=xi;y=yi;}

intGetX({return

x;}intGetY({return

y;}private:int

x;int

y;};class

Distance{public:Distance(Point

xp1,Point

xp2);GetDis(){returnp1,p2;dist;doubleprivate:Pointdouble};华南理工大学计算机学院周霭如2025dist;}#include<cmath>#include<iostream>using

namespace

std;class

Point{

public:Point(intxi=0,int

yi=0){x=xi;y=yi;}

int

GetX({

return

x;}int

GetY(

{

return

y;}private:int

x;int

y;};class

Distance{public:Distance(Pointxp1,Pointxp2);double

GetDis(){return

dist;}private:Point

p1,p2;

//Point

类数据成员doubledist;};//例6-24用类包含求计算两点之间的距离华南理工大学计算机学院周霭如2025//例6-24用类包含求计算两点之间的距离#include<cmath>#include<iostream>using

namespace

std;class

Point{public:Point(int

xi=0,int

yi=0){x=xi;y=yi;}

int

GetX({return

x;}int

GetY(){return

y;}private:int

x;int

y;};class

Distance{public:Distance(Point

xp1,Point

xp2);double

GetDis(){return

dist;}private:Point

p1,p2;double

dist;

};//Point类数据成员//记录距离的数据成员华南理工大学计算机学院周霭如2025//构造函数计算距离Distance::Distance(Point

xp1,Point

xp2):p1(xp1),p2(xp2){double

x=double(p1.GetX()-p2.GetXO);double

y=double(p1.GetY()-p2.GetYO);dist=sqrt(x*x+y*y);}int

main()#include<cmath>#include<iostream>using

namespace

std;class

Point{public:Point(int

xi=0,intint

GetX({returint

GetY(){retury;{Point

mp1(5,10),mp2(20,30);Distancemdist(mp1,mp2);cout<<"The

distance

is"<<mdist.GetDis()<<endl;N

C:\WINDOWs\system32\cmd..

-

×Thedistance

is

25请按任意键继续..Distance(Point

xp1,Point

xp2);double

GetDis(){return

dist;}

private:private:int

x;int};class

Distance{public://Point

类数据成员//记录距离的数据成员//例6-24用类包含求计算两点之间的距离Pointdouble};p1,p2;

dist;华南理工大学计算机学院周霭如2025华南理工大学计算机学院周霭如2025>类类型通常用关键字class定义。类的实例称为对象。>数据成员是类的属性,可以为各种合法的C++数据类型。>成员函数用于操作类的数据或在对象之间发送消息。类成员由private

、protected

和public决定访问特性。>构造函数是特殊的成员函数,在创建和初始化对象时自动调用;析

构函数则在对象作用域结束时自动调用。>重载构造函数和复制构造函数提供了创建对象的不同初始化方式。>常数据成员是建立对象后约束为只读的数据成员,常成员函数不能

修改数据成员的值。静态成员为同类对象共享机。静态成员函数用于操作静态数据成员。

一个类的友元可以访问该类各种性质的成员。类的包含是一种软件重用技术。华南理工大学计算机学院周霭如2025华南理工大学计算机学院周霭如2025华南理工大学计算机学院周霭如20257.1

运算符重载规则7.2

用成员或友元函数重载运算符7.3

几个典型运算符重载7.4

类类型转换

小结第7章运算符重载运算符重载使得用户自定义的数例如int

x,y;y=X+y;complexc1,c2;c1=Cadd(c1,c29;matrixm1,m2;m1=Madd(m1,m2);能表示为c1

=c1+c2;定义运算符重载函数矩阵类对象//调用函数计算两个矩阵的和第7章运

载7.1

运算符重载规则7.1.1

重载运算符的限制可以重载的运算符不能重载的算符.*

?:

sizeof^-=>>=delete[]%+=>>++new[]/><<delete+!八[]*<|=&&new■=&=>=()&*=<=华南理工大学计算机学院周霭如2025~%!=

->/===7.1

运算符重载规则7.1.1

重载运算符的限制重载运算符函数可以对运算符作出新的解释,但原有基本语义不变:>

不改变运算符的优先级>

不改变运算符的结合性>

不改变运算符所需要的操作数>

不能创建新的运算符华南理工大学计算机学院周霭如20257.1.2运算符重载的语法形式运算符函数是一种特殊的成员函数或友元函数成员函数的语法形式为:类型类名::

operator

op(参数表)//相对于该类定义的操作华南理工大学计算机学院周霭如20257.1.2运算符重载的语法形式运算符函数是一种特殊的成员函数或友元函数成员函数的语法形式为:类

类名::

operator

op(参数表)//

相对于该类定义

函数返回类型华南理工大学计算机学院周霭如20257.1.2运算符重载的语法形式运算符函数是一种特殊的成员函数或友元函数成员函数的语法形式为:类

名::

operator

op(参数表)//

相对于该类定义

的操作重载该运算符的类华南理工大学计算机学院周霭如20257.1.2运算符重载的语法形式运算符函数是一种特殊的成员函数或友元函数成员函数的语法形式为:类型类名::

operator

op(参数表)//相对于该类定义的操作被重载的运算符华南理工大学计算机学院周霭如20257.1.2运算符重载的语法形式运算符函数是一种特殊的成

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论