为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

C程序设计实验

2021-08-23 2页 doc 76KB 0阅读

用户头像 个人认证

群妹仔998

现场QA工程师

举报
C程序设计实验Documentserialnumber【UU89WT-UU98YT-UU8CB-UUUT-UUT108】C程序设计实验实验6运算符重载实验目的掌握运算符重载的规则;掌握运算符成员函数与运算符友元函数的实现及应用;学会定义类中单目和双目运算符的重载函数;理解重载运算符的作用,学会对典型的运算符进行重载。实验学时本次实验需要2个学时。实验要求实验上机之前,根据实验内容要求,自行设计编写程序,完成预习报告。实验上机时调试并修正程序。当次上机结束前分析错误原因并给出实验结论,提交实验报告。实验内容1.基础部分(1)定义复数类comp...
C程序设计实验
Documentserialnumber【UU89WT-UU98YT-UU8CB-UUUT-UUT108】C程序设计实验实验6运算符重载实验目的掌握运算符重载的规则;掌握运算符成员函数与运算符友元函数的实现及应用;学会定义类中单目和双目运算符的重载函数;理解重载运算符的作用,学会对典型的运算符进行重载。实验学时本次实验需要2个学时。实验要求实验上机之前,根据实验内容要求,自行设计编写程序,完成预习。实验上机时调试并修正程序。当次上机结束前错误原因并给出实验结论,提交实验报告。实验内容1.基础部分(1)定义复数类complex,包括私有数据成员实部real和虚部image。定义该类的构造,拷贝构造,析构函数。为该类重载运算符+,-(友元函数),前置和后置++,--(成员函数),插入符和提取符<<,>>(友元函数)。在main函数里定义复数对象,测试重载的这些运算符。2.进阶部分(2)设计一个mystring类,包括数据成员char*pstr;和intlength;通过运算符重载实现字符串的输入>>、输出<<、连接+=、赋值=、关系运算(==、!=、>、<)、下标[]等运算。/*(1)定义复数类complex,包括私有数据成员实部real和虚部image。定义该类的构造,拷贝构造,析构函数。为该类重载运算符+,-(友元函数),前置和后置++,--(成员函数),插入符和提取符<<,>>(友元函数)。在main函数里定义复数对象,测试重载的这些运算符。#include#includeusingnamespacestd;classComplex{public:Complex(intreal1=0,intimage1=0):real(real1),image(image1){}~Complex(){};friendComplexoperator+(constComplex&a1,constComplex&a2);friendComplexoperator-(constComplex&a1,constComplex&a2);Complexoperator++();Complexoperator++(int);Complexoperator--();Complexoperator--(int);friendostream&operator<<(ostream&os,constComplex&a3);friendistream&operator>>(istream&is,Complex&a3);private:intreal;intimage;};Complexoperator+(constComplex&a1,constComplex&a2){returnComplex+,+;}Complexoperator-(constComplex&a1,constComplex&a2){returnComplex-,-;}ComplexComplex::operator++(){++real;++image;return*this;}ComplexComplex::operator++(int){Complexa=*this;++(*this);returna;}ComplexComplex::operator--(){--real;--image;return*this;}ComplexComplex::operator--(int){Complexa=*this;--(*this);return*this;}ostream&operator<<(ostream&os,constComplex&a3){os<<<<"+"<<<<"i";returnos;}istream&operator>>(istream&is,Complex&a3){is>>>>;returnis;}intmain(){Complexa4(4,5),a5(6,7),A,B;cout<<"a4:"<>a4;cin>>a5;cout<<"重新输入后a4:"<>、输出<<、连接+=、赋值=、关系运算(==、!=、>、<)、下标[]等运算。#include#includeusingnamespacestd;classmystring{public:mystring(char*p);~mystring(){free(pstr);}mystring&operator=(mystring&s);voidoperator+=(mystring&a){this->pstr=(char*)realloc(this->pstr,this->length++1);strcpy(this->pstr+(this->length),;}char&operator[](intn);booloperator==(constmystring&s){if(strcmp(this->pstr,==0)return1;elsereturn0;}booloperator!=(constmystring&s){if(strcmp(this->pstr,!=0)return1;elsereturn0;}booloperator<(constmystring&s){if(strcmp(this->pstr,<0)return1;elsereturn0;}booloperator>(constmystring&s){if(strcmp(this->pstr,>0)return1;elsereturn0;}friendostream&operator<<(ostream&os,constmystring&s){returnos<<<>(istream&is,mystring&s){delete[];is>>;=newchar[+1];is>>;*+='\0';returnis;}private:char*pstr;intlength;};mystring::mystring(char*p){this->pstr=(char*)malloc(strlen(p)+1);strcpy(this->pstr,p);this->length=strlen(p);}mystring&mystring::operator=(mystring&s){delete[]pstr;this->pstr=newchar[strlen+1];if(pstr)strcpy(this->pstr,;return*this;}char&mystring::operator[](intn){staticcharch=0;if(n>length||n<0){cout<<"数组越界"<>a;cout<b){cout<<"a字符串更大:"<
/
本文档为【C程序设计实验】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索