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

下载本文档

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

文档简介

1、1 函数重载定义重载函数max3用于计算三个数的最大值(参数类型分别为int和double)。#include<iostream>using 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 :

2、z) ? (x > y ? x : y) : (x > z ? x : z);void main()int a, b, c;cout << "Please input three integer:"cin >> a >> b >> c;cout << "The Max of three is " << max3(a, b, c) << endl;double a1, b1, c1;cout << "Please input three r

3、eal number:"cin >> 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的对象,并输出其

4、长度。#include<iostream>#include<cmath>using 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();clas

5、s 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 &quo

6、t; << l1.getlength()<<endl;3 对象数组和函数定义student类,数据成员包括姓名name和成绩score,成员函数包括构造函数,拷贝构造函数和析构函数。定义函数void highestscore(student s),输出分数最高的学生姓名和分数。在main函数中定义student sN,调用highestscore函数,输出分数最高的学生姓名和分数。#include<iostream>#include<iomanip>#include<string>const int N = 2;using names

7、pace 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

8、; */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

9、 ostream & operator <<(ostream & os, student s)os << << " " << s.score << endl;return os;friend istream & operator >>(istream & is, student &s)is >> >> s.score;return is;void main()student sN;for (int i = 0;

10、i < N; i+)cin >> si;sN.highestscore(s);4 静态数据成员设计一个书类,能够保存书名、定价,所有书的本数和总价。(将书名和定价设计为普通数据成员;将书的本数和总价设计为静态数据成员)class bookprivate:double priece;string name;static int num;static double total;int book:num;double book:total;5 动态内存分配定义point类,数据成员包括x,y,

11、成员函数包括构造函数,拷贝构造函数和析构函数,以及setx,getx,sety,gety四个属性函数。在main函数中,用new和delete分配和释放N个point的数组。(N是const常量,N=10)#include<iostream>const 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

12、 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的对象,并计算其面积。#include<iostream>using n

13、amespace 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)

14、: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类公有派生tru

15、ck类,增加数据成员载货量laod,公有的构造函数,析构函数和输出函数display;从car类和truck类共同公有派生出pickup类,包括公有的构造函数和输出函数。在main函数中,定义pickup类对象,并输出其基本信息。#include<iostream>using namespace std;class vehicleprivate:double weight;public:vehicle(double w) :weight(w)void display()cout << "the weight of vehicle is " <&

16、lt; 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 vehi

17、cleprivate:double load;public:truck(double ww, double l) :vehicle(ww), load(l)void display()/vehicle:display();cout << "The load of the truck is " << load << endl;truck();class pickup :public car, public truckpublic:pickup(double w1, int p, double w2, double l, double w)

18、:car(w1, p), truck(w2, l), vehicle(w)void display()vehicle:display();car:display();truck:display();pickup();void main()pickup p1(211, 2, 200, 170,100);p1.display();8 运算符重载,友元函数和this指针定义一个计数器类counter,具备自增,自减功能(前后缀);输入输出>>,<<功能。在main函数里测试该类。#include<iostream>using namespace std;class

19、 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 &am

20、p; operator << (ostream & os, counter c);friend istream & operator >> (istream & is, counter &c);ostream & operator << (ostream & os, counter c)os << c.num << endl;return os;istream & operator >> (istream & is, counter &c)is >

21、;> 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公有

22、派生circle类,增加私有数据成员半径r,以及构造函数,析构函数。从circle公有派生cylinder类,增加私有数据成员高度h,以及构造函数,析构函数。(在定义三个派生类的过程中,自己考虑需要重定义哪个虚函数)。在main函数中,定义shape类的指针,指向派生类的对象,输出三类对象的基本信息,面积,体积;(将shape指针改为引用再尝试)。#include<iostream>using namespace std;class shapepublic:virtual double area() = 0;virtual double volume() = 0;virtual v

23、oid 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) :poin

24、t(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, do

25、uble 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,

26、 4, 5);shape *s;s = &c1;(*s).printinfo();cout << "circle area is " << s->area() << endl;cout << "volume is " << (*s).volume() << endl;10 模板设计一个堆栈的类模板Stack,在模板中用类型参数T表示栈中存放的数据,用非类型参数MAXSIZE代表栈的大小。#include<iostream>/const int MAXSIZE

27、 = 5;using namespace std;template<typename T,int MAXSIZE>class 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" <

28、;< endl;elsetop-;return atop;void main()stack<int,5> s;s.push(1);cout << s.pop() << endl;11 文件读写定义学生类数组,有N个人(N=5),包括姓名和语数外三名课的成绩,通过重载<<和>>运算符实现学生数组的文件读写。(姓名用string name;)#include<iostream>#include<fstream>#include<string>#include<iomanip>const int N = 3;using namespace std;class studentprivate:char name10;double Chinese, Math, English;public:student()student(char n, double

温馨提示

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

评论

0/150

提交评论