【2017年整理】c++上机实验代码5个(1)

上传人:爱****1 文档编号:951604 上传时间:2017-05-23 格式:DOC 页数:13 大小:86.50KB
返回 下载 相关 举报
【2017年整理】c++上机实验代码5个(1)_第1页
第1页 / 共13页
【2017年整理】c++上机实验代码5个(1)_第2页
第2页 / 共13页
【2017年整理】c++上机实验代码5个(1)_第3页
第3页 / 共13页
【2017年整理】c++上机实验代码5个(1)_第4页
第4页 / 共13页
【2017年整理】c++上机实验代码5个(1)_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《【2017年整理】c++上机实验代码5个(1)》由会员分享,可在线阅读,更多相关《【2017年整理】c++上机实验代码5个(1)(13页珍藏版)》请在金锄头文库上搜索。

1、C+面向对象程序设计实验一:C+基础练习1、 编写一个将 3 个整数从小到大排序输出的程序。要求编写两个函数 main 和 sort,输入输出在 main 中完成,数的排序在 sort 中完成(要用引用实现) 。 (注意加上适当的注释)#include iostreamusing namespace std;void sort(int &a,int &b,int &c)/排序子函数int temp=0;if(babc;sort(a,b,c);cout2)return (fib(n-1)+fib(n-2);else return 0;int main()int i;int *p;p=new in

2、t20;/动态分配 20 个整型空间for(i=1;iusing namespace std;class Boxprivate:double length,width,high,volume;public :Box()length=2;width=6;high=9;volume=length*width*high;Box(double a,double b,double c )length=a;width=b;high=c;volume=length*width*high;Box(const Box &d)length=d.length*1.0/2;width=d.width*1.0/2;hi

3、gh=d.high*1.0/2;volume=length*width*high;void disp_vol()cout#include using namespace std;class Studentprivate:int no;char *name;double escore;public :Student(int a,char *b,double c)no=a;name=new charstrlen(b)+1;strcpy_s(name,strlen(b)+1,b);escore=c;Student ()cout#include using namespace std;class st

4、udentprivate:string sno;double score;public:student(string sno1,double score1)sno=sno1;score=score1;void display()cout#include using namespace std;class studentprivate:string sno;double score;public:student(string sno1,double score1)sno=sno1;score=score1;friend void max(student *);void max(student *

5、stp)student *p=stp;double temp,maxs=p-score;+p;while(p-sno!=)if(p-scoremaxs)maxs=p-score;p+;p=stp;while(p-sno!=)if(p-score=maxs)coutsno#include using namespace std;class catprivate :string name;int weight;static int allweight;static int count;public :cat(string name1,int weight1)name=name1;weight=we

6、ight1;allweight+=weight;count+;cout#include /包含字符串类,参见 book P83-85 string类的使用介绍using namespace std;class girl;/*()此处要添加合适代码,否则会有语法错误*/class boy public:boy(string n, int d) name=n;age=d; void disp(girl &); / 声明disp()为类boy的成员函数private:string name;/将name定义为C+ 字符串类型,可以简化和方便字符串的处理int age;class girlprivat

7、e :string nameg;int ageg;public:friend boy;girl(string s, int age1) nameg=s;ageg=age1; ;/*()此处要定义girl类的代码,在此省略了*/void boy:disp(girl &g)coutusing namespace std;class complexdouble real,image;public:complex(double real1=0,double image1=0)real=real1;image=image1;void print();friend complex operator+(co

8、mplex &a,complex &b);friend complex operator*(complex &a,complex &b);complex operator+(complex &a,complex &b)complex temp;temp.real=a.real+b.real;temp.image=a.image+b.image;return temp;complex operator*(complex &a,complex &b)complex temp;temp.real=a.real*b.real-a.image*b.image;temp.image=a.real*b.im

9、age+a.image*b.real;return temp;void complex:print()cout0) coutusing namespace std;const double pi=3.1415;class circleprotected :double radius;public:circle (double r=0)radius=r;virtual double volume()return 0;virtual void rprint()coutrprint();coutvolume()rprint();coutvolume()endl;return 0;说明:(1)请大家按学号来坐,便于考勤和管理。(2)请珍惜宝贵的实验时间!不要做与实验无关的事情,比如聊 QQ、上网或打游戏。(3) 直接把 C+代码粘贴到实验题目 word 文档的正确位置,上交实验报告时只交 word 文档。(4) word 文档名称:班级+学号后 3 位+姓名+实验编号,例如 10 通 1 班 160张山实验六.doc。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 实验/测试

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号