第十五次实验-多态与运算符重载2

更新时间:2024-01-19 01:20:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

面向对象程序设计 lab15 学号 班级 姓名

实验15 运算符重载,异常处理和文件操作

【实验目的】

(1)掌握运算符重载的基本方法。

(2)掌握异常和异常处理。 (3)掌握文件操作的基本方法。

【实验内容】

第一部分:教程练习

1、【编写程序】:实现以下功能

1.输入N本图书的信息:书号(6个字符)、书名(12个字符)、作者(8个字符)、单价(2位小数);将所有数据写入文件ST1.DAT中; 2.从ST1.DAT文件中读取图书数据,将价钱高于30元的图书信息输出; 3.输入书号,在ST1.DAT文件中查找该图书,找到以后输出该图书的所有数据,如果文件中没有输入的书号,给相应的提示信息。

===================源程序=======================

#include

#include #include class book {

public: int id;

char name[10]; char author[10]; int price; public:

void getdata() {

cout<<\请分别输入书号,书名,作者,价格:\ cin>>id>>name>>author>>price; }

void dispdata() {

面向对象程序设计 lab15 学号 班级 姓名

cout<

void inputdata() {

ofstream outfile(\ book book1; int num;

cout<>num;

for(int i=0;i

cout<<\输入第\本书的数据:\ book1.getdata();

outfile.write((char*)&book1,sizeof(book1)); }

outfile.close(); }

void display() {

ifstream infile(\ book book1;

cout<

cout<

infile.read((char*)&book1,sizeof(book1)); while(infile) {

if(book1.price>30) book1.dispdata();

infile.read((char*)&book1,sizeof(book1)); }

infile.close();

面向对象程序设计 lab15 学号 班级 姓名

}

void main() {

int a; do {

cout<<\输入数据??2输入数据??others退出\ cin>>a; switch(a) {

case 1:

inputdata(); break; case 2: display(); break; }

}while(a==1||a==2); }

第二部分:自测题

1、 多态练习:

1) 设计一个立体图形类(CStereoShape类),并满足如下要求: ?CStereoShape类有一个纯虚函数GetArea,能够获取立方体的表面积; ?CStereoShape类有一个纯虚函数GetVolume,能够获取立方体的体积

面向对象程序设计 lab15 学号 班级 姓名 2) 设计一个立方体类(CCube类),该类继承于CStereoShape类,并满足如下要求:

?CCube类有一个带参数的构造函数,其参数分别对应于立方体的长、宽、高。 ?用一个成员函数来实现对立方体长、宽、高的设置。

?重载CStereoShape类的GetArea和GetVolume,分别完成立方体的表面积和 体积的计算。

3) 设计一个球体类 (CSphere),该类继承于CStereoShape类,并满足如下要求:

?CSphere类有一个带参数的构造函数,其参数对应于球体的半径。 ?用一个成员函数来实现对球体半径的设置。

?重载CStereoShape类的GetArea和GetVolume,分别完成球体的表面积和体 积的计算。

4) 实现一个main函数,在main函数中至少完成如下工作:

?实例化一个CCube类的对象a_cube和CSphere类的对象c_sphere; ?定义一个CStereoShape类的指针p;

?将a_cube的长、 宽和高分别设置为4、 5和6; 将p指向a_cube, 通过p将a_cube 的表面积和体积打印到屏幕上;

?将c_sphere的半径设置为7;将p指向c_sphere,通过p将c_sphere的表面积和 体积打印到屏幕上。

#include

#include using namespace std; const int Pi = 3.1415; class CSreroShape { private: double area, volume; public:

virtual double GetArea(){return 0;}

面向对象程序设计 lab15 学号 班级 姓名

virtual double GetVolume(){return 0;} }; class CCube : public CSreroShape { private:

double length, width, height; public: CCube(double l, double w, double h) { length = l; width = w; height = h; } ~CCube(); virtual double GetArea() const { return 2 * (length * width + length * height + height * width); } virtual double GetVolume() const { return length * width * height; } }; class CSphere : public CSreroShape { int radius; public: CSphere(int r) { radius = r; } virtual double GetArea() { return 4 * Pi * pow(radius, 2); } virtual double GetVolume() { return 4.0 / 3 * Pi * pow(radius, 3); } }; int main(void) { CCube *a_cube = new CCube(4, 5, 6); CSphere *c_sphere = new CSphere(7); CSreroShape *p; p = a_cube; cout<<\ cout<<\olume is \ delete p; p = c_sphere; cout<<\ cout<<\olume is \ delete p; return 0; }

