运算符重载实验8
- 格式:doc
- 大小:46.00 KB
- 文档页数:7
运算符重载实验报告运算符重载实验报告引言:运算符重载是C++语言中的一项重要特性,它允许用户自定义运算符的行为。
通过运算符重载,可以使得程序更加直观、简洁,并提高代码的可读性和可维护性。
本实验旨在探索运算符重载的用法和效果。
一、实验目的本实验旨在通过实际操作,深入了解运算符重载的机制和使用方法,以及运算符重载对程序设计的影响。
二、实验环境本实验使用C++编程语言,并在Visual Studio开发环境下进行实验。
三、实验过程1. 了解运算符重载的基本概念运算符重载是指通过定义函数,改变运算符的行为。
在C++中,可以通过重载运算符函数来实现运算符的重载。
运算符重载函数的命名规则为"operator 运算符",例如"operator+"表示重载加法运算符。
2. 实现运算符重载的实验示例为了更好地理解运算符重载的使用方法,我们以矩阵的加法为例进行实验。
首先,定义一个Matrix类,并重载"+"运算符。
```cppclass Matrix {private:int** data;int rows;int cols;public:Matrix(int rows, int cols) {this->rows = rows;this->cols = cols;data = new int*[rows];for (int i = 0; i < rows; ++i) {data[i] = new int[cols];}}Matrix operator+(const Matrix& other) {Matrix result(rows, cols);for (int i = 0; i < rows; ++i) {for (int j = 0; j < cols; ++j) {result.data[i][j] = data[i][j] + other.data[i][j]; }}return result;}};```在上述代码中,我们定义了一个Matrix类,其中包含矩阵的数据成员data、行数rows和列数cols。
实验9 运算符重载(1)一、实验目的1、掌握运算符重载的概念;二、实验内容1、用成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。
要求:(1)输出结果是最简分数(可以是带分数);(2)分母为1,只输出分子。
过程分析:1) 定义一个类Complex,在公共部分定义构造函数,输出函数,和运算符=、+、-、*、/的重载函数,此处运算符=可以使用系统默认的运算符=的函数。
在私有部分定义两个数据成员x和y。
2) 定义构造函数时先在类里面声明构造函数,并对参数初始化,再在类外定义构造函数,分别给x和y初始化。
3) 定义输出函数print(),对分数进行化简,采用的方法是利用for循环,分子分母均除以i,i从2增加到分子分母中更小的一个数后截止,每次增加1,在利用if else 语句,如果分子分母除以i均被整除,则说明这是的i是分子分母的公约数,分子分母均赋值为整除后的结果值,同时将i重新赋值为2,因为再求公约数是要再从i=2开始循环;否则i++,表示及进入下一个循环。
化成最简形式后在利用if else语句,判断最终结果值的分母是否为1,如果不是,则输出分数,否则只输出分子。
4) 定义运算符+重载函数,参数作为+的右操作对象,调用函数的对象作为左操作对象,在函数里定义一个Complex对象d,将左右操作对象的分数相加,将得到的结果的分子和分母分别赋给d的分子和分母,返回类的对象d。
5) 定义运算符-重载函数,参数作为-的右操作对象,调用函数的对象作为左操作对象,在函数里定义一个Complex对象d,将左右操作对象的分数相减,并将得到的结果的分子和分母分别赋给d的分子和分母,返回类的对象d。
6) 定义运算符*重载函数,参数作为*的右操作对象,调用函数的对象作为左操作对象,在函数里定义一个Complex对象d,将左右操作对象的分数相乘,并将得到的结果的分子和分母分别赋给d的分子和分母,返回类的对象d。
《C++面向对象程序设计》实验内容实验1 C++程序设计初步1.实验目的(1)了解在C++编译系统(Visual C++6.0)上如何编辑、编译、连接和运行一个C++程序。
(2)通过运行简单的C++程序, 初步了解C++源程序的结构和特点。
(3)掌握简单C++程序的编写和调试方法。
(4)掌握重载函数的定义方法。
(5)能正确使用引用型变量。
2.实验内容和步骤(1)在Visual C++环境下编译和运行C++程序①先进入Visual C++6.0环境。
②在自己指定的子目录中建立一个名为test.cpp的新文件。
③从键盘输入以下程序int main(){int a,b;c=a+b;cout>> “a+b=”>>a+b;}选择Build→Compile test.cpp命令, 对此源程序进行编译。
观察和分析编译信息。
⑤根据编译信息指出的错误, 修改程序。
再进行编译, 如果还有错, 再重复此过程, 直到编译不出错为止。
⑥选择Build→Build test.exe命令, 对程序进行连接, 如果不出错, 就会生成可执行程序test.exe。
⑦选择Build→Execute test.exe命令, 执行可执行程序test.exe。
观察屏幕的变化。
在输出窗口应显示程序运行结果。
⑧分析结果是否正确, 如果不正确或认为输出格式不理想, 可以修改程序, 然后重新执行以上④和⑧步骤。
改过后的程序:#include<iostream>using namespace std;int add(int x,int y){int z;z=x+y;return(z);}int main(){int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}实验2 C++对C的扩充(2)编一个程序, 用来求2个或3个正整数中的最大数。
Complex运算符重载实验实验报告实验名称:Complex类的运算符重载姓名:XX 学号:XXXXXXXXXX 班级XXXXXXXX 完成日期:20XX-X-XX 实验目的:这个实验题目的主要目的是让同学们掌握c++运算符重载的用法过程描述:一、本实验中主要内容有创建一个Complex类,这个类需要完成的运算符重载有:(1)+ :重载+,用来完成两个复数的加法;(2)- :重载-,用来完成两个复数的减法;(3)* :重载*,用来完成两个复数的乘法;(4)<< :重载<<,用来输出一个复数,输出的格式为(a + b*i),其中a为实部,b为虚部。
二、本实验中1、首先定义一个名为Complex的类,其友元函数有六个,包括前面提到的+,-,*,<<的重载,以及附加功能/ 和== 的重载,具体如下:friend ostream& operator<<(ostream &os, const Complex& obj); //输出重载函数friend Complex operator+(const Complex &c1, const Complex &c2); // +运算符重载friend Complex operator-(const Complex &c1, const Complex &c2); // -运算符重载friend Complex operator*(const Complex &c1, const Complex &c2); // *运算符重载//附加功能friend Complex operator/(const Complex &c1, const Complex &c2); // /运算符重载friend bool operator==(const Complex &c1, const Complex &c2); // ==运算符重载2、其中数据成员有两个,分别为real(表示实部),img(表示虚部)。
+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;}运行结果【实例编程】运行结果。
《c++面向对象程序设计》实验报告实验序号:8 实验项目名称:友元与运算符重载1:#include<iostream> using namespace std;namespace std{class fraction{private:int num; //分int deno; //分母public:fraction(){num=0;deno=0;}fraction(int a,int b);fraction reduction(fraction f);int frac(fraction f); //取整数部分double frac1(fraction f); //将分数转变成小数friend fraction operator+(fraction f,fraction k); //重载+运算符friend fraction operator-(fraction f,fraction k); //重载-运算符friend fraction operator*(fraction f,fraction k); //重载*运算符friend fraction operator/(fraction f,fraction k); //重载/运算符friend ostream& operator<<(ostream& outs,const fraction &f);friend istream& operator>>(istream& ins, fraction &f); //分子分母仅有一位*/void show();};fraction::fraction(int a,int b){num=a;deno=b;}fraction fraction::reduction(fraction a){int i;if(a.num%a.deno==0){a.num=a.num/a.deno;a.deno=1;}else{for(i=2;i<=10;i++){if(a.num%i==0){if(a.deno%i==0){a.num=a.num/i;a.deno=a.deno/i;i=i-1;}}}}return fraction(a.num,a.deno);}void fraction::show(){cout<<endl<<num<<"/"<<deno;}int fraction::frac(fraction f) //取整数部分{int a;a=f.num/f.deno;return(a);}double fraction::frac1(fraction f){double a;a=double(f.num)/double(f.deno);return(a);}fraction operator+(fraction f,fraction k){fraction one;one.num=f.num*k.deno+k.num*f.deno;one.deno=f.deno*k.deno;one=one.reduction(one);return one;}fraction operator-(fraction f,fraction k){fraction one;one.num=f.num*k.deno-k.num*f.deno;one.deno=f.deno*k.deno;one=one.reduction(one);return one;}fraction operator*(fraction f,fraction k){fraction one;one.num=f.num*k.num;one.deno=f.deno*k.deno;one=one.reduction(one);return one;}fraction operator/(fraction f,fraction k){fraction one;one.num=f.num*k.deno;one.deno=f.deno*k.num;one=one.reduction(one);return one;}ostream& operator<<(ostream& outs,const fraction &f) {outs<<f.num<<"/"<<f.deno;return(outs);}istream& operator>>(istream& ins, fraction &f){char a;cout<<endl<<"请输入分数(num/deno):";ins>>f.num>>a>>f.deno;f=f.reduction(f);return ins;}}int main(){int a;double b;fraction one(1,2),two(2,3),four,five;one.show();two.show();one=one.reduction(one);one.show();a=one.frac(one);b=one.frac1(one);cout<<endl<<"整数部分为:"<<a;cout<<endl<<"将分数转变成小数为:"<<b;four=one+two;four.show();four=one-two;four.show();four=one*two;four.show();four=one/two;four.show();cout<<endl<<four;cin>>five;cout<<five;cout<<endl;return(0);}运行结果:2:重载函数为类的成员函数:#include<iostream>using namespace std;class complex{private:int real,imag;public:complex(){real=0;imag=0;};complex(int r,int i);complex operator-(complex f);complex operator-=(complex f);complex operator*=(complex f);complex operator/=(complex f);void didplay();};complex::complex(int r,int i){real=r;imag=i;}complex complex::operator-(complex f){complex one;one.real=real-f.real;one.imag=imag-f.imag;return one;}complex complex::operator-=(complex f) {real=real-f.real;imag=imag-f.imag;return complex(real,imag);}complex complex::operator*=(complex f) {real=real*f.real;imag=imag*f.imag;return complex(real,imag);}complex complex::operator/=(complex f) {real=real/f.real;imag=imag/f.imag;return complex(real,imag);}void complex::didplay(){cout<<endl<<"real="<<real;cout<<endl<<"imag="<<imag;}int main(){complex one(1,2),two(2,3),a;a=two-one;a.didplay();one-=two;one.didplay();one*=two;one.didplay();one/=two;one.didplay();cout<<endl;return(0);}运行结果:重载函数为类的成员函数:#include<iostream>using namespace std;namespace std{class complex{private:int real,imag;public:complex(){real=0;imag=0;};complex(int r,int i);friend complex operator-(complex f,complex k);friend complex operator-=(complex &f,complex &k);friend complex operator*=(complex &f,complex &k);friend complex operator/=(complex &f,complex &k);void didplay();};complex::complex(int r,int i){real=r;imag=i;}complex operator-(complex f,complex k){complex one;one.real=f.real-k.real;one.imag=f.imag-k.imag;return one;}complex operator-=(complex &f,complex &k){return complex(f.real-=k.real,f.imag-=k.imag);}complex operator*=(complex &f,complex &k){return complex(f.real*=k.real,f.imag*=k.imag); }complex operator/=(complex &f,complex &k){return complex(f.real/=k.real,f.imag/=k.imag); }void complex::didplay(){cout<<endl<<"real="<<real;cout<<endl<<"imag="<<imag;}}int main(){complex one(1,2),two(2,3),a;a=two-one;a.didplay();one-=two;one.didplay();one*=two;one.didplay();one/=two;one.didplay();cout<<endl;return(0);}运行结果:。
运算符重载的应用【实验目的】1、理解重载运算符的意义。
2、掌握用成员函数、友元函数重载运算符的特点。
3、掌握重载运算符函数的调用方法。
【实验内容】1.定义一个复数类,通过重载运算符:*,/,直接实现二个复数之间的乘除运算。
编写一个完整的程序,测试重载运算符的正确性。
要求乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。
⑴分析两复数相乘的计算公式为:(a+b i)*(c+d i)=(ac–bd )+(ad+bc) i两复数相除的计算公式为:(a+b i)/(c+d i)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d) i复数类及运算符重载函数可定义为:2.根据下面描述定义完整的日期类:class Date{//成员变量void IneDay();//日期增加1天int DayCalc()const;//距基准日期的天数static const int day[];//每月的天数public:Date(int y,int m,int d);Date(int m, int d);Date();void SystmDate();//读取系统当前时间void SetDate(int yy,int mm,int dd);//设置日期void SetDate(int mm,int dd);bool IsLeapYear(int yy) const;//是否闰年bool IsEndofMonth()const ;//是否月末//重载cout<<,cout>>完成读入和输出日期工作//重载+,+=,-,-=,++,--,等运算符,完成日期的运算符操作。
//成员函数:判读一个日期是否是系统的当前日期//实现从键盘读入你的生日,如果是则显示:生日快乐,否则先生还有多少天到生日,或你的生日已经过了多少天,明年生日要再等多少天。
选作,实现:在文件中读入事先写入的亲朋好友的纪念日,系统到时自动提醒。
实验八运算符重载
实验目的
理解重载运算符的作用,学会对典型的运算符进行重载。
实验内容
1.编写程序重载字符串运算符+、<分别用于字符串的拼接、比较运算,实现字
符串直接操作。
其中<运算符重载函数为友元函数,而+运算符重载为成员函数。
2.编写一个矩形类rect,分别采用友元函数的方式重载和成员函数的方式重载
运算符<、>、==,用于比较两个矩形面积是否相等。
3.在point类中定义一个运算符函数,用于两个对象a和b相加,其结果为一
个point类对象c,c中的x和y分别为对象a和b的x和y的和。
(+操作符作为成员函数重载)
4.在point类中定义一个运算符函数,用于两个对象a和b相加,其结果为一
个point类对象c,c中的x和y分别为对象a和b的x和y的和。
(+操作符作为友元函数重载)
5.以下程序通过重载运算符+、*实现集合(用数组表示)的并(∪)、交(∩)运算。
集合中的元素不能相同。
两个集合的并包含了两个集合的所有元素。
两个集合的交仅包含两个集合中共同存在的元素。
设s1={1,2,3,4,5,6},s2={3,
6.定义一个人民币类RMB,包含私有数据成员元、角、分,请用友元函数重。
《面向对象程序设计》实验七运算符重载(电信系教师陈小常)一、实验目的1、掌握用成员函数重载运算符的方法2、掌握用友元函数重载运算符的方法二、实验内容1.重载自增运算符修改下面程序中的错误,添加成员函数的定义重载自增运算符,并调试输出运行结果。
#include<iostream>using namespace std;class counter{public;Counter() {v=0;}Counter opretor ++()Counter opretor ++(int)void print() {cout<<v<<endl;}private:unsingned v;};int main(){Counter c;c.print();(c++).print();c.print();++c;c.print();}//修改下面程序中的错误,添加成员函数的定义重载自增运算符,并调试输出运行结果。
#include<iostream>using namespace std;class counter{public:counter() {v=0;}counter operator ++()//前缀{++v;return *this;}counter operator ++(int)//后缀{counter te(*this);v++;return te;}void print(){cout<<v<<endl;}private:unsigned v;};int main(){counter c;c.print();(c++).print();c.print();++c;c.print();return 0;}2.、重载加、赋值运算符已知Mydatatype类的定义如下,添加成员函数的定义重载加或者赋值运算符,并调试输出运行结果:# include <iostream.h>class mydatatype{protected:int mndata;public:mydatatype();mydatatype & operator=(int);mydatatype & operator=(mydatatype & );mydatatype & operator+(mydatatype & );operator int(){return mndata;}};。
int main(){mydatatype obj1,obj2,obj3;obj1=10;obj2=obj1;obj3=obj1+obj2;int nvalue=(int)obj3;cout<<"nvalue="<<nvalue<<endl;return 0;}//已知Mydatatype类的定义如下,添加成员函数的定义重载加或者赋值运算符,并调试输出运行结果:# include <iostream.h>class mydatatype{protected:int mndata;public:mydatatype();mydatatype & operator=(int);mydatatype & operator=(mydatatype & );mydatatype& operator+(mydatatype & );operator int(){return mndata;}void print(){cout<<mndata<<endl;}};mydatatype::mydatatype(){mndata=0;}mydatatype& mydatatype::operator=(int a){mndata=a;return *this;}mydatatype& mydatatype::operator=(mydatatype & p){mndata=p.mndata;return *this;}mydatatype& mydatatype::operator+(mydatatype & m){mndata=mndata+m.mndata;return *this;}int main(){mydatatype obj1,obj2,obj3;obj1=10;//obj1.print();obj2=obj1;//obj2.print();obj3=obj1+obj2;int nvalue=(int)obj3;cout<<"nvalue="<<nvalue<<endl;return 0;}3、建立类StrType,允许下面的操作符类型:(1) 使用+操作符的字符串连接;(2) 使用=操作符的字符串赋值;(3) 使用<、>==操作符的字符串比较。
可以自由使用定长字符串。
修改下面程序中的错误,添加成员函数定义重载+、=、<、>、==运算符,并调试输出运行结果。
#include < iostream.h>#include < string.h>class StrType{public:StrType(){ *s='\0';)StrType(char * p){ strcpy(s,p);}char * Get(){return s;}StrType oprator + (StrType& s2);StrType& oprator = (strType& s2);int operator < (StrType& s2);int operator > (StrType& s2);int operator == (StrType& s2);private:char s[80];}。
void main(){StrType o1("Hollo"),o2("There"),o3;cout << "o1= " << o1.Get() << endl;cout << "o2= " << o2.Get() << endl;o3 = o1 + o2;cout << "o3= o1+o2= " << o3.Get() << endl;o3 = o1;cout << "o1 = o3 \ n";if(o1==o3)cout << "o1 equals o3 \ n";if(o1>o2)cout << "o1 > o2 \ n";if(o1<o2)cout << "o1 < o2 \ n";}/*3、建立类StrType,允许下面的操作符类型:(1) 使用+操作符的字符串连接;(2) 使用=操作符的字符串赋值;(3) 使用<、>==操作符的字符串比较。
可以自由使用定长字符串。
修改下面程序中的错误,添加成员函数定义重载+、=、<、>、==运算符,并调试输出运行结果。
*/#include < iostream.h>#include < string.h>class StrType{public:StrType(){ *s='\0';}StrType(char *p){ strcpy(s,p);}char *Get(){return s;}StrType operator+(StrType& s2);StrType& operator=(StrType& s2);friend int operator<(StrType& s2,StrType& s3);friend int operator>(StrType& s2,StrType& s3);friend int operator==(StrType& s2,StrType& s3);private:char s[80];};StrType StrType:: operator +(StrType& s2) {StrType str;strcpy(str.s,s);strcat(str.s,s2.s);return str;}StrType& StrType::operator=(StrType &s2) {strcpy(s,s2.s);return *this;}int operator<(StrType& s2,StrType& s3) {if(strcmp(s2.s,s3.s)<0)return 1;elsereturn 0;}int operator > (StrType& s2,StrType& s3) {if(strcmp(s2.s,s3.s)>0)return 1;elsereturn 0;}int operator==(StrType& s2,StrType& s3) {if(strcmp(s2.s,s3.s)==0)return 1;else return 0;}void main(){StrType o1("Hollo"),o2("There"),o3;cout << "o1= " << o1.Get() << endl;cout << "o2= " << o2.Get() << endl;o3 = o1 + o2;cout << "o3= o1+o2= " << o3.Get() << endl;o3 = o1;cout << "o1 = o3 \ n";if(o1==o3)cout << "o1 equals o3 \ n";if(o1>o2)cout << "o1 > o2 \ n";if(o1<o2)cout << "o1 < o2 \ n";}void main(){StrType o1("Hollo"),o2(" boy and girl"),o3;cout<< "o1= " << o1.Get() << endl;cout<< "o2= " << o2.Get() << endl;o3 = (o1 + o2);cout<<"o3= o1+o2= "<<o3.Get()<<endl;o3 = o1;cout<< "o1 = o3 \n";if(o1==o3)cout<< "o1 equals o3 \n";if(o1>o2)cout<< "o1 > o2 \n";if(o1<o2)cout<< "o1 < o2 \n";}。