资源描述
______________________________________________________________________________________________________________
计算机科学与工程学院
《面向对象程序设计》实验报告[二]
专业班级
2017数字媒体技术01
实验地点
J411
学生学号
1705121411
指导教师
陈艳
学生姓名
黄浩博
实验时间
实验项目
类的定义及应用(6学时)
实验类别
基础性() 设计性(√) 综合性() 其它( )
实验目的及要求
1.掌握类定义的语法;
2.掌握构造函数和析构函数的定义;
3.掌握几个关键字的用法:this、const、new/delete、friend
4.综合应用类的封装性解决常见问题;
5.完成实验内容,调试通过后将完整代码及运行截图粘贴到实验结果对应的题号下面;
6.对本次实验进行总结(遇到的问题,可行的解决方案,收获,体会等等)
7.完成实验报告后,以学号+姓名+实验2(.doc或.docx)为名上传到ftp://218.199.185.223/student/上传作业/陈艳/面向对象程序设计下对应班级及实验项目文件夹中
成 绩 评 定 表
类 别
评 分 标 准
分值
得分
合 计
上机表现
积极出勤、遵守纪律
主动完成设计任务
30分
程序与报告
程序代码规范、功能正确
报告详实完整、体现收获
70分
说明:
评阅教师:
日 期: 年 月 日
实 验 内 容
题目1:
下面设计一个三角形类,请按下列声明形式完成各成员函数的定义。添加主程序中初始化该类,并调用成员函数测试结果。
#include<iostream>
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()函数和area函数分别求周长和面积。并在主程序中进行测试。
题目3:
定义时间类Time,并计算两个时间之间的间隔。
题目4:
定义Student类(包含3个数据成员name,id_number,score),计算班上30个学生的平均成绩,并按成绩高到低顺序输出高于平均成绩的学生信息。
题目5:从
定义复数类,实现实部、虚部的读写,复数计算,结果输出等功能。并在主程序中进行测试。
题目6:
定义Point类和Line类(Line类的成员是Point的对象——复合),在主程序中进行测试,体会构造函数、析构函数、赋值运算符函数、拷贝构造函数的调用。
实 验 结 果
题目1:
#include<iostream>
#include<math.h>
using namespace std;
class Triangle{
public:
void Setabc(double x, double y, double z);
double Perimeter();
double Area();
private:
double a,b,c;
};
int main(){
double a,b,c;
cout<<"Please enter the triangle's a,b,c:";
cin>>a>>b>>c;
Triangle triangle;
triangle.Setabc(a,b,c);
double len=triangle.Perimeter();
double s=triangle.Area();
cout<<"The triangle's Perimeter is :"<<len<<endl
<<"The triangle's Area is :"<<s<<endl;
return 0;
}
void Triangle::Setabc(double x, double y, double 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:
#include<iostream>
class Square{
private:
double m_a;
public:
Square();
~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.Perimeter();
c=square.Area();
std::cout<<"The square's Perimeter is :"<<b<<std::endl
<<"The square's Area is :"<<c<<std::endl;
return 0;
}
Square::Square(){
std::cout<<"This project is about square!"<<std::endl;
}
Square::~Square(){
std::cout<<"Bye~~"<<std::endl;
}
void Square::Set(double a){
m_a=a;
}
void Square::Get(){
std::cout<<"The square's length is:"<<m_a<<std::endl;
}
double Square::Perimeter(){
return 4*m_a;
}
double Square::Area(){
return m_a*m_a;
}
题目3:
#include<iostream>
class Time{
private:
int m_hour,m_minute,m_second;
public:
Time(){std::cout<<"This is a new time."<<std::endl;};
void Set(int s,int m,int h){m_hour=h;m_minute=m;m_second=s;};
void Interval(Time s);
~Time(){std::cout<<"End..."<<std::endl;};
};
int main(){
Time time[2];
std::cout<<"Please enter the time in chronological order.";
for(int a=0;a<2;a++){
std::cout<<"Please enter the time:(second minute,hour)"<<std::endl;
int s,m,h;
std::cin>>s>>m>>h;
time[a].Set(s,m,h);
}
time[1].Interval(time[0]);
return 0;
}
void Time::Interval(Time S){
int s,m,h;
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
{
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) "<<h<<" hours,"<<m<<" minutes"<<s<<" seconds.";
}
题目4:
Student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include<string>
using std::string;
class Student{
private:
string m_name;
long int m_id;
float m_score;
public:
Student(string name="None",long int id=0,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::cout<<"The name is "<<m_name<<".The id is "<<m_id<<".The score is "<<m_score<<".\n";};
bool bigger(const Student a){if(this->m_score>a.m_score) return true ;else return false;};
};
#endif // STUDENT_H_INCLUDED
main.cpp
#include<iostream>
#include"Student.h"
const int n=30;
int scan(Student [],int );
void sear(Student []);
void prin(Student [],float );
int main(){
Student student[n];
int x=0;
float sum(0);
x=scan(student,x);
for(int a=0;a<n;a++){
sum+=student[a].Interval();
}
std::cout<<"Average achievement is "<<sum/x<<std::endl;
sear(student);
prin(student,sum/x);
return 0;
}
int scan(Student student[n],int x){
char ch;
std::cout<<"Enter q to quit."<<std::endl;
std::cin>>ch;std::cin.get();
for(int a=0;a<n&&ch!='q';a++){
std::cout<<"Please enter a Student:(name,id,score)"<<std::endl;
string name;
long int id;
float score;
std::cin>>name>>id>>score;
std::cin.get();
student[a].Set(name,id,score);
x++;
std::cout<<"Enter q to quit."<<std::endl;
std::cin>>ch;std::cin.get();
}
return x;
}
void sear(Student student[n]){
for(int a=0;a<n;a++){
for(int b=1;b<n;b++){
if(student[b].bigger(student[a])){
Student c;
c=student[a];student[a]=student[b];student[b]=c;
}
}
}
}
void prin( Student student[n],float x){
for(int a=0;a<n;a++){
if(student[a].Interval()>x){
student[a].pri();
}
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) const;
Comp operator-(const Comp &com) const;
Comp& operator=(const Comp &com);
};
#endif // COMPLEX_H_INCLUDED
main.cpp
#include<iostream>
#include"Comp.h"
void sca(Comp [],int );
void pri(Comp [],int );
int main(){
Comp a[3];
Comp b[3];
sca(a,3);
std::cout<<"The 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 "<<real<<std::endl;
std::cout<<"The complex's image is "<<imag<<std::endl;
}
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) 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<n;x++){
int real,imag;
std::cout<<x<<".Please enter the real :";
std::cin>>real;
std::cout<<x<<".Please enter the image :";
std::cin>>imag;
a[x].cset(real,imag);
}
}
void pri(Comp a[],int n){
for(int x=0;x<n;x++){
a[x].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();
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_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<iostream>
#include"Line.h"
using 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_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 << endl;
}
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;
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<<endl;
}
main.cpp
#include<iostream>
#include"Line.h"
using 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 p[6];
pscan(p, 6);
Line line[6];
lscan(line, p, 6, 6);
line[3] = line[1] + line[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 = 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 "<<x<<" :"<<endl;
l[x].lput();
}
}
实 验 总 结
了解了有关类的使用,熟悉了编译器的设置
Welcome To
Download !!!
欢迎您的下载,资料仅供参考!
精品资料
展开阅读全文