您好,欢迎访问三七文档
中国地质大学江城学院VC++实验报告姓名熊威班级学号2611100117指导教师刘艳2012年4月26日目录实验一类和对象...........................................................................................11实验内容.................................................................................................12实验准备和说明.....................................................................................13实验步骤.................................................................................................14运行结果.................................................................................................3实验二运算符的重载和文件流...................................................................41实验内容.................................................................................................42实验准备和说明.....................................................................................43实验步骤.................................................................................................44运行结果.................................................................................................5实验三对话框和相关控件...........................................................................61实验内容.................................................................................................62实验准备和说明.....................................................................................63实验步骤.................................................................................................64运行结果.................................................................................................7实验四窗口界面设计...................................................................................81实验内容.................................................................................................82实验准备和说明.....................................................................................83实验步骤.................................................................................................84运行结果.................................................................................................91实验一类和对象1实验内容定义一个字符串类CStrOne,它包含一个存放字符串的数据成员;能够通过构造函数初始化字符串,通过成员函数显示字符串的内容。在此基础上派生出CStrTwo类,它增加一个存放字符串的数据成员,并能通过派生类的构造函数传递参数,初始化两个字符串,还能通过成员函数进行两个字符串的合并以及输出(字符串合并可使用标准库函数strcat,且需要包含头文件string.h)。编写一个完整的程序进行上机测试,并要求实现头文件和源文件分离。2实验准备和说明VisualC++6.03实验步骤a.打开VisualC++6.0b.新建一个工程1,在工程1中新建一个源程序1,1.cpp中的代码如下:#includeiostream.h#includestring.hclassCStrOne{public:charstring1[10];CStrOne(charstr[]){strcpy(string1,str);}voidprint(){coutstring1endl;}};classCStrTwo:publicCStrOne{public:charstring2[10];CStrTwo(charstr[],charstr1[]):CStrOne(str){strcpy(string2,str1);}voidunions(){strcat(string2,CStrOne::string1);coutstring2endl;}};voidmain(){charstr1[]=nicetomeetyou;charstr2[]=hi;CStrOneone(str1);CStrTwotwo(str1,str2);two.unions();}2c.头文件和源文件分离新建两个源程序CStrOne.cpp和CStrTwo.cpp,CStrOne.cpp和CStrTwo.cpp中的代码分别如下:/*CStrOne.cpp*/#includeiostream.h#includestring.h#includeCStrOne.hCStrOne::CStrOne(charstr[]){strcpy(string1,str);}voidCStrOne::print(){coutstring1endl;}/*CStrTwo.cpp*/#includeiostream.h#includestring.h#includeCStrTwo.hCStrTwo::CStrTwo(charstr[],charstr1[]):CStrOne(str1){strcpy(string2,str);}voidCStrTwo::unions(){strcat(string2,CStrOne::string1);coutstring2endl;}新建两个源程序CStrOne.h和CStrTwo.h,CStrOne.h和CStrTwo.h中的代码分别如下:/*CStrOne.h*/classCStrOne{public:charstring1[20];CStrOne(charstr[]);voidprint();};/*CStrTwo.h*/#includeCStrOne.hclassCStrTwo:publicCStrOne{public:charstring2[10];CStrTwo(charstr[],charstr1[]);voidunions();}31..cpp中的代码如下:#includeiostream.h#includestring.h#includeCStrTwo.hvoidmain(){charstr1[]=nicetomeetyou;charstr2[]=hi;CStrOneone(str1);CStrTwotwo(str1,str2);two.unions();}4运行结果4实验二运算符的重载和文件流1实验内容建立一个二进制文件,用来存放自然数1~20及其平方根,然后输入1~20之内的任意一个自然数,查找出其平方根显示在屏幕上。2实验准备和说明VisualC++6.03实验步骤a.打开VisualC++6.0b.新建一个工程1,在工程1中新建一个源程序1,1.cpp中的代码如下:#includeiostream.h#includefstream.h#includemath.hclassB{intx;doubley;public:voidget(ints){x=s;y=sqrt(s);}voiddisplay(){coutxyendl;}friendostream&operator(ostream&os,B&f1);friendistream&operator(istream&is,B&f1);};ostream&operator(ostream&os,B&f1){os.write((char*)&f1.x,4);os.write((char*)&f1.y,8);returnos;}istream&operator(istream&is,B&f1){is.read((char*)&f1.x,4);is.read((char*)&f1.y,8);returnis;}voidmain(){fstreamf1;f1.open(data.txt,ios::binary|ios::out|ios::in);if(!f1){coutdata.txt不能打开!5;return;}for(inti=1;i21;i++){Ba;a.get(i);f1a;}while(1){intj;cout请输入1-20内的数:;cinj;if(j1||j20){cout您输入的数据不在该范围内,请正确输入!endl;break;}f1.seekg(12*(j-1));Bb;f1b;b.display();}f1.close();}4运行结果6实验三对话框和相关控件1实验内容制作一个“用户登录”对话框,包含确定和重置两个按钮。当用户输入的“用户名”和“密码”正确时,单击确定显示“登录成功!”,否则显示“用户名密码错误,请重新输入!”单击重置,将用户名密码所对应编辑框的内容置为空。2实验准备和说明VisualC++6.03实验步骤a.打开VisualC++6.0b.新建一个基于对话框的应用程序用户登录Researchc.设计”用户登录”对话框a.打开对话框资源IDD_用户登录Research_DIALOGb.添加两个静态文本标题为”用户名”“密码”控件c.两个编辑框d.三个按钮标题为”确定””取消”“重置”d.在CMyDlg::OnOK函数中添加下列代码:voidCMyDlg::OnOK(){//TODO:AddextravalidationhereUpdateData();if(m_name==LiMing&&m_mm==886688){MessageBox(登录成功!);CDialog::OnOK();}elseMessageBox(用户名密码错误,请重新输入!);}在v
本文标题:VC++
链接地址:https://www.777doc.com/doc-5993449 .html