运算符重载—C++课程实验报告
C++实验3

4、熟练掌握一般运算符重载的方法。
二、多态与虚函数
1、理解静态连编和动态连编,理解多态的概念。
2、理解虚函数在类的继承层次中的作用及虚函数的引入对程序运行时,能够对使用虚函数的简单程序写出程序结果。
3、了解虚函数对多态性的支持。
4、掌握函数和纯虚函数的概念。
附录
(源程序清单,只要求有必要的关健代码,最好用图说明,一定不要有系统自动生成的代码。代码页数不能超过2页)
2.编写一个时间类Time,包含时、分、秒等数据成员,实现时间的加、减、输入和输出操作。其中加减通过重载相应运算符来实现。
#include<iostream.h>
class Time
{
private:int hour,min,s;
P 37 1.分析以下程序的错误,分析错误原因改正。
2.下面的shape类是一个表示形状的抽象类,area()为求图形面积的函数。请从shape类派生三角形类triangle和圆类circle,并给出具体的求面积函数。
#include<iostream>
Using namespace std;
Class shape
cin>>a>>b>>c;
cout<<"输入圆的半径:";
cin>>r;
triangle trian(a,b,c);
circle cir(r);
trian.disp();
cir.disp();
return 0;
}
注:实验报告一定要双面打印,模板中字体不能改动,填入的正文要求字体是小五号、宋体,行间距是单倍行距。代码可以双行排列。
实验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++实验21 运算符重载

