[C++]运算符重载实验报告
C++程序设计实验 友元和类运算符重载

《C++程序设计》实验报告准考证号xxxxxx题目:友元运算符重载和类运算符重载姓名xxx 日期xxx 实验环境:Visual C++ 6.0- 1 -- 2 -实验内容与完成情况实验目的:1,掌握运算符重载的必要性2,掌握友元运算符重载的一般格式3,掌握类运算符重载的一般格式4,掌握重载后的运算符的使用格式实验内容:1,设计一个类定义相关的数据成员和成员函数重载加、减运算符,实现对象的加减运算定义相关函数实现对象中数据的输出显示2,定义成员函数时,将成员函数定义在类体之外3,设计main 函数创建多个对象进行对象的加减运算,并输出运算后对象的数据并输出结果源程序代码://类运算符的重载实现#include <iostream>using namespace std;class base{double ra;double ia;public: base(double r=0,double i=0){ ra=r; ia=i;cout<<"构造base 对象"<<ra<<"+"<<ia<<"i"<<endl;}base operator +(base a){ cout<<"开始+操作->"<<endl;double r=a.ra+ra; double i=a.ia+ ia; base s(r,i);cout<<"完成+操作,实现复数相加"<<endl;return s; }base operator -(base a){ cout<<"开始-操作->"<<endl;double r=a.ra-ra; double i=a.ia-ia; base s(r,i);cout<<"完成-操作,实现复数相减"<<endl;return s; }void show(){ cout<<"对象中存放的复数值为:"<<ra<<"+"<<ia<<"i"<<endl; }}; void main(){ base c1(2,6),c2(3,2),c3,c4;c3=c2.operator+(c1); c3.show();c4=c1+c2;c4.show();出现的问题解决方案(列出遇到的问题和解决办法,列出未解决的问题)- 3 -。
实验14 运算符重载c【VIP专享】

请按下列步骤操作,体会什么是运算符重载,理解实现运算符使用的两种
基本方法:
(1) 复制下列代码到开发工具并加以运行,观察整数的+、-运算是否成功?
#include <iostream>
using namespace std;
class Complex
{
private: double real,image;
//输出减的结果
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,通系电1,力过根保管据护线生高0不产中仅工资2艺料22高试2可中卷以资配解料置决试技吊卷术顶要是层求指配,机置对组不电在规气进范设行高备继中进电资行保料空护试载高卷与中问带资题负料2荷试2,下卷而高总且中体可资配保料置障试时2卷,32调需3各控要类试在管验最路;大习对限题设度到备内位进来。行确在调保管整机路使组敷其高设在中过正资程常料1工试中况卷,下安要与全加过,强度并看工且25作尽52下可22都能护可地1关以缩于正小管常故路工障高作高中;中资对资料于料试继试卷电卷连保破接护坏管进范口行围处整,理核或高对者中定对资值某料,些试审异卷核常弯与高扁校中度对资固图料定纸试盒,卷位编工置写况.复进保杂行护设自层备动防与处腐装理跨置,接高尤地中其线资要弯料避曲试免半卷错径调误标试高方中等案资,,料要编试求5写、卷技重电保术要气护交设设装底备备置。4高调、动管中试电作线资高气,敷料中课并设3试资件且、技卷料中拒管术试试调绝路中验卷试动敷包方技作设含案术,技线以来术槽及避、系免管统不架启必等动要多方高项案中方;资式对料,整试为套卷解启突决动然高过停中程机语中。文高因电中此气资,课料电件试力中卷高管电中壁气资薄设料、备试接进卷口行保不调护严试装等工置问作调题并试,且技合进术理行,利过要用关求管运电线行力敷高保设中护技资装术料置。试做线卷到缆技准敷术确设指灵原导活则。。:对对在于于分调差线试动盒过保处程护,中装当高置不中高同资中电料资压试料回卷试路技卷交术调叉问试时题技,,术应作是采为指用调发金试电属人机隔员一板,变进需压行要器隔在组开事在处前发理掌生;握内同图部一纸故线资障槽料时内、,设需强备要电制进回造行路厂外须家部同出电时具源切高高断中中习资资题料料电试试源卷卷,试切线验除缆报从敷告而设与采完相用毕关高,技中要术资进资料行料试检,卷查并主和且要检了保测解护处现装理场置。设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
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 实验上机实验报告(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运算符重载

C++实验5运算符重载
实验5 运算符重载
一、实验目的
1.理解运算符重载概念;
2.学会使用运算符重载;
3.练习类和对象的使用。
二、实验内容
设计一个日期类Date,包括年、月、日等私有数据成员。
要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。
三、实验要求
在Date类中设计如下重载运算符函数:
Date operator+(int days):返回某日期加上天数得到的日期
Date operator-(int days):返回某日期减去天数得到的日期int operator-(Date&b):返回两日期相差的天数
在实现这些重载运算符函数时调用以下私有成员函数:
leap(int):判断指定的年份是否为闰年
dton(Date &):将指定日期转换成从0年O月O日起的天数
ntod(int):将指定的0年O月O日起的天数转换成对应的日期
提示:可定义一个二维数组,存放闰年和非闰年每个月的天数,用于后面的计算,int day_tab[2][12]={{31,28,31,30,3l,30,3l,3l,30,31,30,31),
{31,29,31,30,31,30,31,31,30,31,30,31}};
// day_tab二维数组存放各月天数,第一行对应非闰年,第二行对应闰年。
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、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
+operator+(const COMPLEX &other): COMPLEX
+operator-(const COMPLEX &other) : COMPLEX
+operator-(): COMPLEX
+operator=(const COMPLEX &other) : COMPLEX
运行结果
2. 程序的类结构图为:
T
x,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 const
Point
x,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 const
Circle
radius: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;
}
运行结果
【实例编程】
运行结果。