1、_计算机科学与工程学院面向对象程序设计实验报告二专业班级2017数字媒体技术01实验地点J411学生学号1705121411指导教师陈艳学生姓名黄浩博实验时间实验项目类的定义及应用(6学时)实验类别基础性() 设计性() 综合性() 其它( )实验目的及要求1掌握类定义的语法;2掌握构造函数和析构函数的定义;3掌握几个关键字的用法:this、const、new/delete、friend4综合应用类的封装性解决常见问题;5完成实验内容,调试通过后将完整代码及运行截图粘贴到实验结果对应的题号下面;6对本次实验进行总结(遇到的问题,可行的解决方案,收获,体会等等)7完成实验报告后,以学号+姓名+实
2、验2(.doc或.docx)为名上传到ftp:/218.199.185.223/student/上传作业/陈艳/面向对象程序设计下对应班级及实验项目文件夹中成 绩 评 定 表类 别评 分 标 准分值得分合 计上机表现积极出勤、遵守纪律主动完成设计任务30分程序与报告程序代码规范、功能正确报告详实完整、体现收获70分说明: 评阅教师: 日 期: 年 月 日实 验 内 容题目1:下面设计一个三角形类,请按下列声明形式完成各成员函数的定义。添加主程序中初始化该类,并调用成员函数测试结果。#include using namespace std; class Trianglepublic: void
3、Setabc(doublex, doubley, doublez);/设置三条边的值,注意要能成三角形double Perimeter( );/计算三角形的周长double Area( );/计算并返回三角形的面积 private: double a,b,c; /三条边的长度为私有数据成员;题目2:定义一个正方形类Square,编写构造函数、析构函数,Set()/Get()函数以设置/读取边长,perimeter()函数和area函数分别求周长和面积。并在主程序中进行测试。题目3:定义时间类Time,并计算两个时间之间的间隔。题目4:定义Student类(包含3个数据成员name,id_num
4、ber,score),计算班上30个学生的平均成绩,并按成绩高到低顺序输出高于平均成绩的学生信息。题目5:从定义复数类,实现实部、虚部的读写,复数计算,结果输出等功能。并在主程序中进行测试。题目6:定义Point类和Line类(Line类的成员是Point的对象复合),在主程序中进行测试,体会构造函数、析构函数、赋值运算符函数、拷贝构造函数的调用。实 验 结 果 题目1:#include#includeusing namespace std;class Trianglepublic:void Setabc(double x, double y, double z);double Perimet
5、er();double 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, doub
6、le z) a=x;b=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 Ar
7、ea();int main() 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 Sq
8、uare:Set(double 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_
9、hour=h;m_minute=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
10、(Time S) int 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=th
11、is-m_hour-S.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
12、,float score=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 / STUD
13、ENT_H_INCLUDEDmain.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(
14、student); prin(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(); re
15、turn x;void 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
16、Comp private: 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
17、,int );void 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
18、+com.real; 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;x
19、n;x+) int real,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;
20、;void pput();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, Po
21、int e) m_start = 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_
22、y = this-m_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 + li
23、ne.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 = line.m_start;this-m_end = line.m_end;return *this;void Lin
24、e:lput() cout The start points x,y is:; m_start.pput(); cout The end points x,y is:;m_end.pput();coutendl;main.cpp#include#includeLine.husing std:cin;using std:cout;using std:endl;void pscan(Point, int n);void lscan(Line, Point , int n,int m);void lprin(Line, int);int main() Point p6;pscan(p, 6);Lin
25、e line6;lscan(line, p, 6, 6);line3 = line1 + line0;line4 = line0 - line2;line5=line0;lprin(line, 6);return 0;void pscan(Point p, int n) int x, y;for (int m = 0; m n; m+) cout x y;pm.pset(x, y);void lscan(Line l, Point p, int n,int m) for (int x = 0,y=0; x n&x m; x+,y+,y+) lx.lset(py, py+1);void lprin(Line l, int n) for (int x = 0; x n; x+) cout The line x :endl;lx.lput();实 验 总 结了解了有关类的使用,熟悉了编译器的设置Welcome ToDownload !欢迎您的下载,资料仅供参考!精品资料