2、设计字符串类String,用来存放不定长的字符串,重载运算符“= =”,“>”,“<”,用于两个字符串的大于、小于和等于的比较运算。 [实验提示]

面向对象程序设计 lab15 学号 班级 姓名

1、= = 、> 、< 属于双目运算符,在重载双目运算符时,函数中应该有两个参数。 2、String类成员变量应为字符型指针; 3、多个构造函数的定义;

#include #include class string {

protected: char *sp; public:

string(){sp=0;} //构造函数 string(string &); //复制构造函数 string(char *s) //构造函数 {

sp=new char[strlen(s)+1]; strcpy(sp,s); }

~string (){if(sp)delete sp;} //析构函数 void show(){cout<

string & operator = (string &); //赋值运算符重载

friend string operator + (string &,string &); //友元函数实现加法运算符重载 string operator - (string &); //一个字符串减去另一个字符串 string operator - (char); //一个字符串减去指定字符 int operator > (string &); //比较运算 };

string::string(string &s)

面向对象程序设计 lab15 学号 班级 姓名

{

if(s.sp){

sp=new char[strlen(s.sp)+1]; strcpy(sp,s.sp); } else sp=0; }

string operator + (string &s1,string &s2) {

string t;

t.sp=new char[strlen(s1.sp)+strlen(s2.sp)+1]; strcpy(t.sp,s1.sp); //字符串复制函数 strcat(t.sp,s2.sp); //字符串并接函数 return t; }

string string::operator - (string &s) {

string t1=*this; char *p; while(1) {

if(p=strstr(t1.sp,s.sp)) //strstr()返回的是,s.sp第一次在t1.sp出现时候的位置,赋值给指针p {

if(strlen(t1.sp)==strlen(s.sp)) //如果两个字符串完全相同 {

delete t1.sp; //则全部删除 t1.sp=0; //把他的值赋值为0 break; //返回 }

string t2;

t2.sp=new char[strlen(t1.sp)-strlen(s.sp)+1]; char *p1=t1.sp,*p2=t2.sp; int i=strlen(s.sp);

while(p1

while(i){p1++;i--;} //i是要删除的字符串的长度,p1要跳过这些字符;

while(*p2++=*p1++); //后面的无论相不相同都储存下来,等下一次循环再作处理; t1=t2; }

else break; }

return t1;

面向对象程序设计 lab15 学号 班级 姓名

}

string string::operator - (char s) {

string t1;

int i=0,flag=0;

t1.sp=new char[strlen(sp)+1]; char *p1=sp,*p2=t1.sp; while(*p1) {

if(*p1!=s){

*p2++=*p1++; i++; flag=1; }

else p1++; }

*p2='\\0'; return t1; }

int string::operator > (string &s) {

if(strcmp(sp,s.sp)>0) //strcmp()函数,作用是比较两个字符串的大小 {

return 1; }

else return 0; }

string & string::operator = (string &s) //因为这个类中有指针对象,所以要自定义赋值运算符重载,注意这里要引用 {

if(sp) delete sp; //先将原有的空间释放掉 if(s.sp) {

sp=new char[strlen(s.sp)+1]; //再申请内存赋值 strcpy(sp,s.sp); }

else sp=0; return *this; }

void main() {

string s1(\ s1.show(); s2.show();

面向对象程序设计 lab15 学号 班级 姓名

s3=s1+s2; s3.show(); if(s1>s2)

cout<<\成立!\ else

cout<<\不成立!\ s4=s3-s2; s4.show(); s5=s3-'t'; s5.show(); }

[测试数据]

1、 China china 2、 National Computer 3、 Examination Rank 4、 swust swust

3、异常处理

创建一个表示圆型的类Circle,其中包含表示半径的数据成员radius,当创建Circle类对象时,需要初始化具体对象的数据成员radius,如果使用一个小于零的值初始化对象的半径,则会出现运行时异常,使用C++异常处理机制捕获并处理这种可能产生的异常。

#include

class circle {

private:

面向对象程序设计 lab15 学号 班级 姓名

int r; public:

void input() {

cout<<\输入半径\ cin>>r; }

double S() {

return 3.14*r*r; }

void output() { cout<<\半径: \面积: \ } };

void main() {

circle circle11; double r,R; circle11.input(); circle11.S(); circle11.output(); }

本文来源:https://www.bwwdw.com/article/ajxo.html

Top