




免费预览已结束,剩余13页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
题 目 c+面向对象程序设计课程设计 清单:5小题+职工工资管理系统(类、链表实现) 姓 名: 学 号: 专 业: 计算机科学与技术 学 院: 指导教师: 2018年6月17日Part 1: 小程序练习1 类的继承定义一个point类,包含私有数据成员x,y,成员函数包括无参构造函数,带参构造函数,set和get属性函数。定义circle类,从point类公有派生,增加数据成员半径r,成员函数包括无参构造函数,带参构造函数,计算面积函数getarea。在main函数中定义一个circle的对象,并计算其面积。/*1定义Point类,设置其成员函数(构造函数,拷贝构造函数和析构函数)以及setx() sety() getx() gety() 四个属性函数。2定义circle类,设置其成员函数(构造函数,拷贝构造函数和析构函数)以及获取半径r的函数get_r() 计算面积并获取面积的函数getarea()。3在主函数中定义类的对象c1并初始化r=2。再调用getarea()函数输出面积*/#include using namespace std;class point/定义point类public:point() point(int x, int y)void set_x(int x)this-x = x;int get_x() return x;void set_y(int y)this-y = y;int get_y()return y;private:/私有对象x yint x;int y;class circle :public point/circle类公有派生pointpublic:circle() circle(double r,int x,int y):point(x,y)this-r = r;double get_r() return r;double getarea() return(3.14*r*r);private:int r;/circle私有对象r;int main()circle c1(2,3,6);coutr=c1.get_r()endl;cout 该圆面积=c1.getarea() ,功能。在main函数里测试该类。/*1.定义counter类,私有成员数据weight,设置其成员函数(构造函数和析构函数)2.重载自加自减运算符和运算符。3.在主函数中实现运算符重载。4.友元函数需要声明。*/#include#includeusing namespace std;class counter;istream& operator(istream& is,counter& a);ostream& operator(istream& is,counter& a); /声明友元,重载输入运算符friend ostream& operator(istream& in,counter& a) /运算符重载实现ina.P;if(!in)cerr输入错误!endl;return in;ostream& operator(ostream& out,counter& a) /运算符重载实现outa.P;return out;int main()counter c1(236),c2(632); coutc1=c1endlc2=c2endl;cout+c1=+c1endl; /测试coutc1+=c1+endl;coutc2-=c2-endl;cout-c2=-c2endl;system(pause);return 0;运行结果分析:定义c1的值为236,c2的值为632;此时+c1的数值为237;c1+输出时为237,输出后为238;此时c2-输出时为632;-c2输出时为630,输出后为630;3 虚函数和抽象类定义一个抽象类shape,包括公有的计算面积area函数,计算体积volume函数,输出基本信息函数printinfo(三个函数均为纯虚函数)。从shape公有派生point类,增加私有数据成员x,y坐标,以及构造函数,析构函数。从point公有派生circle类,增加私有数据成员半径r,以及构造函数,析构函数。从circle公有派生cylinder类,增加私有数据成员高度h,以及构造函数,析构函数。(在定义三个派生类的过程中,自己考虑需要重定义哪个虚函数)。在main函数中,定义shape类的指针,指向派生类的对象,输出三类对象的基本信息,面积,体积;(将shape指针改为引用再尝试)。/*1先定义基类shape。设置三个纯虚函数并且声明:声明计算面积纯虚函数area();声明计算体积纯虚函数volume();声明输出基本信息纯虚函数printinfo();2定义类point共有继承自类shape。并且在该类中实现三个纯虚函数。3定义类circle共有继承自类point。并且在该类中实现三个纯虚函数。4定义类cylinder共有继承自类circle。并且在该类中实现三个纯虚函数。5在主函数中分别创建类point的对象a,circle的对象b,cylinder的对象c,并初始化;6在主函数中分别定义shape类的指针和引用,调用printinfo()函数。*/#include #include #define Pi 3.141516using namespace std;class shapepublic:virtual double area() = 0;/三个纯虚函数:面积,体积,基本输出virtual double volume() = 0;virtual void printinfo () = 0;class point :public shape/shape派生的point类;点并没有体积面积,所以只写printinfo函数public:point() point(double x, double y)this-x = x;this-y = y;void printinfo()cout x= x ,y= y r = r;double area()return Pi*r*r;void printinfo()point:printinfo();cout 半径为 r endl;cout 圆的面积是 area() h = h;/*double area()return 2*Pi*this-r+circle:area();/未实现计算圆柱表面积*/double volume()return h*circle:area();void printinfo()circle:printinfo();cout 高为 h endl;cout 圆柱的体积是 volume() endl;cylinder() private:double h;int main()cylinder temp(6, 2, 3, 3);shape *s;/实例化一个圆柱对象s = &temp;(*s).printinfo();printf(n);cout temp中数据构成的圆面积为 area() endl;cout 体积为 (*s).volume() endl;system(pause);return 0;运行结果:4 模板编写一个使用类模板对数组进行查找、求元素和、重载下标运算符,以及输出的程序。1)设计一个类模板:形式1为templateclass Array;形似2为templateclass Array;用于对T类型的数组进行构造和输出;2)产生模板类Array和Array进行测试;3)产生模板类Array和Array进行测试。/先实现第(2)小题#include #include using namespace std;template class Arrayprivate: T* list; int size; public: Array(int size = 10); /构造函数 Array(); /析构函数 T & operator (int i); /重载” const T & operator (int i) const;T sum(int n); int search(T e,int n); int getSize() const; void resize(int sz); ;template Array:Array(int sz) /构造函数 assert(sz = 0); size = sz; list = new T size; template Array:Array() /析构函数 delete list;/重载下标运算符template T &Array:operator (int n) assert(n = 0 & n size); return listn; template const T &Array:operator (int n) const assert(n = 0 & n size); return listn; /取当前数组的大小template int Array:getSize() const return size;/ 将数组大小修改为sztemplate void Array:resize(int sz) assert(sz = 0); if (sz = size) return; T* newList = new T sz; int n = (sz size) ? sz : size; for (int i = 0; i n; i+) newListi = listi; delete list; /删除原数组 list = newList; / 使list指向新数组 size = sz; /更新sizetemplateT Array:sum(int n) T sum = list0; for(int i = 1; i n; i+) sum += listi;return sum;/查找templateint Array:search(T e, int n) for(int i = 0; i n; i+) if(listi = e) return i;return -1;int main() Array a(5); int i,n,m, count = 0; cout n; for (i = 1; i m;if (count = a.getSize() a.resize(count * 2);acount+ = m; for ( i = 0; i count; i+) cout ai; cout endl;cout数组和为:a.sum(n)endl; cout数字4在数组中的位置是:a.search(4,n)endl;/* Array进行测试Array a(5); int i,n,m, count = 0; cout n; for (i = 1; i m;if (count = a.getSize() a.resize(count * 2);acount+ = m; for ( i = 0; i count; i+) cout setw(8) ai; cout endl;cout数组和为:a.sum(n)endl; cout数字4在数组中的位置是:a.search(4,n)endl;*/ return 0;/然后实现第(3)小题#include #include using namespace std;template class Arrayprivate: T *list ; public: Array(); /构造函数 Array(); /析构函数 T & operator (int i); /重载” const T & operator (int i) const;T sum(); int search(T e); ;template Array:Array() /构造函数 list=new Tn; template Array:Array() /析构函数 delete list;/重载下标运算符template T &Array:operator (int i) return listi; templateT Array:sum() T sum = list0; for(int i = 1; i n; i+) sum += listi;return sum;/查找templateint Array:search(T e) for(int i = 0; i n; i+) if(listi = e) return i;return -1;int main() Array a; int i,n,m, count = 0; cout n; for (i = 1; i m;acount+ = m; for ( i = 0; i count; i+) cout ai; cout endl;cout数组和为:a.sum()endl; cout数字4在数组中的位置是:a.search(4)endl;/* Array进行测试Array a; int i,n,m, count = 0; cout n; for (i = 1; i m;if (count = a.getSize() a.resize(count * 2);acount+ = m; for ( i = 0; i count; i+) cout setw(8) ai; cout endl;cout数组和为:a.sum(n)endl; cout数字4在数组中的位置是:a.search(4,n)endl;*/ return 0;运行结果:5 文件读写定义学生类数组,有N个人(N=5),包括姓名和语数外三名课的成绩,通过重载运算符实现学生数组的文件读写。/*1.定义student类,私有数据成员字符数组name20;2.定义运算符重载;3.在住函数中定义student 类数组sN;并以输出和二进制的方式打开文件*/#include #include #include #define N 5using namespace std;class student;ostream& operator (istream & is, student &s);class student/student类:姓名 + 4门成绩public:student() student(string name, int chinese_socre, int maths_score, int english_score)this-name = name;this-chinese_score= chinese_score;this-maths_score = maths_score;this-english_score = english_score;friend ostream& operator(ostream & os, student s)/声明友元,重写os s.chinese_score s.maths_score s.english_score (istream & is, student &s)is s.chinese_score s.maths_score s.english_score;return is;private:string name;int chinese_score;int maths_score;int english_score;int main()int i;student sN;for( i=0;isi;ofstream ofs(c:testtest.txt, ios_base:out); if(!ofs)cerrfile open failedendl;exit(1);for( i=0;iN;i+)/这个我也不太明白-ofs.write(reinterpret_cast(& si),sizeof(student);ifstream ifs(c:testtest.txt, ios_base:out);if(!ifs)cerrfile open failedendl;exit(1);for( i=0;iN;i+)ifs.read(reinterpret_cast(& si),sizeof(student);for(i=0;iN;i+)coutNext用来接收调用浏览函数时所传递过来的实参,用设置好的输出格式(print()、Display()以及while循环,不为空则开始打印信息。4、查询信息 1)按工号查询声明链表指针ptr指向Next ,cout、cin提示输入工号并赋值给code,匹配ptr中的m_code,显示匹配结果即可;2)按科室查询声明链表指针ptr指向Next ,cout、cin提示输入科室并赋值给post,匹配ptr中的m_post,显示匹配结果即可;3)双匹配查询声明链表指针ptr指向Next ,cout、cin提示输入工号、科室并赋值给code、post,匹配ptr中的m_code、m_post,显示匹配结果5、修改信息 通过职工的工号修改职工信息。声明链表指针ptr,调用查找函数Search_Unique_Front()找到需修改的职工信息,再次赋值即可。需要用 cout提示要输入的内容,接着用 cin输入相应的内容。6、删除信息 键盘输入的职工号,通过职工的工号code删除职工信息。链表指针匹配m_code并删除Head-Next7、计算科室平均工资 在按科室查找SearchPost函数中定义一个int类型 n(为科室人数),初始化为 0;每添加一人,则 sum+=ptr-m_Wage。用总的工资除以总人数,算出平均工资。六、源代码#include #include #include #include #include #include #include #include using namespace std; int n=0;class employee public:string m_Code; /职工工号string m_Name; string m_phone; string m_Sex; string m_Post;/所在科室unsigned int m_Average;unsigned int m_Cash;unsigned int m_Wage; /链表节点的指针域- employee* Next; public:void Print();employee* Create(employee* Head);/创建一个链表 void Rel(employee* Head); employee* Add(employee* Head); bool Search(employee* Head); int SearchPost(employee* Head,string post);employee* Search_Unique_Front(employee* Head); void Display_List(employee* Head); void Display_Node(employee* pNode); employee* Modify(employee* Head); employee* Del(employee* Head); void Save_ByFile(employee* Head,fstream& ofile); employee* Sort(employee* Head); ;employee* employee:Create(employee* Head) /创建一个带头节点的空链表。 Head=new employee; if(!Head) cout分配内存失败!m_Code=; Head-m_Name=; Head-m_phone=; Head-m_Sex=; Head-m_Post=; Head-m_Wage=0; Head-Next=NULL;Head-m_Average=0;Head-m_Cash=0;return Head; void employee:Rel(employee* Head) /释放链表。 employee* ptr;/声明一个操作用的指针。 while(Head!=NULL) ptr=Head; Head=Head-Next; delete ptr;/释放节点资源。 employee* employee:Add(employee* Head) /输入职工信息/前插法添加数据。 employee* pNew;/ 声明一个新节点。 char again; string code,name,sex,post,phone; unsigned int wage; do pNew=new employee; /数据域。 coutcode; coutendlname; coutendlphone; coutendlsex; coutendlpost; coutendlwage; while(cin.fail() cout请输入正确的工资数据。wage; coutm_Code=code; pNew-m_Name=name; pNew-m_phone=phone; pNew-m_Sex=sex; pNew-m_Post=post; pNew-m_Wage=wage; /指针域。 pNew-Next=Head-Next; Head-Next=pNew; cout数据添加成功!是否继续添加?(Y/N)again; while(again=Y|again=y); return Head; bool employee:Search(employee* Head) /查询同时满足“姓名”和“科室”的职工信息。 employee* ptr; string name,post; ptr=Head-Next; coutpost; coutendlname; coutendltt-查询结果-m_Name=name)&(ptr-m_Post=post) Display_Node(ptr);/打印满足条件的节点。 return true; ptr=ptr-Next;/查询下一节点。 coutn无此职工的信息。Next;coutcode;coutendltt-查询结果-m_Code=code)Display_Node(ptr);return ptr;ptr=ptr-Next;coutn无此职工的信息。Next;while(ptr)if(ptr-m_Post=post)sum+=ptr-m_Wage;n+;ptr=ptr-Next;cout科室post里的平均工资为:sum/n endl;return 0;void employee:Print()coutsetw(10)left工号; coutsetw(10)left姓名; coutsetw(18)left电话; coutsetw(10)left性别;coutsetw(10)left科
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 失智老人照护上海城建职业76课件
- 宿舍楼卫生间与洗衣设施设计方案
- 建筑工程项目楼宇结构安全监测方案
- 水的基本知识培训总结
- 药师培训基础知识47课件
- 原发性高血压82课件
- 中医药现代献检索医学信息检索78课件
- 二零二五年度门窗工程知识产权保护合同
- 二零二五年度土地整治与勘察设计合同
- 二零二五年度文化项目居间合同范本格式
- 鼓胀中医护理
- 设备整厂出售合同协议
- 2025-2030中国高k和ALD和和CVD金属前体行业市场发展趋势与前景展望战略研究报告
- 高考补习学生管理制度
- 2025年4月12日衢州事业单位及市直遴选(选调)笔试真题及答案解析
- 占用林地补偿协议书
- 大体积混凝土施工培训讲义
- 压力性损伤课件
- 班主任班级管理手册
- 生产经营单位从业人员安全培训档案(一人一档)
- 天津市语文高考试卷及答案指导(2025年)
评论
0/150
提交评论