c++输入输出流实验报告要点.docx

上传人:b****2 文档编号:1864486 上传时间:2022-10-24 格式:DOCX 页数:24 大小:272.52KB
下载 相关 举报
c++输入输出流实验报告要点.docx_第1页
第1页 / 共24页
c++输入输出流实验报告要点.docx_第2页
第2页 / 共24页
c++输入输出流实验报告要点.docx_第3页
第3页 / 共24页
c++输入输出流实验报告要点.docx_第4页
第4页 / 共24页
c++输入输出流实验报告要点.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

c++输入输出流实验报告要点.docx

《c++输入输出流实验报告要点.docx》由会员分享,可在线阅读,更多相关《c++输入输出流实验报告要点.docx(24页珍藏版)》请在冰豆网上搜索。

c++输入输出流实验报告要点.docx

c++输入输出流实验报告要点

实验四输入输出流

实验课程名:

面向对象程序设计(C++)

专业班级:

学号:

姓名:

实验时间:

实验地点:

指导教师:

一、实验目的和要求

(1)理解类和对象的概念,掌握声明类和定义对象的方法。

(2)掌握构造函数和析构函数的实现方法。

(3)初步掌握使用类和对象编制C++程序。

(4)掌握对象数组、对象指针和string类的使用方法。

(5)掌握使用对象、对象指针和对象引用作为函数参数的方法。

(6)掌握类对象作为成员的使用方法。

(7)掌握静态数据成员和静态成员函数的使用方法。

(8)理解友元的概念和掌握友元的使用方法。

 

二、实验内容

1.定义描述职工工资的类Laborage,数据成员为职工号(No)、姓名(Name[8])、应发工资(Ssalary)、社保金(Security)、实发工资(Fsalary)。

定义公有成员函数Input(),在Input()函数内输入职工号、姓名、应发工资、社保金,实发工资由公式:

Fsalary=Ssalary-Security计算。

定义输出职工工资的成员函数Show()。

在显示函数Show()中,职工号、姓名的输出域宽为8、左对齐,其余数据的输出域宽为10、右对齐、保留小数点后两位,输出格式均用预定义格式控制函数设置。

在主函数中用Laborage类定义职工对象数组a[3]。

用Input()输入职工工资,用Show()显示每个职工的工资。

(提示:

用getline输入姓名后,必须用回车结束姓名输入)

实验数据:

1001ZhouZhi3000200

1002ChenHua4000400

1003WangFan5000500

实验代码:

#include

#include

#include

usingnamespacestd;

classLaborage

{

public:

Laborage(){}

voidinput();

voiddisplay();

private:

intnum;

charname[10];

floatSsalary;

floatSecurity;

floatFsalary;

};

voidLaborage:

:

input()

{

cin>>num;

cin.get(name,10,'\n');

cin>>Ssalary;

cin>>Security;

Fsalary=Ssalary-Security;

}

voidLaborage:

:

display()

{

cout<

:

right)<

:

left)<

cout<

:

left)<

cout<

:

right)<

:

fixed)<

(2)<

cout<

:

right)<

:

fixed)<

(2)<

cout<

:

right)<

:

fixed)<

(2)<

cout<

}

intmain(void)

{

Laboragelab[3];

inti;

for(i=0;i<3;i++)

{

cout<<"请输入第"<

";

lab[i].input();

}

for(i=0;i<3;i++)

{

lab[i].display();

}

return0;

}

实验结果:

代码分析:

1)在输入时调用getline()以换行符作为输入姓名时的结束标志,已达到输入空格的目的

2)输出时采用resetiosflags(ios:

:

right)实现结束向左对齐,用setw(8)实现输出域宽为8,使用一系列的格式控制字符来实现输出格式的控制。

2.重载运算符“<<”和“>>”,使其能够输入一件商品的信息和输出这件商品的信息。

商品的信息由编号、商品名和价格。

假如商品类Merchandise的框架如下:

classmerchandise{

public:

Merchandiss();

~Merchandiss();

friendistream&operator>>(istream&in,Merchandiss&s);

friendostream&operator<<(ostream&out,Merchandiss&s);

private:

intno;

char*name;

doubleprice;

};

要求实现该类,并编写以下的main函数对该类进行操作。

intmain()

{Merchandisemer;

cin>>mer;

cout<

return0;

}

实验代码:

#include

usingnamespacestd;

classmerchandise{

public:

merchandise(){no=0;name[0]='\0';price=1;}

~merchandise(){}

friendistream&operator>>(istream&in,merchandise&s);

friendostream&operator<<(ostream&out,merchandise&s);

private:

intno;

charname[100];

doubleprice;

};

istream&operator>>(istream&in,merchandise&s)

{

cout<<"请输入商品信息(编号、名称、价格):

";

in>>s.no;

in.getline(s.name,100,'\n');

in>>s.price;

returnin;

}

ostream&operator<<(ostream&out,merchandise&s)

{

cout<<"商品信息显示如下:

"<

out<

returnout;

}

intmain()

{

merchandisemer;

cin>>mer;

cout<

return0;

}

实验结果:

实验分析:

先定义一个商品的类,构造函数定义为无参函数在函数体中将name置为空,其他为0.在重载>>时使用getline函数,输入name以’\n’作为结束标记

重载<<时直接输出。

在主函数中调用这些函数实现输入输出的功能。

3.将一个源文件复制为两个不同名目的文件,源文件与目的文件均用构造函数打开,使用成员函数get与put复制第一个目的文件,使用getline与插入运算符复制第二个目的文件。

(提示:

用get函数将输入文件流对象的指针指向文件尾后,无法将该指针移到文件首位置。

所以只能定义两个输入文件流对象打开同一源文件,用于两种方式的文件复制。

实验数据:

源文件:

e:

\ex\a.txt,文件内容为soucefile

目的文件1:

e:

\ex\b.txt

目的文件2:

e:

\ex\c.txt

实验代码:

#include

#include

#include

usingnamespacestd;

voidcreatefile()

{

ofstreamoutfile("a.txt");

if(!

outfile)

{

cerr<<"opena.txterror!

"<

exit

(1);

}

charstr[100];

cin.getline(str,100,'\n');

outfile<

outfile.close();

}

voidcopyfile_b()

{

ofstreamoutfile("b.txt");

if(!

outfile)

{

cerr<<"openb.txterror!

"<

exit

(1);

}

ifstreaminfile("a.txt");

if(!

infile)

{

cerr<<"opena.txterror!

"<

exit

(1);

}

charch;

while(infile.get(ch))

{

outfile<

}

outfile.close();

infile.close();

}

voidcopyfile_c()

{

ofstreamoutfile("c.txt");

if(!

outfile)

{

cerr<<"openc.txterror!

"<

exit

(1);

}

ifstreaminfile("a.txt");

if(!

infile)

{

cerr<<"opena.txterror!

"<

exit

(1);

}

charch;

while(infile.get(ch))

{

outfile<

}

outfile.close();

infile.close();

}

voiddisplay(char*filename)

{

ifstreaminfile(filename);

if(!

infile)

{

cerr<<"openthefileerror!

"<

exit

(1);

}

charch;

while(infile.get(ch))

{

cout.put(ch);

}

cout<

infile.close();

}

intmain()

{

createfile();

copyfile_b();

copyfile_c();

cout<<"a文?

件t中D的Ì?

内¨²容¨Y为a:

êo";

display("a.txt");

cout<<"b文?

件t中D的Ì?

内¨²容¨Y为a:

êo";

display("b.txt");

cout<<"c文?

件t中D的Ì?

内¨²容¨Y为a:

êo";

display("c.txt");

return0;

}

实验结果:

定义几个函数分别实现:

创建文件、复制文件、读取文件中的内容到显示器

在主函数中调用创建函数,创建一个文件a,调用复制文件的函数将a中的内容复制到文件b,c中在调用读取文件的函数将a、b、c中的内容输出到显示器中。

4.将存放在源文件(e

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1