实验十_运算符重载答案

合集下载

c 运算符的重载习题答案

c  运算符的重载习题答案

1.概念填空题1.1运算符重载是对已有的运算符赋予多重含义,使同一个运算符在作用于不同类型对象时导致不同的行为。

运算符重载的实质是函数重载,是类的多态性特征。

1.2可以定义一种特殊的类型转换函数,将类的对象转换成基本数据类型的数据。

但是这种类型转换函数只能定义为一个类的成员函数而不能定义为类的友元函数。

类类型转换函数既没有参数,也不显式给出返回类型。

类类型函数中必须有return 表达式的语句返回函数值。

一个类可以定义多个类类型转换函数。

1.3运算符重载时其函数名由operator运算符构成。

成员函数重载双目运算符时,左操作数是对象,右操作数是函数参数。

2.简答题2.2简述运算符重载的规则。

2.2简述重载单目运算符++、--,前置和后置时的差别。

2.3 C++中重运算符是否都可以重载?是否都可以重载成类的成员函数?是否都可以重载成类的友元函数?2.4 构造函数作为类型转换函数的条件是什么。

3.选择题3.1在下列运算符中,不能重载的是(B)A.!B. sizeofC. newD. delete3.2 不能用友员函数重载的是(A)。

A.=B.==C.<=D.++3.3下列函数中,不能重载运算符的函数是(B)。

A.成员函数B.构造函数C.普通函数D.友员函数3.4如果表达式++i*k时中的”++”和”*”都是重载的友元运算符,则采用运算符函数调用格式,该表达式还可表示为(B)。

A.operator*(i.operator++(),k) B.operator*(operator++(i),k)C.i.operator++().operator*(k) D.k.operator*(operator++(i))3.5已知在一个类体中包含如下函数原型:VOLUME operator-(VOLUME)const;下列关于这个函数的叙述中,错误的是(B )。

A.这是运算符-的重载运算符函数B.这个函数所重载的运算符是一个一元运算符C.这是一个成员函数D.这个函数不改变数据成员的值3.6在表达式x+y*z中,+是作为成员函数重载的运算符,*是作为非成员函数重载的运算符。

最新《C++程序设计案例教程》习题答案第10章 运算符重载

最新《C++程序设计案例教程》习题答案第10章  运算符重载

第10章运算符重载一、选择题1.A2.D3.A4.B5.D6.C二、写出下列程序运行结果(略)三、简答题1.(1)只能重载已有的C++运算符,不可自创新的运算符。

(2)重载后运算符的优先级和结合性都不变。

(3)重载的功能应当与原有功能相类似。

2.: : . .* ? : sizeof typeid 3.&& | | ,四、编程题1.#include<iostream>class MyString{public:MyString(){strLength=0;pStr=new char[1];*pStr='\0';}MyString(const char*pString){strLength=strlen(pString);pStr=new char[strLength+1];strcpy(pStr,pString);}MyString(char ch,int n){strLength=n;pStr=new char[strLength+1];for(unsigned int i=0;i<strLength;*(pStr+i++)=ch);*(pStr+strLength)='\0';}MyString(int number){char buffer[20];int temp=number;if(number<0)number=-number;int len=0;do{buffer[len++]=static_cast<char>('0'+number%10 );number/=10;}while(number>0);if(temp<0)buffer[len++]='-';buffer[len]='\0';strLength=len;pStr=new char[strLength+1];strcpy(pStr,buffer);char ch=0;for(int i=0,j=len-1;i<j;i++,j--){ch=pStr[i];pStr[i]=pStr[j];pStr[j]=ch;}}MyString(const MyString&rString){strLength=rString.strLength;pStr=new char[strLength+1];strcpy(pStr,rString.pStr);}~MyString(){delete[]pStr;}MyString& operator+(const MyString&rStr)const {return MyString(*this)+=rStr;}};2.#include<iostream>class MyString{public:MyString(){strLength=0;pStr=new char[1];*pStr='\0';}MyString(const char*pString){strLength=strlen(pString);pStr=new char[strLength+1];strcpy(pStr,pString);}MyString(char ch,int n){strLength=n;pStr=new char[strLength+1];for(unsigned int i=0;i<strLength;*(pStr+i++)=ch);*(pStr+strLength)='\0';}MyString(int number){char buffer[20];int temp=number;if(number<0)number=-number;int len=0;do{buffer[len++]=static_cast<char>('0'+number%10 );number/=10;}while(number>0);if(temp<0)buffer[len++]='-';buffer[len]='\0';strLength=len;pStr=new char[strLength+1];strcpy(pStr,buffer);char ch=0;for(int i=0,j=len-1;i<j;i++,j--){ch=pStr[i];pStr[i]=pStr[j];pStr[j]=ch;}}MyString(const MyString&rString){strLength=rString.strLength;pStr=new char[strLength+1];strcpy(pStr,rString.pStr);}~MyString(){delete[]pStr;}MyString& Operator=(const MyString&rStr)const{if(this == &rStr)return *this;delete []str;str = new char[strlen(rStr.str )+1];strcpy(str, rStr.str);return *this;}};。

c运算符的重载习题答案

c运算符的重载习题答案

1.概念填空题1.1运算符重载是对已有的运算符赋予多重含义,使同一个运算符在作用于不同类型对象时导致不同的行为。

运算符重载的实质是函数重载,是类的多态性特征。

1.2可以定义一种特殊的类型转换函数,将类的对象转换成基本数据类型的数据。

但是这种类型转换函数只能定义为一个类的成员函数而不能定义为类的友元函数。

类类型转换函数既没有参数,也不显式给出返回类型。

类类型函数中必须有return 表达式的语句返回函数值。

一个类可以定义多个类类型转换函数。

1.3运算符重载时其函数名由operator运算符构成。

成员函数重载双目运算符时,左操作数是对象,右操作数是函数参数。

2.简答题运算符重载的规则。

2.2简述重载单目运算符++、--,前置和后置时的差别。

2.3 C++中重运算符是否都可以重载?是否都可以重载成类的成员函数?是否都可以重载成类的友元函数?2.4 构造函数作为类型转换函数的条件是什么。

3.选择题3.1在下列运算符中,不能重载的是(B)A.!B. sizeofC. newD. delete3.2 不能用友员函数重载的是(A)。

A.=B.==C.<=D.++3.3下列函数中,不能重载运算符的函数是(B)。

A.成员函数B.构造函数C.普通函数D.友员函数3.4如果表达式++i*k时中的”++”和”*”都是重载的友元运算符,则采用运算符函数调用格式,该表达式还可表示为(B)。

A.operator*(i.operator++(),k) B.operator*(operator++(i),k)C.i.operator++().operator*(k) D.k.operator*(operator++(i))3.5已知在一个类体中包含如下函数原型:VOLUME operator-(VOLUME)const;下列关于这个函数的叙述中,错误的是(B )。

A.这是运算符-的重载运算符函数B.这个函数所重载的运算符是一个一元运算符C.这是一个成员函数D.这个函数不改变数据成员的值3.6在表达式x+y*z中,+是作为成员函数重载的运算符,*是作为非成员函数重载的运算符。

C++运算符重载题库及答案

C++运算符重载题库及答案

运算符重载一.单项选择题1.下列运算符中,运算符在C++中不能重载。

A.?:B.+C.D.<=解:C++中不能被重载的运算符有:·,一,::,?:。

本题答案为A。

2.下列运算符中,运算符在C++中不能重载。

A.&&B.[]C.::D.new解:c++中不能被重载的运算符有:·,·+,::,?:。

本题答案为c。

3.下列关于运算符重载的描述中,是正确的。

A.运算符重载可以改变操作数的个数B.运算符重载可以改变优先级C.运算符重载可以改变结合性D.运算符重载不可以改变语法结构解:运算符重载不能改变操作数的个数、运算符的优先级、运算符的结合性和运算程的语法结构。

本题答案为D。

4.友元运算符objl>obj2被C++编译器解释为。

A.operator>(objl,obj2) B.>(obj1,obj2)C.obj2.operator:>(obj1) D.objl.operator>(obj2)解:重载为友元函数的运算符的调用形式如下:operator<运算符>(<参数,<参数2>)等价于:<参数1><运算符><参数2>本题答案为A。

5.现需要对list类对象使用的逻辑运算符“==”重载,以下函数声明是正确的。

A、list&list::operator==(const list&a) ;B、list list::operator==(const list&a) ;C、bool&list::operator==(const list&a);D、bool list::operator==(const list&a);6.以下类中分别说明了“+=”和“++”运算符重载函数的原型。

如果主函数中有定义:fun m,c,d;,那么,执行语句c=m++;时,编译器把m++解释为:(3)A)c.o p e r a t o r++(m);B)m=o p e r a t o r++(m);C)m.o p e r a t o r++(m);D)o p e r a t o r++(m);class fun{public:....f u n o p e r a t o r+=(f u n);f r i e n d f u n o p e r a t o r++(f u n&,i n t);};答案:D7.在第33题中,当执行语句d+=m;时,C++编译器对语句作如下解释:(34)A.d=operator+=(m);B.m=operator+=(d);C. d.operator+=(m);D.m.operator+=(d);答案:C8.设有以下类定义,其中说明了“+”运算符重载函数的原型。

[C++]运算符重载实验报告

[C++]运算符重载实验报告

