1、 计算机科学与工程学院 《面向对象程序设计》实验报告[二] 专业班级 2017数字媒体技术01 实验地点 J411 学生学号 1705121411 指导教师 陈艳 学生姓名 黄浩博 实验时间 实验项目 类的定义及应用(6学时) 实验类别 基础性() 设计性(√) 综合性() 其它( ) 实验目的及要求 1.掌握类定义的语法;
2、 2.掌握构造函数和析构函数的定义; 3.掌握几个关键字的用法:this、const、new/delete、friend 4.综合应用类的封装性解决常见问题; 5.完成实验内容,调试通过后将完整代码及运行截图粘贴到实验结果对应的题号下面; 6.对本次实验进行总结(遇到的问题,可行的解决方案,收获,体会等等) 7.完成实验报告后,以学号+姓名+实验2(.doc或.docx)为名上传到ftp://218.199.185.223/student/上传作业/陈艳/面向对象程序设计下对应班级及实验项目文件夹中 成 绩 评 定 表 类 别 评 分 标 准 分值 得分 合
3、 计
上机表现
积极出勤、遵守纪律
主动完成设计任务
30分
程序与报告
程序代码规范、功能正确
报告详实完整、体现收获
70分
说明:
评阅教师:
日 期: 年 月 日
实 验 内 容
题目1:
下面设计一个三角形类,请按下列声明形式完成各成员函数的定义。添加主程序中初始化该类,并调用成员函数测试结果。
#include
4、 using namespace std; class Triangle{ public: void Setabc(doublex, doubley, doublez); //设置三条边的值,注意要能成三角形 double Perimeter( ); //计算三角形的周长 double Area( ); //计算并返回三角形的面积 private: double a,b,c; //三条边的长度为私有数据成员 }; 题目2: 定义一个正方形类Square,编写构造函数、析构函数,Set()/Get()函数以 设置/读取边长,perimeter()
5、函数和area函数分别求周长和面积。并在主程序中进行测试。 题目3: 定义时间类Time,并计算两个时间之间的间隔。 题目4: 定义Student类(包含3个数据成员name,id_number,score),计算班上30个学生的平均成绩,并按成绩高到低顺序输出高于平均成绩的学生信息。 题目5:从 定义复数类,实现实部、虚部的读写,复数计算,结果输出等功能。并在主程序中进行测试。 题目6: 定义Point类和Line类(Line类的成员是Point的对象——复合),在主程序中进行测试,体会构造函数、析构函数、赋值运算符函数、拷贝构造函数的调用。 实 验 结 果
6、
题目1:
#include
7、>>c;
Triangle triangle;
triangle.Setabc(a,b,c);
double len=triangle.Perimeter();
double s=triangle.Area();
cout<<"The triangle's Perimeter is :"< 8、{
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:
#include 9、quare();
~Square();
void Set(double a);
void Get();
double Perimeter();
double Area();
};
int main(){
Square square;
std::cout<<"Please enter the square's length:";
double a,b,c;
std::cin>>a;
square.Set(a);
square.Get();
b=square.Perimete 10、r();
c=square.Area();
std::cout<<"The square's Perimeter is :"< 11、}
void Square::Set(double a){
m_a=a;
}
void Square::Get(){
std::cout<<"The square's length is:"< 12、ur,m_minute,m_second;
public:
Time(){std::cout<<"This is a new time."< 13、e time in chronological order.";
for(int a=0;a<2;a++){
std::cout<<"Please enter the time:(second minute,hour)"< 14、
bool flag1(0),flag2(0);
if(this->m_second>S.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_minute>S.m_minute) m=this->m_minute-S.m_minute;
else
15、
{
m=60+this->m_minute-S.m_minute;
flag2=1;
}
if(flag2) this->m_hour--;
h=this->m_hour-S.m_hour;
std::cout<<"The time interval is:(hour,minute,second) "< 16、UDED
#define STUDENT_H_INCLUDED
#include 17、t id,float score){m_name=name;m_id=id;m_score=score;};
float Interval(){return m_score;};
void pri(){std::cout<<"The name is "< 18、};
#endif // STUDENT_H_INCLUDED
main.cpp
#include 19、a 20、td::cin.get();
for(int a=0;a 21、et(name,id,score);
x++;
std::cout<<"Enter q to quit."< 22、
Student c;
c=student[a];student[a]=student[b];student[b]=c;
}
}
}
}
void prin( Student student[n],float x){
for(int a=0;a 23、else continue;
}
}
题目5:
Comp.h
#ifndef COMP_H_INCLUDED
#define COMP_H_INCLUDED
class 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) cons 24、t;
Comp operator-(const Comp &com) const;
Comp& operator=(const Comp &com);
};
#endif // COMPLEX_H_INCLUDED
main.cpp
#include 25、 a[3] :";
pri(a,3);
b[0]=a[0]+a[1];
b[1]=a[0]-a[2];
b[2]=a[0];
std::cout<<"The b[3] :";
pri(b,3);
return 0;
}
void Comp::cset(int r,int i){
real=r;
imag=i;
}
void Comp::cput(){
std::cout<<"The complex's real is "< 26、<"The complex's image is "< 27、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 28、 std::cin>>real;
std::cout< 29、
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();
Point operator+(const Point& point) const;
Point operator-(const Point& point) const;
Point& operator=(const Point& point);
};
class Lin 30、e {
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_start = s, m_end = e; };
Line operator+(const Line& line) const;
Line operator-(const Line& line) const 31、
Line& operator=(const Line& line);
};
#endif
Line.cpp
#include 32、erator-(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 << end 33、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) const {
Line l;
l.m_start = this->m_start - line.m_start;
l.m_end = this->m_end - line.m_end; 34、
return l;
}
Line& Line::operator=(const Line& line) {
this->m_start = line.m_start;
this->m_end = line.m_end;
return *this;
}
void Line::lput() {
cout << "The start point's x,y is:";
m_start.pput();
cout << "The end point's x,y is:";
m_end.pput();
cout< 35、
#include 36、0];
line[4] = line[0] - line[2];
line[5]=line[0];
lprin(line, 6);
return 0;
}
void pscan(Point p[], int n) {
int x, y;
for (int m = 0; m < n; m++) {
cout << "Please enter x,y:";
cin >> x >> y;
p[m].pset(x, y);
}
}
void lscan(Line l[], Point p[], int n,int m) {
for (int x 37、 = 0,y=0; x < n&&x < m; x++,y++,y++) {
l[x].lset(p[y], p[y+1]);
}
}
void lprin(Line l[], int n) {
for (int x = 0; x < n; x++) {
cout << "The line "<






