




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算机科学与工程学院面向对象程序设计实验报告二专业班级2017数字媒体技术01实验地点J411学生学号指导教师陈艳学生姓名黄浩博实验时间实验项目类的定义及应用(6学时)实验类别基础性() 设计性() 综合性() 其它( )实验目的及要求1掌握类定义的语法;2掌握构造函数和析构函数的定义;3掌握几个关键字的用法:this、const、new/delete、friend4综合应用类的封装性解决常见问题;5完成实验内容,调试通过后将完整代码及运行截图粘贴到实验结果对应的题号下面;6对本次实验进行总结(遇到的问题,可行的解决方案,收获,体会等等)7完成实验报告后,以学号+姓名+实验2(.doc或.do
2、cx)为名上传到23/student/上传作业/陈艳/面向对象程序设计下对应班级及实验项目文件夹中成 绩 评 定 表类 别评 分 标 准分值得分合 计上机表现积极出勤、遵守纪律主动完成设计任务30分程序与报告程序代码规范、功能正确报告详实完整、体现收获70分说明: 评阅教师: 日 期: 年 月 日实 验 内 容题目1:下面设计一个三角形类,请按下列声明形式完成各成员函数的定义。添加主程序中初始化该类,并调用成员函数测试结果。#include using namespace std; class Trianglepublic: void Setabc(doub
3、lex, doubley, doublez);/设置三条边的值,注意要能成三角形double Perimeter( );/计算三角形的周长double Area( );/计算并返回三角形的面积 private: double a,b,c; /三条边的长度为私有数据成员;题目2:定义一个正方形类Square,编写构造函数、析构函数,Set()/Get()函数以设置/读取边长,perimeter()函数和area函数分别求周长和面积。并在主程序中进行测试。题目3:定义时间类Time,并计算两个时间之间的间隔。题目4:定义Student类(包含3个数据成员name,id_number,score),
4、计算班上30个学生的平均成绩,并按成绩高到低顺序输出高于平均成绩的学生信息。题目5:从定义复数类,实现实部、虚部的读写,复数计算,结果输出等功能。并在主程序中进行测试。题目6:定义Point类和Line类(Line类的成员是Point的对象复合),在主程序中进行测试,体会构造函数、析构函数、赋值运算符函数、拷贝构造函数的调用。实 验 结 果 题目1:#include#includeusing namespace std;class Trianglepublic:void Setabc(double x, double y, double z);double Perimeter();double
5、 Area();private:double a,b,c;int main() double a,b,c; coutabc; Triangle triangle; triangle.Setabc(a,b,c); double len=triangle.Perimeter(); double s=triangle.Area(); coutThe triangles Perimeter is :lenendl The triangles Area is :sendl; return 0;void Triangle:Setabc(double x, double y, double z) a=x;b
6、=y;c=z;double Triangle:Perimeter() double len; len=a+b+c; return len;double Triangle:Area() double p=(a+b+c)/2; double s=sqrt(p*(p-a)*(p-b)*(p-c); return s;题目2:#includeclass Squareprivate: double m_a;public: Square(); Square(); void Set(double a); void Get(); double Perimeter(); double Area();int ma
7、in() Square square; std:couta; square.Set(a); square.Get(); b=square.Perimeter(); c=square.Area(); std:coutThe squares Perimeter is :bstd:endl The squares Area is :cstd:endl; return 0;Square:Square() std:coutThis project is about square!std:endl;Square:Square() std:coutByestd:endl;void Square:Set(do
8、uble a) m_a=a;void Square:Get() std:coutThe squares length is:m_astd:endl;double Square:Perimeter() return 4*m_a;double Square:Area() return m_a*m_a;题目3:#includeclass Timeprivate: int m_hour,m_minute,m_second;public: Time()std:coutThis is a new time.std:endl; void Set(int s,int m,int h)m_hour=h;m_mi
9、nute=m;m_second=s; void Interval(Time s); Time()std:coutEnd.std:endl;int main() Time time2; std:coutPlease enter the time in chronological order.; for(int a=0;a2;a+) std:coutPlease enter the time:(second minute,hour)smh; timea.Set(s,m,h); time1.Interval(time0); return 0;void Time:Interval(Time S) in
10、t s,m,h; bool flag1(0),flag2(0); if(this-m_secondS.m_second) s=this-m_second-S.m_second; else s=60+this-m_second-S.m_second; flag1=1; if(flag1) this-m_minute-; if(this-m_minuteS.m_minute) m=this-m_minute-S.m_minute; else m=60+this-m_minute-S.m_minute; flag2=1; if(flag2) this-m_hour-; h=this-m_hour-S
11、.m_hour; std:coutThe time interval is:(hour,minute,second) h hours,m minutess seconds.;题目4:Student.h#ifndef STUDENT_H_INCLUDED#define STUDENT_H_INCLUDED#includeusing std:string;class Studentprivate: string m_name; long int m_id; float m_score;public: Student(string name=None,long int id=0,float scor
12、e=0):m_name(name),m_id(id),m_score(score); void Set(string name,long int id,float score)m_name=name;m_id=id;m_score=score; float Interval()return m_score; void pri()std:coutThe name is m_name.The id is m_id.The score is m_scorem_scorea.m_score) return true ;else return false;#endif / STUDENT_H_INCLU
13、DEDmain.cpp#include#includeStudent.hconst int n=30;int scan(Student ,int );void sear(Student );void prin(Student ,float );int main() Student studentn; int x=0; float sum(0); x=scan(student,x); for(int a=0;an;a+) sum+=studenta.Interval(); std:coutAverage achievement is sum/xstd:endl; sear(student); p
14、rin(student,sum/x); return 0;int scan(Student studentn,int x) char ch; std:coutEnter q to quit.ch;std:cin.get(); for(int a=0;an&ch!=q;a+) std:coutPlease enter a Student:(name,id,score)nameidscore; std:cin.get(); studenta.Set(name,id,score); x+; std:coutEnter q to quit.ch;std:cin.get(); return x;void
15、 sear(Student studentn) for(int a=0;an;a+) for(int b=1;bn;b+) if(studentb.bigger(studenta) Student c; c=studenta;studenta=studentb;studentb=c; void prin( Student studentn,float x) for(int a=0;ax) studenta.pri(); else continue; 题目5:Comp.h#ifndef COMP_H_INCLUDED#define COMP_H_INCLUDEDclass Comp privat
16、e: int real,imag;public: Comp(int r=0,int i=0):real(r),imag(i); Comp(); void cset(int ,int ); void cput(); Comp operator+(const Comp &com) const; Comp operator-(const Comp &com) const; Comp& operator=(const Comp &com);#endif / COMPLEX_H_INCLUDEDmain.cpp#include#includeComp.hvoid sca(Comp ,int );void
17、 pri(Comp ,int );int main() Comp a3; Comp b3; sca(a,3); std:coutThe a3 :; pri(a,3); b0=a0+a1; b1=a0-a2; b2=a0; std:coutThe b3 :; pri(b,3); return 0;void Comp:cset(int r,int i) real=r; imag=i;void Comp:cput() std:coutThe complexs real is realstd:endl; std:coutThe complexs image is imagreal+com.real;
18、sum.imag=this-imag+com.imag; return sum;Comp Comp:operator-(const Comp &com) const Comp sum; sum.real=this-real-com.real; sum.imag=this-imag-com.imag; return sum;Comp& Comp:operator=(const Comp &com) this-real=com.real; this-imag=com.imag; return *this;void sca(Comp a,int n) for(int x=0;xn;x+) int r
19、eal,imag; std:coutxreal; std:coutximag; ax.cset(real,imag); void pri(Comp a,int n) for(int x=0;xn;x+) ax.cput(); 题目6:Line.h#ifndef LINE_H_#define LINE_H_class Point private:int m_x, m_y;public:Point(int x = 0, int y = 0) :m_x(x), m_y(y) ;Point() ;void pset(int x, int y) m_x = x; m_y = y; ;void pput(
20、);Point operator+(const Point& point) const;Point operator-(const Point& point) const;Point& operator=(const Point& point);class Line private:Point m_start, m_end;public:Line(int x1=0, int y1=0, int x2=0, int y2=0) :m_start(x1, y1), m_end(x2, y2) ;Line() ;void lput();void lset(Point s, Point e) m_st
21、art = s, m_end = e; ;Line operator+(const Line& line) const;Line operator-(const Line& line) const;Line& operator=(const Line& line);#endif Line.cpp#include#includeLine.husing std:cout;using std:endl;Point Point:operator+(const Point& point) const Point p;p.m_x = this-m_x + point.m_x;p.m_y = this-m_
22、y + point.m_y;return p;Point Point:operator-(const Point& point) const Point p;p.m_x = this-m_x - point.m_x;p.m_y = this-m_y - point.m_y;return p;Point& Point:operator=(const Point& point) this-m_x = point.m_x;this-m_y = point.m_y;return *this;void Point:pput() cout m_x , m_y m_start + line.m_start;l.m_end = this-m_end + line.m_end;return l;Line Line:operator-(const Line& line) const Line l;l.m_start = this-m_start - line.m_start;l.m_end = this-m_end - line.m_end;return l;Line& Line:operator=(const Line& line) this-m_start =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年麻醉科危重病人监护技术模拟考试答案及解析
- 2025山东威海方正外国语学校教师招聘5人笔试备考试题及答案解析
- 2025年外科手术技术模拟考试答案及解析
- 2025福建南平市公安局建阳分局招聘警务辅助人员16人笔试参考题库附答案解析
- 2025宁波市鄞州区五乡镇人民政府代招村工作人员2人笔试模拟试题及答案解析
- 2025年肿瘤学乳腺癌手术护理考核答案及解析
- 2025年矿业权评估师资格考试(矿业权评估经济与法律专业能力)复习题及答案
- 2025年矿业权评估师考试(矿业权评估实务与案例固体矿产资源勘查与实物量估算)练习题及答案(娄底)
- 克拉玛依市中石油2025秋招面试半结构化模拟题及答案油气储运与管道岗
- 2025年初级经济师资格考试(运输经济专业知识和实务)能力提高训练试题库及答案(广东省)
- 电动起重机司机装卸司机
- DLT817-2014 立式水轮发电机检修技术规程
- 上海西门妇孺医院研究(1884-1952)
- 劳动创造美好生活课件 第二课 璀璨的星空
- 使用林地可行性研究报告
- (样表)有限空间有毒有害气体检测表
- 公司员工考勤表模板竖版
- 模电ppt课件 第1章绪论
- 水处理项目施工组织方案
- 幼儿园红色故事绘本:《闪闪的红星》 课件
- GA/T 383-2014法庭科学DNA实验室检验规范
评论
0/150
提交评论