班级: B145A7 学号: 11 成绩:
一. 实验目的
1.理解面向对象程序设计的基本思想;
2.掌握类和对象的概念、定义和使用方法。
3.掌握不同特性对象成员的访问方法。
二. 使用的设备和仪器
计算机+Windows XP +Visual C++6.0
三. 实验内容及要求
1、 定义一个表示长方体类Cuboid,数据成员包括length(长)、width(宽)、height(高),成员函数包括长方体的输入、输出、计算体积和表面积等。在主函数中,定义3个长方体的对象,并调用成员函数完成其功能。
2、 定义一个学生类Student,数据成员包括学号、姓名、数学成绩、英语成绩和C++成绩,成员函数包括:输入学生的信息函数;输出学生的信息函数;设置学生的信息函数;计算学生的平均成绩的函数。在main函数中调用以上函数实现相应功能。
3、 定义一个图书类Book,在该类中包括以下数据成员和成员函数:
数据成员:id(书号)、bookname(书名)、price(价格)、total(总存书数量)、number(当前剩余图书数量)
成员函数:
Input()——图书信息输入;
Output()——图书信息输出;
Borrow()——借阅图书,并显示当前剩余图书数量;
Restore()——归还图书,并显示当前剩余图书数量。
在主函数中,要求创建某种图书对象,并对该图书进行简单的输入、输出、借阅和归还管理。
选择题:
4、 根据以下要求类的编写。
1)定义一个日期类Date,数据成员包括年、月、日,成员函数包括:
Input()——日期信息输入;
Output()——日期信息输出;
Set()——设置日期信息
2)在第2题Student类中增加一个出生日期成员,使用Date类来定义。然后修改相应的成员函数,并增加一个成员函数GetAge,用来计算并返回学生的年龄。
在主函数中定义对象,测试以上功能。
四. 实验步骤
实验一程序代码:
#include<iostream>
using namespace std;
class Cuboid
{
private:
float len,wit,high;
public:
void Input();
void Output();
void Volume();
void area();
};
void Cuboid::Input()
{
cout<<"请输入长方体长:";
cin>>len;
cout<<"请输入长方体宽:";
cin>>wit;
cout<<"请输入长方体高:";
cin>>high;
}
void Cuboid::Output()
{
cout<<"长方体长为:"<<len<<endl;
cout<<"长方体宽为:"<<wit<<endl;
cout<<"长方体高为:"<<high<<endl;
}
void Cuboid::Volume()
{
cout<<"长方体体积为:"<<len*wit*high<<endl;
}
void Cuboid::area()
{
cout<<"长方体表面积为:"<<len*wit*2+len*high*2+wit*high*2<<endl;
}
int main()
{
Cuboid t1,t2,t3;
t1.Input();
t1.Output();
t1.Volume();
t1.area();
t2.Input();
t2.Output();
t2.Volume();
t2.area();
t3.Input();
t3.Output();
t3.Volume();
t3.area();
return 0;
}运行结果:
实验二程序代码:
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
int num;
string name;
double english,math,c;
public:
void Input();
void Show();
int Are();
void Set(int x,string y,double z,double h,double k);
};
void Student::Input()
{
cout<<"请输入您的学号:";
cin>>num;
cout<<"请输入您的名字:";
cin>>name;
cout<<"请输入你的数学,英语,c++成绩:"<<endl;
cin>>math>>english>>c;
}
void Student::Show()
{
cout<<"学号:"<<num<<endl;
cout<<"名字:"<<name<<endl;
cout<<"数学:"<<math<<endl;
cout<<"英语:"<<english<<endl;
cout<<"c++:"<<c<<endl;
}
int Student::Are()
{
return (math+english+c)/3;
}
void Student::Set(int x,string y,double z,double h,double k)
{
num=x;
name=y;
english=z;
math=h;
c=k;
}
int main()
{
Student s;
s.Input();
s.Show();
cout<<"系统默认为:"<<endl;
s.Set(2014,"yule",100,100,100);
s.Show();
cout<<"平均成绩为:"<<s.Are()<<endl;
return 0;
}
运行结果:
实验三程序代码:
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
class Book
{
private:
int id;
string name;
float price;
int total;
int num;
public:
void Input();
void Output();
void Borrow();
void Restore();
};
void Book::Input()
{
cout<<"请输入图书信息:"<<endl;
cout<<"图书id:";
cin>>id;
cout<<"图书名字:";
cin>>name;
cout<<"图书价格:";
cin>>price;
cout<<"图书总量:";
cin>>total;
cout<<"图书剩余量:";
cin>>num;
}
void Book::Output()
{
cout<<"图书id为:"<<id<<endl;
cout<<"图书名字为:"<<name<<endl;
cout<<"图书价格:"<<price<<endl;
cout<<"图书总量:"<<total<<endl;
cout<<"图书剩余量:"<<num<<endl;
}
void Book::Borrow()
{
int n;
cout<<"请输入借书量:";
cin>>n;
while(n>num||n<0)
{
cout<<"输入数据有错,请重新输入"<<endl;
cin>>n;
}
num=num-n;
cout<<"图书剩余量为:"<<num<<endl;
}
void Book::Restore()
{
int m;
cout<<"请输入归还量:";
cin>>m;
num=num+m;
cout<<"图书剩余量为:"<<num<<endl;
}
int main()
{
Book s;
s.Input();
s.Output();
s.Borrow();
s.Restore();
return 0;
}
运行结果:
第四题代码
#include<iostream>
#include<iomanip>
using namespace std;
class Date
{
private:
int year,YEAR;
int month,MONTH;
int day,DAY;
public:
void Input();
void Output();
void Set(int YEAR,int MONTH,int DAY);
void Set_Intput();
};
void Date::Input()
{
cout<<"请输入年份:";
cin>>year;
cout<<"请输入月份:";
cin>>month;
while(month>12||month<0)
{
cout<<"数据有错,重新输入:";
cin>>month;
}
cout<<"请输入日:";
cin>>day;
while(day>31||day<0)
{
cout<<"数据有错,重新输入:";
cin>>day;
}
}
void Date::Output()
{
cout<<setw(6)<<"年"<<setw(6)<<"月"<<setw(6)<<"日"<<endl;
cout<<setw(6)<<year<<setw(6)<<month<<setw(6)<<day<<endl;
}
void Date::Set_Intput()
{
cout<<"请输入重置信息"<<endl;
cout<<"请输入年份:";
cin>>year;
cout<<"请输入月份:";
cin>>month;
while(month>12||month<0)
{
cout<<"数据有错,重新输入:";
cin>>month;
}
cout<<"请输入日:";
cin>>day;
while(day>31||day<0)
{
cout<<"数据有错,重新输入:";
cin>>day;
}
}
void Date::Set(int YEAR,int MONTH,int DAY)
{
year=YEAR;
month=MONTH;
day=DAY;
}
int main()
{
Date m;
m.Input();
m.Output();
m.Set_Intput();
m.Output();
return 0;
}
五. 实验总结
1、 实验中遇到的问题及解决方法
对于设置函数的理解方面有比较大的问题。解决方法:问了老师和同学,把这个问题解决了。
2、 个人收获和体会
通过这次试验,使我对C++的熟练度进一步提高;对于一些不理解的问题也弄明白了很多