实验21 运算符重载一、实验目的和要求1.理解运算符重载的作用。
2.掌握实现运算符重载的方法及几种特殊运算符的重载格式。
3.进一步锻炼针对类的编程能力。
二、实验准备☞知识点1.运算符重载定义的一般格式、基本概念。
2.运算符函数重载的两种形式。
3.通过成员函数实现运算符重载的一般格式。
4.通过友元函数实现运算符重载的一般格式。
5.”++”运算符的重载。
6.”=”赋值运算符的重载。
☞课前练习题1.重载运算”+”,实现a+b运算,则。
A.a必须为对象,b可为整数或实数B.a和b必须为对象C.b必须为对象,a可为整数或实数D.a和b均可为整数或实数2.在C++中,运算符的重载有两种实现方法,一种是通过成员函数来实现,另一种则通过_________来实现。
3.不能重载的5个运算符是:______、______、______、______、_________。
4.重载赋值运算符必须通过________函数实现。
5.用成员函数实现前置”--”运算符重载的一般格式为:_______________________________。
6.用友元函数实现后置”--”运算符重载的一般格式为:_______________________________。
☞分析1.有以下程序:#include<iostream.h>class C{private:double x;public:C( ) { x=0.0; }C(double a) { x=a; }friend C operator--(C t) //行A{t.x--;return t;}void show(){cout<<x<<endl;}};void main(){C c(5.6);(--c).show(); //行Bc.show(); //行C}请回答问题:①从行A 的形式上看,这是利用________函数实现__________运算符的重载。
[C++]运算符重载实验报告
![[C++]运算符重载实验报告](https://img.taocdn.com/s3/m/29d7be84bb68a98271fefabf.png)
+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 实验上机实验报告(3)

a=i;b=j;c=l;
y=j;
}
}
private:
virtual void disp(){
double a,b,c;
cout<<"y= "<<y<<endl;
};
}
double triangle::area(){
private:
int y;
double p=(a+b+c)/2;
};int main()
double r;
3
v=0):hour(i),minute(j),second(v)
};
{}
double circle::area()
void get();
{
void display();
return r*r*PI;
Time (Time &);
}
Time operator + ( Time);
int main()
"<<minute<<" : "<<second<<endl;
class Triangle
}
{
Time Time::operator + (Time time)
public:
{
Triangle(double i=0.0,double j=0.0,double
Time t;
v=0.0):a(i),b(j),c(v)
else {
t.hour=hour+24-time.hour; } return t; }Int main() {Time time1,time2,time3; time1.get(); time2.get(); time3=time1+time2; cout<<"时间的加法结果: \n"; time3.display(); time3=time1-time
运算符重载实验报告

一、实验目的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++面向对象程序设计》实验报告实验序号: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运算符重载和多态性实验报告

实验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.复数类比较运算 (用友元函数定义运算重载)。
c++实验三实验报告

实验三实验报告1.实验目的及要求(1)掌握运算符重载的定义及实现。
(2)掌握具有运算符重载的应用。
2.实验设备计算机、Microsoft Visual C++3.实验内容(1)将一个16 位二进制数表示成0 和1 的字符序列,即用一个字符数组来存放这个二进制数。
在这个类中设置两个构造函数,一个是传递整数参数的,另一个是传递字符串参数的。
因为用户在创建对象时传递的二进制数,可能是以整数形式给出,也可能是以数字串形式给出,系统应该都能接受。
另外有一个类型转换函数int(),用来将类类型向整型转换。
程序中的两个重载运算符“+”,“-”,用来完成两个二进制数之间的加减运算。
#include "iostream.h"#include "string.h"#include "conio.h"class binary { //定义二进制类char bits[16]; //二进制字模数组public:binary(char *); //字符串参数构造函数binary(int); //整型参数构造函数friend binary operator +(binary,binary); //重载“+”friend binary operator -(binary,binary); //重载“-”operator int(); //类类型转换函数void print();};binary::binary(char *num){int isrc=strlen(num)-1; //字符串长度-1 为最低位int idest=15;while(isrc>=0 && idest>=0)bits[idest--]=(num[isrc--]=='0'?'0':'1'); // 逐位赋值while(idest>=0) bits[idest--]='0'; // 空高位值0}binary::binary(int num){for(int i=15; i>=0;i--){ bits[i]=( (1) ); //求余数num>>=1; //移位,相当于整除2}}binary operator +(binary n1, binary n2){unsigned carry=0;unsigned value;binary res="0";for(int i=15; i>=0; i--){value=(n1.bits[i]=='0'?0:1)+( (2) )+carry;res.bits[i]=(value%2==0?'0':'1');carry=value>>1;}return res;}binary operator -(binary n1, binary n2){unsigned borrow=0;int value;binary res="0";for(int i=15; i>=0; i--){value=(n1.bits[i]=='0'?0:1)-(n2.bits[i]=='0'?0:1)+borrow; res.bits[i]=(value==-1||value==1?'1':'0');borrow=(value==-1||borrow!=0&&(value==0||value==1)?1:0); }return res;}binary::operator int(){unsigned value=0;for(int i=0; i<=15; i++)value=( (3) )+(bits[i]=='0'?0:1);return value;}void binary::print(){char str[17];strncpy(str,bits,16);str[16]='\0';cout<<str<<"\n";}main(){binary n1="1011";binary n2=int(n1)+15;binary n3=n1-binary(7);n1.print();n2.print();n3.print();cout<<int(n2)+5<<endl;cout<<n2-binary(5)<<endl;cout<<n3+binary(5)<<endl;cout<<int(n3)-5<<endl;getch();return 1;}[基本要求]♉ 阅读下列程序,根据题意要求在处填上合适的内容完成程序。
C++程序设计运算符重载实验报告

二、实验内容与设计(主要内容,操作步骤、算法描述或程序代码)
本次实验主要实现以下内容:
【项目内容】
定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”“++”“--”“>”“<”,使之能用于复数的加、减、乘、除、自增、自减、比较大小。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数之和、差、积、商、自增、自减、比较大小。 提示:注意复数的乘、除法计算。
//重载*
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;
专业:计算机科学与技术年级班级:13计科
学生姓名:侯瑄学号:222013321210092
实验教师:陈睿
计算机与信息科学学院软件学院
实验项目名称
C++程序设计运算符重载
实验时间
2015.10.25
实验类型
□验证性□设计性□综合性
一、实验目的和要求
(1)掌握通过运算符重载实现多态性的方法;
(2)学会运算符重载的成员函数法和友元函数法;
cout<<r;
if(i>0) cout<<"+";
if(i!=0) cout<<i<<"i"<<endl;
实验四-运算符重载-实验报告

实验报告实验目的:掌握运算符重载的语法要点,学会运算符重载的编程方法。
实验内容:(1)先读程序,预测程序的输出结果,再运行程序验证程序的输出。
用友元重载方式重新编写次程序。
#include <iostream.h>class V ector{ public:V ector(){}V ector(int i,int j) {x=i;y=j;}friend V ector operator+=(V ector v1,V ector v2){ v1.x+=v2.x;v1.y+=v2.y;return v1;}V ector operator-=(V ector v){ V ector temp;temp.x=x-v.x;temp.y=y-v.y;return temp;}void display(){ cout<<"("<<x<<","<<y<<")"<<endl;}private:int x,y;};void main(){V ector v1(1,2),v2(3,4),v3,v4;v3=v1+=v2;v4=v1-=v2;cout<<"v1=";v1.display();cout<<"v2=";v2.display();cout<<"v3=";v3.display();cout<<"v4=";v4.display();}运行结果:v1=(1,2)v2=(3,4)v4=(-2,-2)Press any key to continue用友元重载方式重新编写次程序:#include <iostream>using namespace std;class V ector{public:V ector(int i = 0,int j = 0){x=i;y=j;}friend V ector operator+=(V ector &v1,V ector &v2) {v1.x+=v2.x;v1.y+=v2.y;return v1;}friend V ector operator-=(V ector &v1,V ector &v2){v1.x=v1.x-v2.x;v1.y=v1.y-v2.y;return V ector( v1.x, v1.y);}void display(){cout<<"("<<x<<","<<y<<")"<<endl;}private:int x,y;};void main(){V ector v1(1,2),v2(3,4),v3,v4;v3 = v1+=v2;v4 = v1-=v2;cout<<"v1="; v1.display();//1,2cout<<"v2="; v2.display();//3,4cout<<"v3="; v3.display();//4,6cout<<"v4="; v4.display();//1,2}运行结果:v2=(3,4)v3=(4,6)v4=(-2,-2)Press any key to continue(2)定义一个有理数类,重载比较运算符.写一个完整的程序,进行数据成员的设置和输出。
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:定义一个向量(一维数组)类,通过重载运算符实现向量之间的加法和减法。
c++课程设计---重载运算符编程

实验课程名称 C++面向对象语言编程设计题目重载运算符编程专业班级计算机科学与技术0806401班面向对象课程设计报告一、设计时间2011年6月 13日-----6月17日二、设计地点湖南城市学院实验楼计算机机房三、设计目的1、《面向对象程序设计》是计算机科学与技术专业的必修专业基础课程,其实践性、应用性很强。
实践教学环节是必不可少的一个重要环节。
面向对象程序设计的设计目的是加深对理论教学内容的理解和掌握,使学生较系统地掌握程序设计及其在网络开发中的广泛应用,基本方法及技巧,为学生综合运用所学知识,利用软件工程为基础进行软件开发、并在实践应用方面打下一定基础。
2、要求学生在设计指导教师的帮助下自行完成各个操作环节,并能实现且达到举一反三的目的,完成一个项目解决一类问题。
3、要求学生能够全面、深入理解和熟练掌握所学内容,并能够用其分析、设计和解答类似问题;对此能够较好地理解和掌握,能够进行简单分析和判断;能编写出具有良好风格的程序;掌握面向对象程序设计的基本技能和面向对象的概念和方法。
4、同时培养学生进行分析问题、解决问题的能力;培养学生进行设计分析、设计方法、设计操作与测试、设计过程的观察、理解和归纳能力的提高。
四、设计小组成员0806401-09冯雅文、0806401-10李敏、0806401-11孙慧群五、指导老师贾丽媛老师六、设计课题一个日期类Date,包括年、月、日等私有成员。
要求实现日期的基本运算,如一日期加上天数,一日期减去天数、两日期相差天数七、基本思路及关键问题的解决方法(一)基本思路:1:在Date类中设计如下重载运算符函数:Date operator+(int days); 返回一日期加一天数得到的日期Date operator-(int days); 返回一日期减去天数得到的日期int operator-(Date &b); 返回两日期相差的天数2:在实现这些重载运算符函数调用以下私有成员函数:leap(int); 判断指定的年份是否为闰年dton(Date &); 将指定日期转换为从0年0月0日起的天数ntod(int); 将指定的0年0月0日起的天数转换为对应的日期3:在main()中,实现日期的基本运算(二)关键问题的解决办法:1:闰年与非闰年的判断,以及各月的天数的获得问题,解决办法是设置并引用二维数组2:日期基本问题的实现问题,解决办法是在Date类中设计重载运算符函数:Date operator+(int days)、Date operator-(int days)、int operator-(Date &b),同时调用以下私有成员函数leap(int)、dton(Date &)、ntod(int)得以实现3:如何使输入的日期有效,如2011 13 34为无效日期,解决办法是在input()中设置if (month>12||month<1||day>day_tab[leap(year)][month-1]||year<=0),判断日期是否为有效日期。
运算符重载

高级程序设计语言C++(2)实验报告学号:11730121姓名:彭昆明日期:2012/11/16实验6 运算符重载1.实验目的1)进一步了解运算符重载的概念和使用方法。
2)掌握几种常用的运算符重载的方法。
3)了解转换构造函数的使用方法。
4)了解在Visual C++6.0环境下进行运算符重载要注意的问题。
2.实验内容和步骤1)定义一个复数类Complex,重载运算符“+”和“-”,使之能用于复数的加和减,分别求两个复数的和和差。
要求“+”运算符重载函数作为友元函数,“-”运算符重载作为成员函数。
源程序:#include<iostream.h>class com{private:double r;double i;public:com(){r=0;i=0;}com(double rt,double it){r=rt;i=it;}friend com operator+(com &c1,com &c2);com operator-(com &c1);void dis();};com com::operator-(com &c1){com c;c.r=r-c1.r;c.i=i-c1.i;return c;}com operator+(com &c1,com &c2){com c;c.r=c1.r+c2.r;c.i=c1.i+c2.i;return c;}void com::dis(){cout<<"("<<r<<","<<i<<")"<<endl;int main(){com c1(2,3),c2(4,6),c3,c4;c3=c1+c2;c4=c2-c1;cout<<"c1=";c1.dis();cout<<"c2=";c2.dis();cout<<"c2-c1=";c4.dis();cout<<"c1+c2=";c3.dis();return 0;}运行结果2)定义一个复数类Complex,重载运算符“+”使之能够完成复数的加法运算,参加运算的两个运算量可以都是类对象,也可以其中一个是整数,顺序任意(即c1+i,和i+c1都能实现,这里c1是复数类对象,i是整数)。
运算符重载—C++课程实验报告