+operator+(const COMPLEX &other): COMPLEX+operator-(const COMPLEX &other) : COMPLEX+operator-(): COMPLEX+operator=(const COMPLEX &other) : COMPLEX运行结果2. 程序的类结构图为:Tx,y:int+T(int a,int b)+&operator<<(ostream &os,T &a):friend ostream运行结果3. 程序的类结构图为:Shape+Area():virtual double const+PrintShapeName():virtual void const +Print():virtual void constPointx,y:int+Point(int=0,int=0)+SetPoint(int a,int b):void+GetX():int const+GetY():int const+PointShapeName():virtual void const +Print():virtual void constCircleradius:double+Circle(int x=0,int y=0,double r=0.0) +SetRadius(double r):void+GetRadius():double const+Area():virtual double const+Print():virtual void const+PrintShapeName():virtual void const 运行结果{cout<<'['<<x_size<<","<<y_size<<']'<<", "<<'['<<i_size<<","<<j_size<<']'; }int main(){Circle1 circle(0.0,0.0,3.0);circle.area();circle.perimeter();circle.print();cout<<"\n";Square1 square(0.0,0.0,3.0,3.0);square.area();square.perimeter();square.print();cout<<"\n";cout<<"圆的面积为:"<<circle.area()<<endl;cout<<"圆的周长为:"<<circle.perimeter()<<endl;cout<<"圆的圆心坐标和半径为:";circle.print();cout<<"\n\n";cout<<"正方形的面积为:"<<square.area()<<endl;cout<<"正方形的周长为:"<<square.perimeter()<<endl;cout<<"正方形的中心坐标和一个顶点坐标分别为:";square.print();cout<<"\n";return 0;}运行结果【实例编程】运行结果。

《Java程序设计案例教程》第十章练习答案

《Java程序设计案例教程》第十章练习答案

第10章多态性与虚函数一、单项选择题1.实现运行时的多态性要使用(D)。

A.重载函数B.构造函数C.析构函数D.虚函数2.通过运算符重载,可以改变运算符原有的(A)。

A.操作数类型B.操作数个数C.优先级D.结合性3.将运算符重载为类成员函数时,其参数表中没有参数,说明该运算符是(B)。

A.不合法的运算符B.一元运算符C.无操作数的运算符D.二元运算符4.在重载一个运算符时,其参数表中没有任何参数,说明该运算符是(B)。

A.作为友元函数重载的一元运算符B.作为成员函数重载的一元运算符C.作为友元函数重载的二元运算符D.作为成员函数重载的二元运算符5.如果表达式++a中的"++"是作为成员函数重载的运算符,若采用运算符函数调用格式,则可表示为(D)。

A.a.operator++(1)B.operator++(a)C.operator++(a,1)D.a.operator++()6.如果表达式a>=b中的">="是作为非成员函数重载的运算符,则可以等效地表示为(C)。

A.a.operator>=(b)B.b.operator>=(a)C.operator>=(a,b)D.perator>=(b,a)7.有如下程序:#include<iostream>using namespace std;class A{public:virtual void funl (){cout<<"A1";}void fun2 (){cout<<"A2";}};class B: public A{public:void funl (){cout<<"Bl";}void fun2 (){cout<<"B2";}};int main(){A*p=new B;p->funl ();p->fun2();return 0;}程序执行后,输出结果为(C)。

运算符重载程序例题解答

运算符重载程序例题解答

/*1.定义一个复数类,通过重载运算符:+、-、*、/ 等,实现两个复数之间的各种运算。

编写一个完整的程序。

*/#include<iostream.h>class Complex{float Real,Image;public:Complex(float x=0,float y=0){Real=x;Image=y;}friend Complex operator + (Complex &,Complex &);friend Complex operator - (Complex &,Complex &);friend Complex operator * (Complex &,Complex &);friend Complex operator / (Complex &,Complex &);void show(){cout<<"Real="<<Real<<'\t'<<"Image="<<Image<<endl;}};Complex operator + (Complex &a,Complex &b){Complex t;t.Real=a.Real+b.Real;t.Image=a.Image+b.Image;return t;}Complex operator - (Complex &a,Complex &b){Complex t;t.Real=a.Real-b.Real;t.Image=a.Image-b.Image;return t;}Complex operator * (Complex &a,Complex &b){Complex t;t.Real=a.Real*b.Real-a.Image*b.Image;t.Image=a.Real*b.Image+a.Image*a.Real;return t;}Complex operator / (Complex &a,Complex &b){Complex t;t.Real=(a.Real*b.Real+a.Image*b.Image)/(b.Real*b.Real+b.Image*b.Image);t.Image=(a.Image*a.Real-a.Real*b.Image)/(b.Real*b.Real+b.Image*b.Image);return t;}{Complex c1(10,20),c2,c3(50,40);c2=c1+c3;c2.show();c2=c1-c3;c2.show();c2=c1*c3;c2.show();c2=c1/c3;c2.show();}/*2.定义描述一个三维点,利用友元函数重载"++"和"--"运算符,并区分这两种运算符的前置和后置运算。

第11章 运算符重载 习题解答

