c++课程设计 - 职工工资管理系统
更新时间:2023-10-03 18:11:01 阅读量: 综合文库 文档下载
题目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
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 y int x; int y; };
class circle :public point //circle类公有派生point {
public: 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); cout<<\ cout << \该圆面积=\ system(\ return 0; }
//发现问题:定义的r好像只显示出int类型
运行结果分析:
主函数中r=2,输出圆面积12.56
2 运算符重载,友元函数和this指针
定义一个计数器类counter,具备自增,自减功能(前后缀);输入输出>>,<<功能。在main函数里测试该类。
/* return a;
} 1.定义counter类,私有成员数据weight,设置其成员函数(构
counter counter::operator --() //前置--重载实现 造函数和析构函数)
{ 2.重载自加自减运算符和<<、>>运算符。
--P; 3.在主函数中实现运算符重载。
return *this;
4.友元函数需要声明。
}
*/
counter counter::operator --(int) //后置--重载实现 #include
{ #include
counter a=*this; using namespace std;
--(*this); class counter;
return a; istream& operator>>(istream& is,counter& a);
} ostream& operator<<(ostream& os,counter& a);
class counter //定义类counter
istream& operator>>(istream& in,counter& a) //运算{
符>>重载实现 private:
{ double P;
in>>a.P; public:
if(!in) counter(){} //无参构造函数
cerr<<\输入错误!\ counter(double p):P(p){} //带参构造函数
return in;
counter operator ++(); //重载前置++
}
counter operator ++(int); //重载后置++ counter operator --(); //重载前置-- ostream& operator<<(ostream& out,counter& a) // counter operator --(int); //重载后置-- 运算符<<重载实现 friend istream& operator>>(istream& is,counter& a); { //声明友元,重载输入运算符>> out<
int main()
{ };
counter c1(236),c2(632);
cout<<\counter counter::operator ++() //前置++重载实现
cout<<\ {
//测试 ++P;
cout<<\ return *this;
cout<<\}
cout<<\counter counter::operator ++(int) //后置++重载实现
system(\{
return 0; counter a=*this;
} ++(*this);
运行结果分析:
定义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
class shape {
public: 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 << \ } ~point() {} private: double x; double y; };
class circle :public point //同理;circle类(圆)需重写两个虚函数; {
public: circle() {} circle(double r,double x,double y):point(x,y) { this->r = r; } double area() { return Pi*r*r; } void printinfo() { point::printinfo(); cout << \半径为\ cout << \圆的面积是 \ } ~circle() {} private: double r; };
class cylinder :public circle //圆柱,同理,不多说 {
public: cylinder() {} cylinder(double h, double x,double y, double r) :circle(x, y,r) { this->h = h; } /* double area() { return 2*Pi*this->r+circle::area(); //未实现计算圆柱表面积 } */ double volume() { return h*circle::area(); } void printinfo() { circle::printinfo(); cout << \高为\ cout << \圆柱的体积是\ } ~cylinder() {}
private: double h; };
int main() { cylinder temp(6, 2, 3, 3); shape *s; //实例化一个圆柱对象 s = &temp;
endl; }
(*s).printinfo(); printf(\
cout << \中数据构成的圆面积为 \cout << \体积为 \system(\return 0;
运行结果:
4 模板
编写一个使用类模板对数组进行查找、求元素和、重载下标[]运算符,以及输出的程序。
1)设计一个类模板:形式1为template
2)产生模板类Array
3)产生模板类Array
//先实现第(2)小题 #include
template
private:
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
assert(sz >= 0); size = sz;
list = new T [size];
}
template
delete [] list; }
//重载下标运算符[] template
T &Array
assert(n >= 0 && n < size); return list[n]; }
template
const T &Array
assert(n >= 0 && n < size); return list[n]; }
正在阅读:
c++课程设计 - 职工工资管理系统10-03
国民经济行业分类新旧类别对照表04-15
北京101中学2016-2017学年高一上学期期末考试数学试卷 Word版含06-02
何凯文长难句突破讲义完整版含授课内容04-13
XX太阳能光伏电站项目建设实施方案07-02
立体几何中的向量方法(二) - 求空间角和距离 高考考点精讲10-18
微机原理实验指导书07-08
2014华师中小学班主任在线作业答案12-19
鲁教版九年级7-8单元复习学案07-18
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- c++
- 管理系统
- 职工
- 工资
- 课程
- 设计
- 株洲工务段党员突击队、示范队章程
- 江苏省溧水县孔镇中学七年级生物下册《第十一章第二节人体废物的排出》素材苏教版
- 2019年河北省高职单招考试十类
- 环己烯的制备实验预习报告
- 老托福1996-05语法笔记
- 生产目标责任书范文4篇
- 2015实验技能大赛新闻稿
- (新人教版)八年级英语下册 Unit 5(Section B 1a-1d)导学案 - 图文
- 2018婚姻法司法解释一
- 水电安装规范内容00290 - 图文
- 、三年级二位数乘二位数练习题
- 齐鲁工业大学10-11第一学期企业管理试卷B卷
- 马英红 - 浅谈校外教育之论文
- 交银青会邮033号转发总行关于印发《交通银行跨境贸易人民币结算业务代理清算规程》的通知的通知
- 2016兰州市物业管理条例
- 严格依纪依法办案实施办法
- 13-14人教版八年级数学上册期中期末复习试题
- 实验小学“校园文化建设先进学校” 申报材料
- 材料库施工组织设计修改最终版
- 动车组列车消防安全管理实施细则