题 目 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
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 {
//定义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函数里测试该类。
/* {
++P; 1.定义counter类,私有成员数据weight,设置其成员函数(构
return *this; 造函数和析构函数)
} 2.重载自加自减运算符和<<、>>运算符。
counter counter::operator ++(int) //后置++重载实3.在主函数中实现运算符重载。
现 4.友元函数需要声明。
{ */
counter a=*this; #include
++(*this); #include
return a; using namespace std;
} class counter;
counter counter::operator --() //前置--重载实现 istream& operator>>(istream& is,counter& a);
{ ostream& operator<<(ostream& os,counter& a);
--P; class counter //定义类counter
return *this; {
} private:
counter counter::operator --(int) //后置--重载实 double P;
现 public:
{ counter(){} //无参构造函数
counter a=*this; counter(double p):P(p){} //带参构造函数
--(*this); counter operator ++(); //重载前置++
return a; counter operator ++(int); //重载后置++
} counter operator --(); //重载前置--
istream& operator>>(istream& in,counter& a) // counter operator --(int); //重载后置--
friend istream& operator>>(istream& is,counter& a); 运算符 >>重载实现
{ //声明友元,重载输入运算符>>
friend ostream& operator<<(ostream& os,counter& a); in>>a.P;
if(!in) //同上
cerr<<\输入错误!\};
return in; counter counter::operator ++() //前置++重载实现
}
ostream& operator<<(ostream& out,counter& a) //运算符<<重载实现 { out< int main() { counter c1(236),c2(632); cout<<\ cout<<\ //测试 cout<<\ cout<<\ cout<<\ system(\ 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 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; (*s).printinfo(); printf(\ cout << \中数据构成的圆面积为 \<< endl; cout << \体积为 \ system(\ return 0; } 运行结果: 4 模板 编写一个使用类模板对数组进行查找、求元素和、重载下标[]运算符,以及输出的程序。 1)设计一个类模板:形式1为template //先实现第(2)小题 #include 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]; } //取当前数组的大小 template int Array return size; } // 将数组大小修改为sz template void Array 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++) newList[i] = list[i]; delete[] list; //删除原数组 list = newList; // 使list指向新数组 size = sz; //更新size } template T sum = list[0]; for(int i = 1; i < n; i++) sum += list[i]; return sum; } //查找 template int Array for(int i = 0; i < n; i++) if(list[i] == e) return i; return -1; } int main() { Array cout << \请输入数字个数: \ cin >> n; for (i = 1; i <= n; i++) { cin>>m; if (count == a.getSize()) a.resize(count * 2); a[count++] = m; } for ( i = 0; i < count; i++)
相关推荐: