欢迎来到天天文库
浏览记录
ID:41978753
大小:62.00 KB
页数:5页
时间:2019-09-05
《南昌大学C实验报告(5)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、一、实验项目名称:多态性二、实验目的1.掌握运算符重载的方法2.学习使用虚函数实现动态多态性三、实验内容1.设计Set(集合)类,Set可以包含零个或多个无序的非重复元素,元素都是int型。公共接口包含以下成员函数:创建一个Set,增加一个新元素到Set,从Set中删除一个元素,显示Set中元素等。为Set类重载+(并集)、一(差集)、*(交集)。完成后,编写相应程序测试你的设计。实验代码:#includeusingnamespacestd;classSet{unsignedintlen;intelem[100];public:Set();Set(
2、constSet&Setother);voidaddelem(intelem);voiddeleteelem(int);Setoperator*(constSet&);Setoperator+(constSet&);Setoperator・(constSet&);voidshowelem();};Set::Set():len(O){}Set::Set(constSet&Setother){for(len=0;len3、==100)cout«n插入越界";inti;for(i=0;i4、i]==e)break;if(i==len){elem[len]=e;len++;}}voidSet::deleteelem(inte)if(len==0)cout«"集合为空”;inti;for(i=0;ivlen;i++)if(elem[i]==e)break;if(i!=len)for(;i5、{for(intj=0;j6、b.addelem(elemfi]);returnb;voidSet::showelem(){for(inti=0;i7、out«nb=H;b.showelem();cout«nc=a+b=n;c.showelem();cout«"d=a*b=";d.showelem();cout«He=a-b=n;e.showelem();}实验测试:创建五个集合对象abede,为集合a添加元素12345,为集合b添加元素34567,测试重载运算符+—*,执行c=a+b;d=a*b;e=a-b,得实验结果如截图:HwF:VC++6.0EXERC51Debug51.exe"a=l2345b=34567c=a+b=3456712d=a*b=345e=a-b=l2Pressanykeytoconti8、niw.1.设i^一个类层次,其中的基类为Date,它拥有一个虚函数print,从Date类派生出EurpDate>MediumDate和LongDate类。•Date的print函数使用如下格式输出日期:10-1-2005其中10为月份,1为口,2005为年•EurpDate的print函数使用下面格式:1-10-2005•MediumDate的print函数使用下面格式:Oct.1,2005•LongDate的print函数使用下面格式:October1,2005编写相应程序测试各个类的print函数(使用Date类型的指针来调用各类的print
3、==100)cout«n插入越界";inti;for(i=0;i4、i]==e)break;if(i==len){elem[len]=e;len++;}}voidSet::deleteelem(inte)if(len==0)cout«"集合为空”;inti;for(i=0;ivlen;i++)if(elem[i]==e)break;if(i!=len)for(;i5、{for(intj=0;j6、b.addelem(elemfi]);returnb;voidSet::showelem(){for(inti=0;i7、out«nb=H;b.showelem();cout«nc=a+b=n;c.showelem();cout«"d=a*b=";d.showelem();cout«He=a-b=n;e.showelem();}实验测试:创建五个集合对象abede,为集合a添加元素12345,为集合b添加元素34567,测试重载运算符+—*,执行c=a+b;d=a*b;e=a-b,得实验结果如截图:HwF:VC++6.0EXERC51Debug51.exe"a=l2345b=34567c=a+b=3456712d=a*b=345e=a-b=l2Pressanykeytoconti8、niw.1.设i^一个类层次,其中的基类为Date,它拥有一个虚函数print,从Date类派生出EurpDate>MediumDate和LongDate类。•Date的print函数使用如下格式输出日期:10-1-2005其中10为月份,1为口,2005为年•EurpDate的print函数使用下面格式:1-10-2005•MediumDate的print函数使用下面格式:Oct.1,2005•LongDate的print函数使用下面格式:October1,2005编写相应程序测试各个类的print函数(使用Date类型的指针来调用各类的print
4、i]==e)break;if(i==len){elem[len]=e;len++;}}voidSet::deleteelem(inte)if(len==0)cout«"集合为空”;inti;for(i=0;ivlen;i++)if(elem[i]==e)break;if(i!=len)for(;i5、{for(intj=0;j6、b.addelem(elemfi]);returnb;voidSet::showelem(){for(inti=0;i7、out«nb=H;b.showelem();cout«nc=a+b=n;c.showelem();cout«"d=a*b=";d.showelem();cout«He=a-b=n;e.showelem();}实验测试:创建五个集合对象abede,为集合a添加元素12345,为集合b添加元素34567,测试重载运算符+—*,执行c=a+b;d=a*b;e=a-b,得实验结果如截图:HwF:VC++6.0EXERC51Debug51.exe"a=l2345b=34567c=a+b=3456712d=a*b=345e=a-b=l2Pressanykeytoconti8、niw.1.设i^一个类层次,其中的基类为Date,它拥有一个虚函数print,从Date类派生出EurpDate>MediumDate和LongDate类。•Date的print函数使用如下格式输出日期:10-1-2005其中10为月份,1为口,2005为年•EurpDate的print函数使用下面格式:1-10-2005•MediumDate的print函数使用下面格式:Oct.1,2005•LongDate的print函数使用下面格式:October1,2005编写相应程序测试各个类的print函数(使用Date类型的指针来调用各类的print
5、{for(intj=0;j6、b.addelem(elemfi]);returnb;voidSet::showelem(){for(inti=0;i7、out«nb=H;b.showelem();cout«nc=a+b=n;c.showelem();cout«"d=a*b=";d.showelem();cout«He=a-b=n;e.showelem();}实验测试:创建五个集合对象abede,为集合a添加元素12345,为集合b添加元素34567,测试重载运算符+—*,执行c=a+b;d=a*b;e=a-b,得实验结果如截图:HwF:VC++6.0EXERC51Debug51.exe"a=l2345b=34567c=a+b=3456712d=a*b=345e=a-b=l2Pressanykeytoconti8、niw.1.设i^一个类层次,其中的基类为Date,它拥有一个虚函数print,从Date类派生出EurpDate>MediumDate和LongDate类。•Date的print函数使用如下格式输出日期:10-1-2005其中10为月份,1为口,2005为年•EurpDate的print函数使用下面格式:1-10-2005•MediumDate的print函数使用下面格式:Oct.1,2005•LongDate的print函数使用下面格式:October1,2005编写相应程序测试各个类的print函数(使用Date类型的指针来调用各类的print
6、b.addelem(elemfi]);returnb;voidSet::showelem(){for(inti=0;i7、out«nb=H;b.showelem();cout«nc=a+b=n;c.showelem();cout«"d=a*b=";d.showelem();cout«He=a-b=n;e.showelem();}实验测试:创建五个集合对象abede,为集合a添加元素12345,为集合b添加元素34567,测试重载运算符+—*,执行c=a+b;d=a*b;e=a-b,得实验结果如截图:HwF:VC++6.0EXERC51Debug51.exe"a=l2345b=34567c=a+b=3456712d=a*b=345e=a-b=l2Pressanykeytoconti8、niw.1.设i^一个类层次,其中的基类为Date,它拥有一个虚函数print,从Date类派生出EurpDate>MediumDate和LongDate类。•Date的print函数使用如下格式输出日期:10-1-2005其中10为月份,1为口,2005为年•EurpDate的print函数使用下面格式:1-10-2005•MediumDate的print函数使用下面格式:Oct.1,2005•LongDate的print函数使用下面格式:October1,2005编写相应程序测试各个类的print函数(使用Date类型的指针来调用各类的print
7、out«nb=H;b.showelem();cout«nc=a+b=n;c.showelem();cout«"d=a*b=";d.showelem();cout«He=a-b=n;e.showelem();}实验测试:创建五个集合对象abede,为集合a添加元素12345,为集合b添加元素34567,测试重载运算符+—*,执行c=a+b;d=a*b;e=a-b,得实验结果如截图:HwF:VC++6.0EXERC51Debug51.exe"a=l2345b=34567c=a+b=3456712d=a*b=345e=a-b=l2Pressanykeytoconti
8、niw.1.设i^一个类层次,其中的基类为Date,它拥有一个虚函数print,从Date类派生出EurpDate>MediumDate和LongDate类。•Date的print函数使用如下格式输出日期:10-1-2005其中10为月份,1为口,2005为年•EurpDate的print函数使用下面格式:1-10-2005•MediumDate的print函数使用下面格式:Oct.1,2005•LongDate的print函数使用下面格式:October1,2005编写相应程序测试各个类的print函数(使用Date类型的指针来调用各类的print
此文档下载收益归作者所有