using namespace std; class AB
{
public:
AB(int xx, int yy); void ShowAB(); AB& operator ++(); AB operator ++(int);
AB& operator --();
AB operator --(int);
通过这次实验我基本掌握了通过运算符重载实现多态性的方法学会了运算符重载的成员函数法和友元函数法基本能够区分单目运算符的前置与后置
C++
学生姓
名
xxx
班级
学号
xxxxxxxxx
实验项
目
实验四运算符重载
指导教师
杜之波
实验目
的和要
求
一、实验目的
(1)掌握通过运算符重载实现多态性的方法;
(2)学会运算符重载的成员函数法和友元函数法;
运算后,
A的值为:"
什+AA).ShowAB();
coutvv"
B
的值为:";
(++BB).ShowAB();
cout<v"B=A--运算后,A的值为:";
(--AA).ShowAB();
coutvv"B的值为:";
(BB--).ShowAB();
cout<<"B=--A运算后,A的值为:";
(--AA).ShowAB();
{
AB AA(0,0);
AB BB(0,0);
cout<v"A的值为:";
运算符重载-- 实验报告

南昌航空大学实验报告2011年12月1号课程名称:面向对象程序设计B 实验名称:运算符重载班级:姓名:同组人:无指导教师评定: 签名:一、实验目的理解运算符重载(非成员形式和成员形式)、学习重载几类运算符(++,=,!=,+,-,==等)。
二、实验内容应用VC++6.0的构建一个复数类Complex,试对下列几个运算符进行重载:++,=,!=,+,-,==,其中要求要有成员重载形式和友元重载形式,而且,++运算符要求实现先加和后加两种形式。
三、概要设计函数原型:class complex{private:double real;double image;public:complex(double r=0,double i=0);complex &operator+(complex &c);complex operator-(complex &c);complex operator*(complex &c);friend complex operator/(complex &c1,complex &c2);friend bool operator==(complex &c1,complex &c2);friend bool operator!=(complex &c1,complex &c2);complex operator++();complex operator++(int);void show();};四、详细设计重载运算符 + 的函数:complex &complex::operator+(complex &c){complex temp;temp.real=real+c.real;temp.image=image+c.image;return temp;}重载运算符 - 的函数:complex complex::operator-(complex &c){complex temp;temp.real=real-c.real;temp.image=image-c.image;return temp;}重载运算符 * 的函数:complex complex::operator*(complex &c){complex temp;temp.real=real*c.real;temp.image=image*c.image;return temp;}重载运算符 / 的函数:complex operator/(complex &c1,complex &c2){complex temp;double t;t=1/(c2.real*c2.real+c1.image*c1.image);temp.real=(c1.real*c2.real+c1.image*c2.image)*t;temp.image=(c2.real*c1.image-c1.real*c2.image)*t;return temp;}重载运算符 == 的函数:bool operator==(complex &c1,complex &c2){if(c1.real==c2.real&&c1.image==c2.image)return true;elsereturn false;重载运算符 != 的函数:bool operator!=(complex &c1,complex &c2){if(c1.real!=c2.real||c1.image!=c2.image) return true;elsereturn false;}重载运算符 ++ 的函数:complex complex::operator++(){++real;++image;return *this;}complex complex::operator++(int){real++;image++;return *this;}五、程序调试经调试无误后,运行的结果为:六、实验总结通过这次的试验,我明显的感觉到自己对这方面的知识掌握的还不够熟练,不能顺利地、流畅地运用这方面的知识,因为我没有在规定的时间内完成程序的设计,课后还是要多复习。
运算符重载实验报告

RMB::RMB(unsigned int d, unsigned int c)
{
yuan = d;
jf = c;
while ( jf >=100 ){ //以使构造时,确保角分值小于100
yuan ++;
jf -= 100;
}
}
RMB operator+(RMB& s1, RMB& s2) // 此处改为RMB& operator+(RMB& s1, RMB& s2)
private:
unsigned int yuan; //元
unsigned int jf; //角分
};
RMB RMB::interest(double rate)
{
return RMB((yuan + jf / 100.0) * rate);
}
RMB RMB::add(RMB d)
{
return RMB(yuan + d.yuan + jf / 100.0 + d.jf / 100.0);
expense2(x,yrate).display();
}
3.实验结果:
(二)实验题目二:
(2) 将以下程序中重载运算符定义函数的返回类型更改(值返回更改为引用返回,引用
返回更改为值返回),观察程序运行结果,说明原因。
#include<iostream.h>
class RMB{
public:
RMB(unsigned int d, unsigned int c);
学 号:
指导教师:
2013年11月18日
c++运算符重载

淮海工学院计算机科学系实验报告书课程名:《 C++程序设计(二)》题目:多态性与虚函数班级:软件102班学号:111003215姓名:鹿迅1. 实验内容或题目(1)教材2、3、4。
2. 实验目的与要求(1)进一步了解运算符重载的概念和使用方法(2)掌握常用的运算符重载的方法(3)了解在VC++6.0环境下进行运算符重载要注意的问题3、实验步骤与源程序⑴实验步骤1.首先看懂书本上的例题,研究其要点和思想实质;2.看懂题目在考察什么知识点;3.根据书本上的例题,写出代码,并根据题意做适当修改;4.运行,查找错误。
⑵源代码P343_2#include<iostream>using namespace std;class Complex{public:Complex(){real=0;image=0;}Complex(double r,double i){real=r;image=i;}Complex operator+(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2);Complex operator/(Complex &c2);void set();void get();void display();private:double real;double image;};ComplexComplex::operator+(Complex &c2) {Complex c;c.real=real+c2.real;c.image=image+c2.image;return c;}ComplexComplex::operator-(Complex &c2) {Complex c;c.real=real-c2.real;c.image=image-c2.image;return c;}ComplexComplex::operator*(Complex &c2) {Complex c;c.real=real*c2.real-image*c2. image;c.image=image*c2.real+real*c2 .image;return c;}ComplexComplex::operator/(Complex &c2) {Complex c;c.real=(real*c2.real+image*c2 .image)/(c2.real*c2.real+c2.imag e*c2.image);c.image=(image*c2.real-real*c2.image)/(c2.real*c2.real+c2.ima ge*c2.image);return c;}void Complex::display(){cout<<"("<<real<<","<<image<< "i)"<<endl;}int main(){Complexc1(1,1),c2(1,-1),c3,c4,c5,c6;c3=c1+c2;c4=c1-c2;c5=c1*c2;c6=c1/c2;cout<<"c3=";c3.display(); cout<<"c4=";c4.display(); cout<<"c5=";c5.display(); cout<<"c6=";c6.display();return 0;}P343_3#include<iostream.h>class Complex{public:Complex(){real=0;image=0;}Complex(doubler ){real=r;image=0;}Complex(double r,double i){real=r;image=i;}friend Complex operator+(Complex &c1,Complex &c2);void display();private:double real;double image;};Complex operator+(Complex &c1 ,Complex &c2){returnComplex(c1.real+c2.real,c1.image +c2.image);}void Complex::display(){cout<<"("<<real<<","<<image<< "i)"<<endl;}int main(){Complexc1(1,1),c2(1,-1),c3,c4,c5 ;c3=c1+ Complex(3);c4=c1+c2;cout<<"c1+Complex(3)=";c3.display();cout<<"c1+c2="; c4.display();return 0;}P343_4#include<iostream>using namespace std;class jz{public:jz(){aa=0;bb=0;cc=0;dd=0;ee=0 ;ff=0;}jz(double a,double b,double c,double d,double e,double f){aa=a;bb= b;cc=c;dd=d;ee=e;ff=f;}jz operator+(jz &j2);void display();private:double aa,bb,cc,dd,ee,ff,gg ; };jz jz::operator+(jz &j2){jz j;j.aa=aa+j2.aa;j.bb=bb+j2.bb;=cc+;j.dd=dd+j2.dd;j.ee=ee+j2.ee;j.ff=ff+j2.ff;return j;}void jz::display(){cout<<"["<<aa<<" "<<bb<<" "<<cc<<" "<<dd<<" "<<ee<<" "<<ff<<"]"<<endl;}int main(){jza(1,2,3,4,5,6),b(2,3,4,5,6,7),c;c=a+b;cout<<"a+b=";c.display();return 0;}4、测试数据与实验结果(可以抓图粘贴)P343_2 P343_3P343_45、结果分析与实验体会这次试验依然没有脱离书本上的知识,考察的仍然是最基本,要先从基础入手,掌握书本上的知识,再根据知识做延伸,就可以解决很多的问题。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1,重载前置运算符++、--;
2,重载后置运算符++、--;
3,主函数:申明点对象,进行前置和后置运算并显示点值。
实验内
容
实验程序设计如下:
#include<iostream>
using namespace std; class AB
{
public:
AB(int xx, int yy); void ShowAB(); AB& operator ++(); AB operator ++(int);
{
AB old=*this;
--(*this);
return old;
}
int main(void)
{
AB AA(0,0);
AB BB(0,0);
cout<v"A的值为:";
AA.ShowAB(); cout«"B的值为:";
BB.ShowAB();
coutvv"B=A++
运算后,
A的值为:";
什+AA));
coutvv"
B
的值为:";
(BB++).ShowAB();
coutvv"B=++A
运算后,
A的值为:"
什+AA).ShowAB();
coutvv"
B
的值为:";
(++BB).ShowAB();
cout<v"B=A--运算后,A的值为:";
(--AA).ShowAB();
coutvv"B的值为:";
AB& operator --();
AB operator --(int);
private:
int x1,x2;
};
AB::AB(int xx, int yy)
{
x1=xx;
x2=yy;
}
void AB::ShowAB()
{
cout<vx1vv" , "<<x2<<endl;
}
AB& AB::operator ++()
C++
学生姓
名
xxx
班级
学号
xxxxxxxxx
实验项
目
实验四运算符重载
指导教师
杜之波
实验目
的和要
求
一、实验目的
(1)掌握通过运算符重载实现多态性的方法;
(2)学会运算符重载的成员函数法和友元函数法;
(3)能区分单目运算符的前置与后置。
二、实验内容
编写如下要求的完整程序:点对象运算符重载
(1)建立点类,包含两个成员变量,分别表示横坐标和纵坐标;
{
x1++;
x2++; return *this;
}
AB AB::operator ++(int)
{
AB old=*this;
++(*this);
return old;
}
AB& AB::operator --()
{
x1--;
x2--;
return *this;
}
AB AB::operator --(int)
(BB--).ShowAB();
cout<<"B=--A运算后,A的值为:";
(--AA).ShowAB();
coutvv"B的值为:";
(--BB).ShowAB();
return 0;
}
实验结果如图:
13
p
EN++
E
E
B
Press key