第11章  运算符重载 习题解答

第11章运算符重载一.单项选择题1.下列运算符中,运算符在C++中不能重载。

A.?: B.+ C. D.<=解:C++中不能被重载的运算符有:·,一,::,?:。

本题答案为A。

2.下列运算符中,运算符在C++中不能重载。

A.&& B.[] C.:: D.new解:c++中不能被重载的运算符有:·,·+,::,?:。

本题答案为c。

3.下列关于运算符重载的描述中,是正确的。

A.运算符重载可以改变操作数的个数B.运算符重载可以改变优先级C.运算符重载可以改变结合性D.运算符重载不可以改变语法结构解:运算符重载不能改变操作数的个数、运算符的优先级、运算符的结合性和运算程的语法结构。

本题答案为D。

4.友元运算符objl>obj2被C++编译器解释为。

A.operator>(objl,obj2) B.>(obj1,obj2)C.obj2.operator:>(obj1) D.objl.operator>(obj2)解:重载为友元函数的运算符的调用形式如下:operator<运算符>(<参数1>,<参数2>)等价于:<参数1><运算符><参数2>本题答案为A。

5.现需要对list类对象使用的逻辑运算符“==”重载,以下函数声明是正确的。

A、list & list::operator==(const list &a);B、list list::operator==(const list &a);C、bool & list::operator==(const list &a);D、bool list::operator==(const list &a);6. 以下类中分别说明了“+=”和“++”运算符重载函数的原型。

类和对象与运算符重载-答案

类和对象与运算符重载-答案

类和对象与运算符重载-答案选择题1、若有以下说明,在类外使用对象objx成员的正确语句是()。

class X{ int a;void fun1();public:void fun2();};A objx.a=0B objx.fun1();C objx.fun2();D X::fun1();2、若有以下说明,对n的正确访问语句是()。

