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 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 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 #include #define Pi 3.141516 using namespace std;

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为templateclass Array;形似2为templateclass Array;用于对T类型的数组进行构造和输出;

2)产生模板类Array和Array进行测试;

3)产生模板类Array和Array进行测试。

//先实现第(2)小题 #include #include using namespace std;

template class Array {

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 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 list[n]; }

template

const T &Array::operator[] (int n) const {

assert(n >= 0 && n < size); return list[n]; }

本文来源:https://www.bwwdw.com/article/gxwd.html

Top