课程设计(小题).doc_第1页
课程设计(小题).doc_第2页
课程设计(小题).doc_第3页
课程设计(小题).doc_第4页
课程设计(小题).doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 函数重载定义重载函数max3用于计算三个数的最大值(参数类型分别为int和double)。#includeusing namespace std;int max3(int x, int y, int z)return (x y ? x : y) (x z ? x : z) ? (x y ? x : y) : (x z ? x : z);double max3(double x, double y, double z)return (x y ? x : y) (x z ? x : z) ? (x y ? x : y) : (x z ? x : z);void main()int a, b, c;cout a b c;cout The Max of three is max3(a, b, c) endl;double a1, b1, c1;cout a1 b1 c1;cout The Max of three is max3(a1, b1, c1) endl;2 类的组合定义point类,数据成员包括x,y,成员函数包括构造函数,拷贝构造函数和析构函数,以及setx,getx,sety,gety四个属性函数。定义line类,端点由两个point类的对象组成,包括构造函数,析构函数以及计算线段长度的函数getlength。在main函数中,定义line的对象,并输出其长度。#include#includeusing namespace std;class pointprivate:double x, y;public:point()point(double x, double y) :x(x), y(y)void setx(double xx)x = xx;double getx() return x; void sety(double yy)y = yy;double gety() return y; point(point & p)x = p.x;y = p.y;point();class lineprivate:point a, b;public:line(point aa,point bb) :a(aa), b(bb)double getlength()double length;length = sqrt(pow(a.getx() - b.getx(), 2) + pow(a.gety() - b.gety(), 2);return length;line();void main()point p1(2, 3);point p2(5, 6);line l1(p1,p2);cout The length of the line is l1.getlength()endl;3 对象数组和函数定义student类,数据成员包括姓名name和成绩score,成员函数包括构造函数,拷贝构造函数和析构函数。定义函数void highestscore(student s),输出分数最高的学生姓名和分数。在main函数中定义student sN,调用highestscore函数,输出分数最高的学生姓名和分数。#include#include#includeconst int N = 2;using namespace std;class studentprivate:string name;double score;public:student()student(string n, double s) :name(n), score(s)student(student &s)name = ;score = s.score;/*void setname(string nn)name = nn;string getname() return name; void setscore(double ss)score = ss;double getscore() return score; */void highestscore(student s)int k = 0;double max;max = s0.score;for (int i = 1; i N; i+)if (max si.score)max = si.score;k = i;cout The name is endl;cout Highestscore is sk.score endl;student()friend ostream & operator (ostream & os, student s)os s.score (istream & is, student &s)is s.score;return is;void main()student sN;for (int i = 0; i si;sN.highestscore(s);4 静态数据成员设计一个书类,能够保存书名、定价,所有书的本数和总价。(将书名和定价设计为普通数据成员;将书的本数和总价设计为静态数据成员)classbookprivate:doublepriece;stringname;staticintnum;staticdoubletotal;intbook:num;doublebook:total;5 动态内存分配定义point类,数据成员包括x,y,成员函数包括构造函数,拷贝构造函数和析构函数,以及setx,getx,sety,gety四个属性函数。在main函数中,用new和delete分配和释放N个point的数组。(N是const常量,N=10)#includeconst int N = 10;using namespace std;class pointprivate:int x, y;public:point()point(int x, int y) :x(x), y(y)point(point & p)x = p.x;y = p.y;void setx(int xx)x = xx;int getx() return x; void sety(int yy)y = yy;int gety() return y; point();void main()point *p = new pointN;delete p;6 类的继承定义一个point类,包含私有数据成员x,y,成员函数包括无参构造函数,带参构造函数,set和get属性函数。定义circle类,从point类公有派生,增加数据成员半径r,成员函数包括无参构造函数,带参构造函数,计算面积函数getarea。在main函数中定义一个circle的对象,并计算其面积。#includeusing namespace std;class pointprivate:double x, y;public:point()point(double x, double y) :x(x), y(y)void setx(double xx)x = xx;double getx() return x; void sety(double yy)y = yy;double gety() return y; point();class circle :public pointprivate:double r;public:circle()circle(double x, double y, double r) :point(x, y), r(r)double getarea()return 3.14*r*r;void main()circle c1(2, 3, 4.5);cout The area of circle is c1.getarea() endl;7 虚基类定义vehicle类,数据成员包括私有的weight,公有的构造函数,析构函数和输出函数dispaly;从vehicle类公有派生car类,增加数据成员载人数personnum,公有的构造函数,析构函数和输出display;从vehicle类公有派生truck类,增加数据成员载货量laod,公有的构造函数,析构函数和输出函数display;从car类和truck类共同公有派生出pickup类,包括公有的构造函数和输出函数。在main函数中,定义pickup类对象,并输出其基本信息。#includeusing namespace std;class vehicleprivate:double weight;public:vehicle(double w) :weight(w)void display()cout the weight of vehicle is weight endl;vehicle();class car :virtual public vehicleprivate:int personnum;public:car(double ww, int p) :vehicle(ww), personnum(p)void display()/vehicle:display();cout The number of person is personnum endl;car();class truck :virtual public vehicleprivate:double load;public:truck(double ww, double l) :vehicle(ww), load(l)void display()/vehicle:display();cout The load of the truck is load ,功能。在main函数里测试该类。#includeusing namespace std;class counterprivate:double num;public:counter()counter(double n) :num(n)counter operator+()+num;return *this;counter operator-()-num;return *this;counter operator+(int)counter nn;nn.num = this-num;num+;return nn;counter operator-(int)counter nn;nn.num = this-num;num-;return nn;friend ostream & operator (istream & is, counter &c);ostream & operator (ostream & os, counter c)os c.num (istream & is, counter &c)is c.num;return is;void main()counter c1,c2;cin c1;c2 = c1.operator+(0);cout c1= c1;cout c2= c2;9 虚函数和抽象类定义一个抽象类shape,包括公有的计算面积area函数,计算体积volume函数,输出基本信息函数printinfo(三个函数均为纯虚函数)。从shape公有派生point类,增加私有数据成员x,y坐标,以及构造函数,析构函数。从point公有派生circle类,增加私有数据成员半径r,以及构造函数,析构函数。从circle公有派生cylinder类,增加私有数据成员高度h,以及构造函数,析构函数。(在定义三个派生类的过程中,自己考虑需要重定义哪个虚函数)。在main函数中,定义shape类的指针,指向派生类的对象,输出三类对象的基本信息,面积,体积;(将shape指针改为引用再尝试)。#includeusing namespace std;class shapepublic:virtual double area() = 0;virtual double volume() = 0;virtual void printinfo() = 0;class point :public shapeprivate:int x, y;public:point(int x, int y) :x(x), y(y)void printinfo()cout x= x ,y= y endl;point();class circle :public pointprivate:double r;public:circle(int x, int y, double r) :point(x, y), r(r)double area()return 3.14*r*r;void printinfo()point:printinfo();cout r= r endl;/cout circle area is area() endl;circle();class cylinder :public circleprivate:double h;public:cylinder(int x, int y, double r, double h) :circle(x, y, r), h(h)double area()return circle:area();double volume()return circle:area()*h;void printinfo()circle:printinfo();cout h= h endl;/cout volume is volume() endl;cylinder();void main()cylinder c1(2, 3, 4, 5);shape *s;s = &c1;(*s).printinfo();cout circle area is area() endl;cout volume is (*s).volume() endl;10 模板设计一个堆栈的类模板Stack,在模板中用类型参数T表示栈中存放的数据,用非类型参数MAXSIZE代表栈的大小。#include/const int MAXSIZE = 5;using namespace std;templateclass stackprivate:T aMAXSIZE;int top;public:stack() :top(0)void push(int x)if (top = MAXSIZE)cout the stack is full endl;elseatop = x;top+;T pop()if (top = 0)cout the stack is empty endl;elsetop-;return atop;void main()stack s;s.push(1);cout s.pop() endl;11 文件读写定义学生类数组,有N个人(N=5),包括姓名和语数外三名课的成绩,通过重载运算符实现学生数组的文件读写。(姓名用string name;)#include#include#include#includ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论