class Y{ //………;public:static int n;};int Y::n=0;Y objy;A n=1;B Y::n=1;C objy::n=1;D Y->n=1;3、若有以下类Z的说明,函数fstatic的正确定义是()。

class Z{ int a;public:void fstatic(Z &)static;};A void Z::fstatic(Z &objz){objz.a=0;}B void Z::fs tatic(Z &objz){a=objz.a;}C void Z::fstatic(Z &objz){a=0;}D void Z::fstatic(Z &objz){Z::a=0;}4、若有以下类T的说明,函数ffriend的错误定义是()。

class T{ int i;friend void ffriend(T &,int);};A void ffriend(T &objt,int k){objt.i=k;}B void ffriend(T &objt,int k){k=objt.i;}C void T::ffriend(T &objt,int k){K+=objt.i;}D void ffriend(T &objt,int k){objt.i+=k;}5、在类定义的外部,可以被访问的成员有()。

A 所有类成员B 私有或保护类成员C公有的类成员 D 公有或私有的类成员6、关于this指针的说法正确的是()。

C++实验报告之静态成员、运算符重载

C++实验报告之静态成员、运算符重载

题目1:定义一个复数类,通过重载运算符:*,/,直接实现二个复数之间的乘除运算。

编写一个完整的程序,测试重载运算符的正确性。

要求乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。

源程序1/*******************第1题*******************//******************单森汉*****************//******************2012-5-1*****************/#include<iostream>using std::cout;using std::endl;class Complex{float Real, Image;public:Complex(float r=0,float i=0) { Real=r;Image=i;}void Show(){cout <<"Real="<<Real<<'\t'<<"Image="<<Image<<'\n';}friend Complex operator *(Complex &, Complex &);Complex operator /(Complex &); //重载运算符+Complex operator +( Complex &);friend Complex operator -(Complex &, Complex &);};Complex operator *( Complex &c1,Complex &c2){Complex t;t.Real=c1.Real * c2.Real - c1.Image * c2.Image;t.Image = c1.Image*c2.Real +c1.Real* c2.Image;return t;}Complex Complex::operator /(Complex &c){Complex t;t.Real =(Real *c.Real+ Image * c.Image)/(c.Real*c.Real+ c.Image * c.Image);t.Image = (Image *c.Real - Real * c.Image)/(c.Real*c.Real+ c.Image * c.Image);return t;}Complex Complex::operator + ( Complex &c){Complex t;t.Real = Real + c.Real;t.Image = Image + c.Image;return t;}Complex operator -(Complex &c1, Complex &c2){Complex t;t.Real=c1.Real-c2.Real;t.Image=c1.Image-c2.Image;return t;}void main(){Complex c1(1,2),c2(3,4),c3;c3=c1*c2;cout<<"两个复数的乘法c3=c1*c2:";c3.Show();c3=c1/c2;cout<<"两个复数的除法c3=c1/c2:";c3.Show();Complex c4(1,2),c5(3,4),c6,c7(1,2),c8(3,0),c9; c6=c4+c5;cout<<"两个复数的加法c6=c4+c5:";c6.Show();c6=c4-c5;cout<<"两个复数的减法c6=c4-c5:";c6.Show();c9=c7+c8;cout<<"一个复数与一个实数的加法c9=c7+c8:"; c9.Show();c9=c7-c8;cout<<"一个复数与一个实数的减法c9=c7-c8:"; c9.Show();}运行结果截图题目2:定义一个向量(一维数组)类,通过重载运算符实现向量之间的加法和减法。

第10章_重载正答案

第10章_重载正答案

一、选择题1.重载赋值操作符时,应声明为()函数。

A.友元 B.虚√ C.成员 D.多态2.指出下列对定义重载函数的要求中,哪些是错误的提法( )。

A.要求参数的个数不同。

B.要求参数中至少有一个类型不同。

√C.要求函数的返回值不同。

D. 要求参数的个数相同时,参数类型不同。

3. 下列有关运算符重载的描述中,()是正确的。

A.运算符重载可改变其优先级√ B.运算符重载不改变其语法结构C.运算符重载可改变其结合性 D.运算符重载可改变其操作数的个数4.下列运算符中,()运算符在C++中不能重载。

√A.?: B.[] C.new D.&&5.去掉6.去掉7. 一个函数为void f(int,char ch='a'),另一个函数为void f(int),则它们√A.不能在同一程序中定义 B.可以在同一程序中定义并可重载C.可以在同一程序中定义,但不可重载 D 以上说法均不正确8.去掉9.假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为()。

A. AB operator+(AB & a , AB & b) √B. AB operator+(AB & a)C. operator+(AB a)D. AB & operator+( )10.系统在调用重载函数时,往往根据一些条件确定哪个重载函数被调用,在下列选项中不能作为依据的是()。

A. 参数个数B. 参数的类型C. 函数的名称√D. 函数的类型二、填空题1.operator是C++的一个关键字,它经常和C++的一个运算符连用,构成一个运算符函数名。

运算符函数的返回类型不能是.void类型。

2.利用成员函数对二元运算符重载,其左操作数为成员函数参数 (默认)第1个对象引用,右操作数为第2个对象引用 。

3.如果表达式--x中的“--”是重载的类运算符,采用运算符函数调用格式,该表达式还可以表示为.operator—(x) 作为友员函数。

C++实验报告(10)运算符重载

C++实验报告(10)运算符重载
指导老师:年月日
填写内容时,可把表格扩大。
{cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\npay:"<<pay<<"\n\n";}
int main()
{Teacher teacher1(10001,"Li",'f',1234.5),teacher2;
Student student1(20010,"Wang",'m',89.5);
Complex operator+(int &i);
friend Complex operator+(int&,Complex &);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c)
return c;}
Complex Complex::operator/(Complex &c2)
{Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

4--运算符重载

4--运算符重载

运算符重载习题一、选择题1、下面关于运算符重载的说法中,错误的是()。

A、可以对C++所有运算符进行重载B、运算符重载保持固有的结合性和优先级顺序C、运算符重载不能改变操作数的个数D、在运算符函数中,不能使用缺省的参数值2、下列运算符能被重载的是()。

A、::B、?:C、.D、%3、下列叙述正确的是()。

A、运算符重载函数只能是一个成员函数B、运算符重载函数既可以是一个成员函数,也可以是友元函数C、运算符重载函数只能是一个非成员函数D、运算符重载函数只能是友元函数4、下列叙述不正确的是()。

A、利用成员函数重载二元运算符时,参数表中的参数必须为两个B、利用成员函数重载二元运算符时,成员函数的this指针所指向的对象作为运算符的左操作数C、利用成员函数重载二元运算符时,参数表中的参数作为此运算符的右操作数D、运算符重载时不能改变运算符的语法结构5、为了区分一元运算符的前缀和后缀运算,在后缀运算符进行重载时,额外添加一个参数,其类型是()。

A、voidB、charC、intD、float6、下列能正确重载运算符的友元函数原型是()。

A、friend B operator?: ( );B、friend B operator+(int x);C、friend B operator+(B b);D、friend B operator+(B b, B a);7、下列叙述正确的是()。

class B{ int a,b;public:B(int aa=0, int bb=0) { a=aa; b=bb; }B operator+ ( int x ) //A{ B r;r.a=a+x;r.b=b+x;return r;}};void main( ){ B x(3,5), y(8,4), z1, z2;z1=x+5;z2=10+y;//B}A、A行有错误B、B行有错误C、A行和B行都有错误D、A行和B行都没有错误8、下列叙述正确的是()。

C++实验10运算符重载

C++实验10运算符重载

实验10 运算符重载1.实习目的及要求1)掌握运算符重载的基本概念和方法;2)熟习几种特殊的运算符的重载2.预习预习运算符重载的概念、运算符重载的一般方法,包括成员函数重载和友元函数重载;以及几种特殊的运算符,包括++和- - 运算符、赋值运算符、3.实验内容3.1分析下面的程序,指出程序运行的结果:1) 分析下面的程序,指出程序运行的结果:类分析和对象分析:结果预测:此程序实现对加法的重载,两对象(坐标)的x 和y 数据项分别相加,即两点坐标相加得出新点,所以结果预测为:30,302) 分析下面的程序,指出程序运行的结果:类和对象分析:结果预测:重载加法,使“+”适用于具体日期的计算,2001年(平年)2月10日(平年2月是28天)的20天后是2001年3月2日,所以结果预测为:3/2/20013.2 编写并调试程序:1)定义一个描述平面上一个点的类point,重载“++”和“--”运算符,并区分这两种运算符的前置和后置操作,构成一个完整的程序。

1)对象分析:只有一类对象:点2)类的分析和设计:一个描述平面上一个点的类point,重载“++”和“--”运算符操作设计:a)类声明和类实现:Main部分实现:程序结果:2)构造一个分数类rationalNumber,该类中包括分子和分母两个成员数据,并具有下述功能:(1)建立构造函数,它能防止分母为零,当分数不是最简形式时进行约分,并避免分母为负数。

(2)重载加法、减法、乘法以及除法运算符。

(3)重载关系运算符:>、<、==等。

◆对象分析:只有一类对象:分数◆类的分析和设计:分数类rationalNumber,该类中包括分子和分母两个成员数据,具有构造函数,重载运算符等功能类声明和类实现:Main部分实现:程序结果:*4 课后练习调试下面的程序,指出程序实现的功能及程序输出结果,掌握对象数组、进一步理解类是如何实现。

1)调试程序1具体分析如分析题1,本程序实现的功能是两点坐标的加法。

课本答案

课本答案
cin >> d; // 输入日期
cout << d << endl; // 输出日期
system("PAUSE"); // 调用库函数system( ),输出系统提示信息
return 0; // 返回值, 返回操作系统
}
*3.设计一个时间类Time,要求:
using namespace std; // 使用命名空间std
// 声明复数类
class Complex
{
private:
// 数据成员
double real; // 实部
double image; // 虚部
……
1.定义一个复数类Complex,重载运算符“+”,“-”,“*”,使之能用于复数的加、减、乘。编程序,分别求两个复数之和、差与积。
……
int main() // 主函数main()
{
Complex z1(2, 3), z2(6, -5), z3; // 定义复数对象
z1.Show(); // 输出z1
z2.Show(); // 输出z2
z3 =z1 + z2; // z3=z1+z2
答案:B
填空题
1.若以非成员函数形式为类MyClass重载“!”运算符,其操作结果为一个bool型数,则该运算符重载函数的原型是 。
解析:以非成员函数形式重载一元运算符“!”,一定含有一个参数,此处为类MyClass的常引用。
答案:bool operator!( const MyClass &);
(1)包含时(hour)、分(minute)和秒(second)私有数据成员。

运算符重载、继承、派生程序题与答案

运算符重载、继承、派生程序题与答案

1. (10分)栈类——类模板题目描述用类模板方式设计一个链栈类stack<T>,其中有两个私有数据成员:Node<T> *head(链首指针,即栈顶元素指针),int num(栈里结点数)以及3个公有成员函数:push(元素入栈)、pop(元素出栈)和stackempty(判断栈是否为空),并建立一个整数栈和一个字符栈。

template <class T>class stack{};注意:为了能够生成结点类型不同的链式栈,结点类的设计也需要用类模板。

template<class T>class Node{};输入描述输入整数栈的数据元素和字符栈的数据元素输出描述输出整数栈的数据元素出栈序列和字符栈的数据元素出栈序列输入样例4 8 3 2a db c输出样例2 3 8 4c bd a我的代码:#include<iostream>using namespace std;template <class T>class stacktemplate <class T1>class Node{T1 a ;Node<T1> *next ;public:Node(T1 x):a(x){}void show(){cout << a << endl ;}friend class stack<T1> ;};Node<T> *head ;int num ;public:stack(){num = 0 ;head = NULL ;}void push(T x){Node<T> *p = new Node<T>(x) ;p->next = head ;head = p ;num++ ;}T pop(){Node<T> *p = head ;head = head->next ;T t = p->a ;delete p ;num-- ;return t ;}bool stackempty(){if( head == NULL )return true;elsereturn false;}};int main(){stack<int> a ;stack<char> b ;int x ;char y ;for(int i = 0 ; i < 4 ; i ++ ){cin >> x ;a.push(x) ;}for( int i = 0 ; i < 4 ; i ++ ){cin >> y ;b.push(y);}for( int i = 3 ; i >= 0 ; i -- ) {a.stackempty() ;cout << a.pop() << " " ;}cout << endl ;for( int i = 3 ; i >= 0 ; i -- ) {b.stackempty();cout << b.pop() << " " ;}cout << endl ;return 0 ;}2. (10分)二进制类(1)——运算符重载题目描述将一个16位二进制数表示成0和1的字符序列,即用一个字符数组来存放这个二进制数。

C++实验21 运算符重载-推荐下载

C++实验21 运算符重载-推荐下载

三、实验内容
1.定义一个类 MatrixAdd,其数据成员为一个 M*N 的矩阵(即二维数组),其中 M 和 N 是两个预先定义的符号常数。通过重载运算符”+”和”=”,实现矩阵相加和赋值运算。如定
1 2 3 2 2 2 义矩阵 A= 4 5 6 和 B= 3 3 3 ,通过运算 C=A+B,得到结果矩阵
课前练习题
1.重载运算”+”,实现 a+b 运算,则
A.a 必须为对象,b 可为整数或实数
C.b 必须为对象,a 可为整数或实数

2.在 C++中,运算符的重载有两种实现方法,一种是通过成员函数来实现,另一种则通过
_________来实现。
3.不能重载的 5 个运算符是:______、______、______、______、_________。
B.a 和 b 必须为对象
D.a 和 b 均可为整数或实数
void main()
{
}
C c(5.6);
(--c).show();
c.show();ห้องสมุดไป่ตู้
请回答问题:
①从行 A 的形式上看,这是利用________函数实现__________运算符的重载。
②行 B 的输出结果:_________,行 C 的输出结果:_________。
③上机验证输出结果,并且回答行 B 和行 C 的输出结果一样吗?为什么?
2.已知 C 为某类名,以下是”+”和”+=”的运算符重载函数。
C C:: operator+(C &m )
{
}
C temp;
temp.x=x+m.x;
return temp;
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验十运算符重载1.实验目的及要求1)掌握运算符重载的基本概念和方法。

2)熟习几种特殊的运算符的重载。

