资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 int20;/动态分配 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;high=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 studentprivate: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 *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=weight1;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 girlprivate :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+(complex &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.image+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。
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号