C++程序设计运算符重载实验报告

合集下载

C++实验三运算符重载

C++实验三运算符重载
char sex;
float score;
};
Student::Student(int n,char nam[],char s,float so)
{num=n;
strcpy(name,nam);
sex=s;
score=so;
}
class Teacher
{public:
Teacher(){}
Teacher(Student&);
{
ARRAY c;
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
c.array[i][j]=a.array[i][j]+b.array[i][j];
cout<<c.array[i][j];
cout<<" ";
}
cout<<endl;
}
return c;
}
/*COMPLEXoperator-(COMPLEX a,COMPLEX b)
cout<<"student1:"<<endl;
student1.display();
teacher2=Teacher(student1);
cout<<"teacher2:"<<endl;
teacher2.display();
return 0;
}
三、实验记录及结果
1.任务1
#include <iostream>
using namespace std;

实验12 运算符重载

实验12 运算符重载

实验12 运算符重载(2)牛旭艳智能二班 20110807201一、实验目的1、进一步理解运算符重载,运用成员函数和友元函数等方法实现运算符的重载。

二、实验内容1、重载函数调用运算符(),将以下的二维数组的下标表示方法:chessBoard[row][column]改为常用的表示方法:chessBoard(row,column)2、重载下标运算符使之返回集合中最大的元素、次最大的元素以及第三大的元素。

3、开发多项式类Polynomial,多项式的每一项用数组表示,每项包含一个系数和一个指数。

例如:2x4的指数为4,系数为2。

请开发一个完整的Polynomial类,包括构造函数、析构函数以及"get"函数和"set"函数。

该类还要提供下述重载的运算符(分别使用成员函数和友元函数):1)重载加法运算符+,将两个多项式相加;2)重载减法运算符-,将两个多项式相减;3)重载赋值运算符=,将一个多项式赋给另外一个多项式;4)重载乘法算符*,将两个多项式相乘;5)重载加法赋值运算符+=、减法赋值运算符-=以及乘法赋值运算符*=。

4.设计一个日期类Date,,要求:(1)包含年(year)、月(month)和日(day)私有数据成员。

(2)包含构造函数,重载关于一日期加上天数的加法运算符+、重载关于一日期减去天数的减加运算符-、重载输出运算符<<与输入运算符>>等。

提示:由于各C++编译器对于重载输入/出运算符为友元的兼容性都存在问题,最好重载输入/出运算符不声明为成员函数与友元函数,而声明一般函数,为编程更方便,可增加一些成员函数,比如:void SetYear(int y); // 设置年int SetMonth(int m); // 设置月int SetDay(int d); // 设置日int GetYear() const; // 返回年int GetMonth() const; // 返回月int GetDay() const; // 返回日static int IsLeapyear(int y); // 判断年份y是否为润年static int GetDays(int y); // 年份y的天数static int GetDays(const Date &d); // 日期d当前月份的天数static int DateToNum(const Date &d); // 返回从公元1年1月1日起的天数static Date NumToDate(int n); //由从公元1年1月1日起的天数返回日期润年条件:年份能被4整除,并且年份不能被100整除,或者年份能被400整除润年天数:366平年天数:365润年2月份天数:29平年2月份天数:285.设计一个时间类Time,要求:(1)包含时(hour)、分(minute)和秒(second)私有数据成员。

运算符重载实验报告

运算符重载实验报告

运算符重载实验报告运算符重载实验报告引言:运算符重载是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。

C++程序设计实验 赋值运算符重载

C++程序设计实验 赋值运算符重载

《C++程序设计》实验报告准考证号xxxxxx题目:赋值运算符重载姓名xxx 日期xxx 实验环境: Visual C++ 6.0- 1 -- 2 -实验内容与完成情况实验目的:1,掌握同类对象相互赋值的原理2,掌握运算符重载的意义3,掌握赋值运算符重载的一般格式实验内容:1,定义一个类,并实现对象产生时动态分配内存功能给类的定义形式给出构造函数和析构函数定义形式给出赋值运算符重载定义2,在类中定义相关的其他函数,并实现数据存入和显示功能3,设计main 函数定义多个对象,并实现多个对象间的相互赋值,对对象中数据进行输出显示并输出结果源程序代码:#include <iostream>using namespace std;class my{ char *str;int l;public:my(char *s){ l=strlen(s)+1;str=new char[l];strcpy(str,s);cout<<"动态分配"<<l<<"个内存单元"<<endl;}int size(){ return l; }void show(){ cout<<"对象中存放的字符串是:"<<str<<endl; }~my(){ delete str; cout<<"释放"<<l<<"个内存单元"<<endl;}my & operator=(const my &s);my& operator=(char *s);};my & my:: operator=(const my &s){ if( &s==this ) return *this;delete str;l=strlen(s.str)+1;str=new char[l];strcpy( str, s.str ); cout<<"对象间赋值"<<endl;return *this;}my & my:: operator=(char *s)出现的问题解决方案(列出遇到的问题和解决办法,列出未解决的问题)- 3 -。

[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;}运行结果【实例编程】运行结果。

运算符重载实验报告

运算符重载实验报告

一、实验目的1. 理解运算符重载的概念和原理。

2. 掌握C++中运算符重载的方法和规则。

3. 通过实例,实现自定义类型对运算符的重载。

4. 分析运算符重载在实际编程中的应用和优势。

二、实验环境1. 编程语言:C++2. 开发环境:Visual Studio 20193. 操作系统:Windows 10三、实验内容1. 运算符重载的概念和原理2. 运算符重载的方法和规则3. 自定义类型运算符重载实例4. 运算符重载的实际应用四、实验步骤1. 概念和原理运算符重载是指为已有的运算符赋予新的功能,使其能够应用于自定义类型的数据。

在C++中,运算符重载可以通过成员函数或友元函数实现。

2. 方法和规则- 成员函数重载:在自定义类型中定义一个成员函数,该函数的名称与要重载的运算符相同。

- 友元函数重载:在自定义类型外部定义一个友元函数,该函数的名称与要重载的运算符相同,并在函数声明中添加类名和作用域解析运算符。

运算符重载规则:- 运算符重载的函数必须返回与操作数相同的类型。

- 运算符重载的函数不能改变原有运算符的操作数个数。

- 运算符重载的函数不能改变原有运算符的优先级。

- 运算符重载的函数不能改变原有运算符的结合性。

3. 自定义类型运算符重载实例假设我们有一个自定义类型`Point`,表示二维平面上的一个点,其坐标为`(x, y)`。

```cppclass Point {public:int x, y;Point(int x, int y) : x(x), y(y) {}// 成员函数重载加法运算符Point operator+(const Point& p) const {return Point(x + p.x, y + p.y);}// 友元函数重载加法运算符friend Point operator-(const Point& p1, const Point& p2);};// 实现友元函数重载减法运算符Point operator-(const Point& p1, const Point& p2) {return Point(p1.x - p2.x, p1.y - p2.y);}```4. 运算符重载的实际应用运算符重载在实际编程中具有以下优势:- 提高代码可读性:使用自定义类型时,可以像操作基本数据类型一样使用运算符,提高代码的可读性。

C++友元与运算符重载

C++友元与运算符重载

《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);}运行结果:。

c运算符重载和多态性实验报告

c运算符重载和多态性实验报告

实验5 运算符重载和多态性一、实验目的1.掌握用成员函数重载运算符的方法2.掌握用友元函数重载运算符的方法3.理解并掌握利用虚函数实现动态多态性和编写通用程序的方法4.掌握纯虚函数和抽象类的使用二、实验内容1.复数类加减法乘除运算 (用成员函数定义运算符重载)。

复数类的定义:class complex //复数类声明{ public: //外部接口complex(double r=0.0,double i=0.0) //构造函数{real=r,imag=i;}complex operator +(complex c2); //运算符"+"重载成员函数complex operator - (complex c2); //运算符"-"重载成员函数complex operator *(complex ); //运算符"*"重载成员函数complex operator /(complex); //运算符"/"重载成员函数complex operator =(complex c2); //运算符"="重载成员函数void display(); //输出复数private: //私有数据成员double real; //复数实部double imag; //复数虚部};实验代码:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display();private:double real; double imag;};Complex Complex::operator+(Complex &c2) {Complex c;c.real=real+c2.real;c.imag=imag+c2.imag; return c;}Complex Complex::operator-(Complex &c2) {Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;}Complex Complex::operator*(Complex &c2) {Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag; 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);return c;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main(){Complex c1(4,6),c2(3,7),c3;cout<<"c1=";c1.display();cout<<"c2=";c2.display();int i,j=1;while(j){cout<<"\n";cout<<"\t\t"<<"1.复数之和\n"; cout<<"\t\t"<<"2.复数之差\n"; cout<<"\t\t"<<"3.复数之积\n"; cout<<"\t\t"<<"4.复数之商\n"; cout<<"\t\t"<<"0.退出\n"; cout<<"请选择(0--4)"; cin>>i;switch(i){case 1: c3=c1+c2;cout<<"c1+c2=";c3.display(); break;case 2: c3=c1-c2;cout<<"c1-c2=";c3.display();break;case 3: c3=c1*c2;cout<<"c1*c2=";c3.display();break;case 4: c3=c1/c2;cout<<"c1/c2=";c3.display();break;case 0: j=0;break;}}}测试结果:2.复数类比较运算 (用友元函数定义运算重载)。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Complex (doubleR=0,doubleI=0):r(R), i(I){ };
Complexoperator+(Complex b);//L1复数加法
Complexoperator-(Complex b);//L2复数减法
friendComplexoperator*(Complex a,Complex b);//L3复数乘法
Ps:同时注意复数的显示。
程序设计如下:(基于vs2010运行)
// redown.cpp :定义控制台应用程序的入口点。
#include"stdafx.h"
#include<iostream>
usingnamespacestd;
classComplex {
private:
doubler, i;
public:
return1;
else
return0;
}
//重载<
booloperator<(Complex a,Complex b) {
Complex t;
t = a-b;
if((t.r<0)&&(t.i<0))
return1;
else
return0;
}
//定义复数的显示
voidComplex::display(){
c5.display();
cout<<"c6=c1/c2,所以c6的数值为";
c6.display();
cout<<"c7=c2++,所以c7的数值为";
c7.display();
cout<<"c8=c2--,所以c8的数值为";
c8.display();
cout<<"比较c8>c7?";
cout<< (c8>c7?"ture":"false");
cout<<r;
if(i>0) cout<<"+";
if(i!=0) cout<<i<<"i"<<endl;
}
voidmain(void) {
Complex c1(1,2),c2(3,4),c3,c4,c5,c6,c7,c8;
c3=c1+c2;
c4=c1-c2;
c5=c1*c2;
c6=c1/c2;
c7=c2++;
cout<<"\n";
cout<<"比较c8<c7?";
cout<< (c8<c7?"ture":"false");
system("pause");
}
三、测试数据和执行结果(在给定数据下,执行操作、算法和程序的结果,可使用数据、图表、截图等给出)
程序运行如图:
四、实验结果分析及总结(对实验的结果是否达到预期进行分析,总结实验的收获和存在的问题等)
friendbooloperator<(Complex a,Complex b);
voiddisplay();
};
//重载+ and—
Complex Complex::operator+(Complex b){returnComplex(r+b.r,i+b.i);}
Complex Complex::operator-(Complex b){returnComplex(r-b.r,i-b.i);}
friendComplexoperator/(Complex a,Complex b);//L4复数除法
friendComplexoperator--(Complex a);//L5复数自减
friendComplexoperator++(Complex a);//L6复数自增
friendbooloperator>(Complex a,Complex b);//L7复数的比较
//重载*
Complexoperator*(Complex a,Complex b){
Complex t;
t.r=a.r*b.r-a.i*b.i;
t.i=b.r*b.i+b.i*b.r;
returnt;
}
//重载/
Complexoperator/(Complex a,Complex b) {
Complex t;
专业课程实验报告
课程名称:面向对象程序设计(C++)
开课学期:2015至2016学年第1学期
专业:计算机科学与技术年级班级:13计科
学生:侯瑄学号:222013321210092
实验教师:睿
计算机与信息科学学院软件学院
实验项目名称
C++程序设计运算符重载
实验时间
2015.10.25
实验类型
□验证性□设计性□综合性
c8=c2--;
cout<<"c1的数值为";
c1.display();
cout<<"c2的数值为";
c2.display();
cout<<"c3=c1+c2,所以c3的数值为";
c3.display();
cout<<"c4=c1-c2,所以c4的数值为";
c4.display();
cout<<"c5=c1*c2,所以c5的数值为";




实验容和设计(A-E):
操作过程、算法或代码(A-E):
实验结果(A-E):
实验分析和总结(A-E):
实验成绩(A-E):
反馈评语:
一、实验目的和要求
(1)掌握通过运算符重载实现多态性的方法;
(2)学会运算符重载的成员函数法和友元函数法;
(3)能区分单目运算符的前置与后置。
二、实验容与设计(主要容,操作步骤、算法描述或程序代码)
本次实验主要实现以下容:
【项目容】
定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”“++”“--”“>”“<”,使之能用于复数的加、减、乘、除、自增、自减、比较大小。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数之和、差、积、商、自增、自减、比较大小。提示:注意复数的乘、除法计算。
returnt;
}
//重载++
Complexoperator++(Complex a){
Complex t;
Complex y(1,1);
t = a + y;
returnt;
}
//重载>
booloperator>(Complex a,Complex b) {
Complex t;
t = a-b;
if((t.r>0)&&(t.i>0))
doublex;
x=1/(b.r*b.r+b.i*b.i);
t.r=x*(a.r*b.r+a.i*b.i);
t.i=x*(a.i*b.r-a.r*b.i);
returnt;
}
//重载——
Complexoperator--(Complex a){
Complex t;
Complex y(1,1);
t = a - y;
通过此次实验我对于运算符的重载有了一定的了解认识和掌握,对于友元,二元,一元,特殊等运算符也有了清楚的认知,同时明白了这些运算符应该如何重载以及不能重载的运算符有. .* :: ?: #等几个。同时进一步的掌握了类的定义,对复数新建了Complex类,同时对新建的类进行了复数的加减比较大小等运算。对C++语言有了进一步的掌握。
相关文档
最新文档