2.实验内容1.分析下面的程序,指出程序运行的结果:1)#include<iostream.h>class point{int x,y;public:point(int vx,int vy){x=vx; y=vy;}point(){x=0,y=0;}point operator+(point p1){int px=x+p1.x;int py=y+p1.y;return point(px,py);}point operator-(point p1){point p;int px=x-p1.x;int py=y-p1.y;return point(px,py);}void print(){cout<<x<<","<<y<<endl;}};void main(void){point p1(10,10),p2(20,20);p1=p1+p2;p1.print();//p2.print();//p3.print();}程序分析:运行结果为:30,30Press any key to continue2)分析下面程序,指出程序运行的结果:#include<iostream.h>static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31}; class date{int mo,da,yr;public:date(int m,int d,int y){mo=m;da=d;yr=y;}date(){}void disp(){cout<<mo<<"/"<<da<<"/"<<yr<<endl;}date operator+(int day){date dt=*this;day+=dt.da;//cout<<day<<endl;while(day>dys[dt.mo-1]){day-=dys[dt.mo-1];//cout<<day<<endl;if(++dt.mo==13){dt.mo=1;dt.yr++;}}//cout<<day<<endl;//dt.disp();dt.da=day;return dt;}};void main(void){date d1(5,31,2009),d2;//d1.disp();d2=d1+20;d2.disp();}程序运行结果:6/20/2009Press any key to continue3)p256 5.14 5.15 5.165.14实验结果:76Press any key to continue5.15实验结果:This is C++book.第1个字符:T第16个字符:.第25个字符:数组下标超界!Press any key to continue5.16实验结果:m=1.5千米Press any key to continue三. 编写并调试程序.定义一个描述平面上一个点的类point,重载“++”和“--”运算符,并区分这两种运算符的前置和后置操作,构成一个完整的程序。

(参考例5.8)程序代码:#include<iostream>using namespace std;class point{public:point(int I1=0,int I2=0,int I3=0);void print();point operator--();point operator--(int);point operator++();point operator++(int);private:int i,j,k;};point::point(int I1,int I2,int I3){i=I1;j=I2;k=I3;}void point::print(){cout<<"i: "<<i<<"j: "<<j<<"k: "<<k<<endl;} point point::operator --(){--i;--j;--k;return *this;}point point::operator --(int){point temp(*this);i--;j--;k--;return temp;}point point::operator ++(){++i;++j;++k;return *this;}point point::operator ++(int){point temp(*this);i++;j++;k++;return temp;}int main(){point obj1(4,5,6),obj2,obj3(11,12,13),obj4;--obj1;obj1.print();obj2=obj1--;obj2.print();obj1.print();cout<<endl;obj3.print();obj3.operator--();obj3.print();obj4=obj3.operator --(0);obj4.print();obj3.print();cout<<endl;point obj5(4,5,6),obj6,obj7(11,12,13),obj8; obj5.print();++obj5;obj5.print();obj6=obj5++;obj6.print();obj5.print();cout<<endl;obj7.operator++();obj7.print();obj8=obj7.operator ++(0); obj8.print();obj7.print();cout<<endl;}程序运行的结果是:i: 4j: 5k: 6i: 3j: 4k: 5i: 3j: 4k: 5i: 2j: 3k: 4i: 11j: 12k: 13i: 10j: 11k: 12i: 10j: 11k: 12i: 9j: 10k: 11i: 4j: 5k: 6i: 5j: 6k: 7i: 5j: 6k: 7i: 6j: 7k: 8i: 11j: 12k: 13i: 12j: 13k: 14i: 12j: 13k: 14i: 13j: 14k: 15Press any key to continue2)构造一个分数类rationalNumber,该类中包括分子和分母两个成员数据,并具有下述功能:1建立构造函数,它能防止分母为0,当分数不是最简形式时进行约分,并避免分母为负数。

