一、实验目的
- 掌握类的数据共享与保护的概念和使用方法
- 学习使用类的友元、静态成员,掌握多文件结构的程序编译预处理
二、实验内容
-
设计一个Grade类,判定学生某个考试的级别,全部操作在类Grade中实现(30分)。要求:
(1) 在定义对象时指定学生姓名和分数。且姓名必须指定,分数可缺省,缺省值为0分。要求包含构造函数、复制构造函数、姓名用string类型来定义。
主函数中实例化6个学生对象的姓名和分数分别为:
Sun l00
Wu 90
Jiang 52
Zhou 0
He 76
Liu 88
(2) 编写一个私有成员函数getGrade(),判定学生成绩的等级(60分以下为E等,[60-70)分为D等,[70-80)为C等,[80-90)为B等,[90-100]分为A等)。
(3)编写一个常成员函数getInfo()输出结果,包含学生姓名、分数、等级。
(4)定义静态数据成员count,用于统计总的学生人数,并在主函数中输出。
(5)定义该类的友元函数compare(Grade &m,Grade &n),比较两个学生成绩的高低,输出成绩高的那个学生信息。 -
请改写课本例题4-4线段(Line类),建立工程,使用多文件的组织结构实现程序(30分)。要求:
(1)文件划分:类定义文件(.h),类实现文件(.cpp),类使用文件(,cpp,主函数文件)。
(2)每个类的设计单独放在1个(.h)文件中,工程共包括5个文件。
(3)使用条件编译指令以免文件多次包含的情况。
(4)在Dev C++或VC中建立工程:文件-新建-项-(Basic—EmptyProject—C++项目)-新建文件夹Line,项目名称为Line,将所有文件保存在名为Line的文件夹中。VC建立过程是:文件—>新建—>工程—>Win32 Console Application。 -
请使用网页工具draw.io或其它或绘制UML图的工具创建一个UML图来描述上一题目中Line类与Point类的关系。(10分)
-
定义Boat和Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight()计算二者重量之和。(10分)
-
定义一个日期类(Date)和一个时间类(Time)。Time类是Date类的友元。Time类中的有打印时间的成员函数void display()可以实现打印出当前的时间,例如格式为:2021年10月30日;请编写程序设计这两个类,要求创建时间对象Today,并一次输出其中的日期和时间。(20分) 参考文献:C++关于时间操作 https://www.runoob.com/cplusplus/cpp-date-time.html
三、实验步骤及结果
- 设计一个Grade类,判定学生某个考试的级别,全部操作在类Grade中实现(30分)。要求:
(1) 在定义对象时指定学生姓名和分数。且姓名必须指定,分数可缺省,缺省值为0分。要求包含构造函数、复制构造函数、姓名用string类型来定义。
主函数中实例化6个学生对象的姓名和分数分别为:
Sun l00
Wu 90
Jiang 52
Zhou 0
He 76
Liu 88
(2) 编写一个私有成员函数getGrade(),判定学生成绩的等级(60分以下为E等,[60-70)分为D等,[70-80)为C等,[80-90)为B等,[90-100]分为A等)。
(3)编写一个常成员函数getInfo()输出结果,包含学生姓名、分数、等级。
(4)定义静态数据成员count,用于统计总的学生人数,并在主函数中输出。
(5)定义该类的友元函数compare(Grade &m,Grade &n),比较两个学生成绩的高低,输出成绩高的那个学生信息。
#include<iostream>
using namespace std;
class Grade{
private:
string name;
int score;
char getGrade(){
if (score<60)
return 'E';
else if (score<70)
return 'D';
else if (score<80)
return 'C';
else if (score<90)
return 'B';
else
return 'A';
}
public:
Grade(string name0,int score0=0):name(name0),score(score0){count++;
}
Grade(const Grade &g):name(g.name),score (g.score){count++;}
void getInfo(){
cout<<"姓名为:"<<name<<"分数为:"<<score<<"等级为:"<<getGrade()<<endl;
}
static int count;
friend void compare(Grade &m ,Grade &n);
};
int Grade::count=0;
void compare (Grade &m ,Grade &n){
if(m.score<m.score)
cout<<"成绩更高的是"<<n.name<<", 成绩为:" <<n.score<<endl;
else
cout<<"成绩更高的是"<<m.name<<", 成绩为:" <<m.score<<endl;
}
int main (){
Grade g1("Sun",100),g2("Wu",90),g3("Jiang",52),g4("Zhou",0),g5("He",76),g6("Liu",88);
g1.getInfo();
g2.getInfo();
g3.getInfo();
cout<<"一共有"<<Grade::count<<"个学生"<<endl ;
compare(g1,g2);
return 0;
}
运行结果
- 请改写课本例题4-4线段(Line类),建立工程,使用多文件的组织结构实现程序(30分)。要求:
(1)文件划分:类定义文件(.h),类实现文件(.cpp),类使用文件(,cpp,主函数文件)。
(2)每个类的设计单独放在1个(.h)文件中,工程共包括5个文件。
(3)使用条件编译指令以免文件多次包含的情况。
(4)在Dev C++或VC中建立工程:文件-新建-项-(Basic—EmptyProject—C++项目)-新建文件夹Line,项目名称为Line,将所有文件保存在名为Line的文件夹中。VC建立过程是:文件—>新建—>工程—>Win32 Console Application。
point.h
#include <iostream>
#include <cmath>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){
x=xx;y=yy;
}
Point(Point &p);
int getX(){return x;}
int getY(){return y;}
private:
int x,y;
};
Point.cpp
#include "Point.h"
Point::Point(Point &p){
x=p.x;y=p.y;
cout<<"Calling the copy constructor of Point"<<endl;
}
Line.h
#include "Point.h"
class Line{
public:
Line(Point xp1,Point xp2);
Line(Line &l);
double getLen(){return len;}
private:
Point p1,p2;
double len;
};
Line.cpp
#include "Line.h"
Line::Line(Point xp1,Point xp2): p1(xp1),p2(xp2){
cout<<"Calling the constructor of Line"<<endl;
double x=static_cast<double>(p1.getX()-p2.getX());
double y=static_cast<double>(p1.getY()-p2.getY());
len=sqrt(x*x+y*y);
}
Line::Line(Line &l):p1(l.p1),p2(l.p2){
cout<<"Calling the copy constructor of Line"<<endl;
len=l.len;
}
main.cpp
#include "Line.h"
int main(){
Point myp1(1,1),myp2(4,5);
Line line(myp1,myp2);
Line line2(line);
cout<<"The length of the line is:";
cout<<line.getLen()<<endl;
cout<<"The length of the line2 is:";
cout<<line2.getLen();
return 0;
}
-
请使用网页工具draw.io或其它或绘制UML图的工具创建一个UML图来描述上一题目中Line类与Point类的关系。(10分)
-
定义Boat和Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight()计算二者重量之和。(10分)
#include <iostream>
using namespace std;
class Car;
class Boat{
private:
int weight;
public:
Boat(int weight0):weight(weight0){}
friend int getTotalWeight(const Boat &b,const Car &c);
};
class Car{
private:
int weight;
public:
Car(int weight0):weight(weight0){}
friend int getTotalWeight(const Boat &b,const Car &c);
};
int getTotalWeight(const Boat &b,const Car &c){
return b.weight+c.weight;
}
int main(){
Boat boat (10);
Car car(5);
cout<<getTotalWeight(boat,car);
}
运行结果
- 定义一个日期类(Date)和一个时间类(Time)。Time类是Date类的友元。Time类中的有打印时间的成员函数void display()可以实现打印出当前的时间,例如格式为:2021年10月30日;请编写程序设计这两个类,要求创建时间对象Today,并一次输出其中的日期和时间。(20分) 参考文献:C++关于时间操作 https://www.runoob.com/cplusplus/cpp-date-time.html
#include <iostream>
using namespace std;
class Date{
private:
int y,m,d;
public:
Date(int y0,int m0,int d0):y(y0),m(m0),d(d0){}
friend class Time;
};
class Time{
private:
int h,m,s;
public:
Time(int h0,int m0,int s0):h(h0),m(m0),s(s0){}
void display(const Date &date){
cout<<date.y<<"年"<<date.m<<"月"<<date.d<<"日"<<h<<":"<<m<<":"<<s<<endl;
}
};
int main (){
Date date(2022,1,1);
Time time(15,24,21);
time.display(date);
return 0;
}
运行结果:
四、实验小结
问题与解决办法:
编译出错 :[Error] ‘Car’ does not name a type
解决办法:当一个类用到下一个类时,需要提前声明下一个类。如在表头声明类 class Car;
编译出错,提示“[Error] expected before return”
解决办法:语句结束时的英文下的分号“;”输成了中文下的分号。改正后,错误消失。
心得体会:感谢老师与助教的细心讲解。认真了解了的类的使用和数据的共享与保护。编程过程中仍然会出现低级错误问题,仍需细心改正。本次实验受益匪浅,课后一定认真复习。