2重载加法、减法、乘法和除法运算符。

3重载关系运算符:>、<、==等。

提示:分数约分函数:class rationalNumber //分数类{ int fm,fz; //fm—分母fz—分子……};void simple() //约分函数{ int k,i;if(abs(fz)<fm)k=abs(fz);elsek=fm;for(i=1;i<=k;i++){if(fz%i==0 && fm%i==0){fz=fz/i;fm=fm/i;}}}//m,n的最大公约数,假定m、n的最大公约数是v,则它们的最小公倍数就是m*n/v。

int fun (int m,int n){int k;if(m>n)k=n;else k=m;for(;k>0;k--)if(m%k==0&&n%k==0)return k;return 1;}程序代码:#include<iostream.h>#include<stdlib.h>#include<math.h>class rationalNumber{int mother,child;public:rationalNumber(int,int);rationalNumber(){}rationalNumber operator+(rationalNumber t);rationalNumber operator-(rationalNumber t);rationalNumber operator*(rationalNumber t);rationalNumber operator/(rationalNumber t);void operator>(rationalNumber t);void operator<(rationalNumber t);void operator==(rationalNumber t);void print(){cout<<child<<"/"<<mother<<'\t';}void simple(){ int k,i;if(abs(child)<mother)k=abs(child);elsek=mother;for(i=1;i<=k;i++){if(child%i==0&&mother%i==0){child=child/i;mother=mother/i;}}}};rationalNumber::rationalNumber(int m,int c){if(m>0){mother=m;child=c;}else{if(m=0){cout<<"该分母是不合法的!"<<endl;}else{mother=-m;child=-c;}}simple();}rationalNumber rationalNumber::operator+(rationalNumber t) {rationalNumber te;te.mother=mother*t.mother;te.child=child*t.mother+mother*t.child;te.simple();return te;}rationalNumber rationalNumber::operator-(rationalNumber t) {rationalNumber te;te.mother=mother*t.mother;te.child=child*t.mother-mother*t.child;te.simple();return te;}rationalNumber rationalNumber::operator*(rationalNumber t) {rationalNumber te;te.mother=mother*t.mother;te.child=child*t.child;te.simple();return te;}rationalNumber rationalNumber::operator/(rationalNumber t){rationalNumber te;te.mother=mother*t.child;te.child=child*t.mother;te.simple();return te;}void rationalNumber::operator>(rationalNumber t){int d=child*t.mother-mother*t.child;if(d>0)cout<<child<<"/"<<mother<<">"<<t.child<<"/"<<mother<<endl;elsecout<<child<<"/"<<mother<<"<="<<t.child<<"/"<<mother<<endl; }void rationalNumber::operator<(rationalNumber t){int d=child*t.mother-mother*t.child;if(d<0)cout<<child<<"/"<<mother<<"<"<<t.child<<"/"<<mother<<endl;elsecout<<child<<"/"<<mother<<">="<<t.child<<"/"<<mother<<endl;}void rationalNumber::operator==(rationalNumber t){int d=child*t.mother-mother*t.child;if(d==0)cout<<child<<"/"<<mother<<"="<<t.child<<"/"<<mother<<endl;elsecout<<child<<"/"<<mother<<"!="<<t.child<<"/"<<mother<<endl; }void main(){rationalNumber p1(6,7),p2(4,8),p3;p1.print();p2.print();cout<<endl<<endl<<"p1+p2=";p3=p1+p2;p3.print();cout<<endl<<"p1-p2=";p3=p1-p2;p3.print();cout<<endl<<"p1*p2=";p3=p1*p2;p3.print();cout<<endl<<"p1/p2=";p3=p1/p2;p3.print();cout<<endl<<endl;p1>p2;p1<p2;p1==p3;}程序执行的结果为:7/6 4/2p1+p2=19/6p1-p2=-5/6p1*p2=14/6p1/p2=7/127/6<=4/67/6<4/67/6!=7/6Press any key to continue。

相关文档
最新文档