ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:18.62KB ,
资源ID:7148964      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7148964.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++实验代码.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++实验代码.docx

1、C+实验代码实验项目一 C+程序设计初步题目欧阳光明(2021.03.07)在Visual Studio 2008平台上,用C+语言创建一个控制台应用程序,当其运行时在屏幕上显示“Hello,World!”(参考P15)实验项目二类与对象(1)有以下程序:#include using namespace std;class Time /定义Time类public:/数据成员为公用的 int hour; int minute;int sec;int main( ) Time t1;/定义t1为Time类对象 cint1.hour;/输入设定的时间 cint1.minute; cint1.sec;

2、 coutt1.hour“:”t1.minute “:”t1.secendl;/输出时间 return 0;改写程序,要求:将数据成员改为私有的;将输入和输出的功能改为由成员函数实现;在类体内定义成员函数。然后编译和运行程序。请分析什么成员应指定为公用的?什么成员应指定为私有的?什么函数最好放在类中定义?什么函数最好放在类外定义?代码:/ 实验_1.cpp : 定义控制台应用程序的入口点。#includestdafx.h#includeusingnamespace std;class Time /定义Time类private:/数据成员为私有的int hour;int minute;int s

3、ec;public:void Input()cout请输入时、分、秒:hourminutesec;void print()cout时间为:endl; couthour:minute :secendl;int _tmain(int argc, _TCHAR* argv) Time t1; t1.Input (); t1.print ();return 0;结果:(2)修改实验1中的学生成绩管理系统,建立类OurClass,实现班级学生信息的输入,排序和输出。要求:添加构造函数,初始化班级名称,将成员函数定义在类外,类的定义单独放在头文件中。代码:头文件:#define N 3struct Stu

4、dentlong lNum;char sName12;float cGrade;float ccGrade;float fGrade;class OurClassprivate:char cName20;Student stuN;public:OurClass:OurClass(); void Input();void print();void Sort();.cpp:/ 学生成绩管理.cpp : 定义控制台应用程序的入口点。#includestdafx.h#includeiostreamusingnamespace std;#includestudent.hint main()OurClas

5、s c1; c1.Input();c1.Sort ();coutendl排序结果为:endlendl;c1.print ();return 0;OurClass:OurClass () /(char Nme20) cout请输入班级名称:cName;void OurClass:Input ()int i;cout输入N个学生的学号、姓名和成绩endl;for(i=0;istui.lNumstui.sName stui.cGrade stui.ccGrade ;stui.fGrade =stui.cGrade+stui.ccGrade; void OurClass:Sort() int i,j;

6、Student Temp;for(i=0;iN;i+)for(j=i+1;jN;j+)if (stui.fGradestuj.fGrade )Temp=stui;stui=stuj;stuj=Temp;void OurClass:print() int i; cout班级名称:; coutcNameendl; cout学号姓名成绩endl;for(i=0;iN;i+) coutstui.lNum stui.sName stui.fGrade endl;结果:(3)需要求3个长方柱的体积(用对象数组完成),请编一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高

7、)。要求用成员函数实现以下功能:由键盘分别输入3个长方柱的长、宽、高;计算长方柱的体积;输出3个长方柱的体积。请编程序,上机调试并运行。完成后,将其中的数组名当常量指针用。代码:头文件:class cubeprivate:double length;double width;double height;public:cube();void input();void print();double volume();/ 求长方柱的体积.cpp : 定义控制台应用程序的入口点。#includestdafx.h#includeiostreamusingnamespace std;#includecub

8、e.hvoid cube:input()double length;double width;double height;cube:cube() cout请输入长方柱的长、宽、高:lengthwidthheight;double cube:volume()return length*width*height;void cube:print() cout长方柱的体积为:volume()endl;int main()cube c3;for(int i=0;iprint();return 0;结果:(4)友元函数(1)有以下程序:#include using namespace std;class

9、Date; /对Date类的提前引用声明class Time /定义Time类public:Time(int,int,int);void display(Date &); /display是成员函数,形参是Date类对象的引用 private:int hour;int minute;int sec;class Date /声明Date类public:Date(int,int,int);friend void Timedisplay(Date &); /声明Time中的display函数为友元成员函数 private:int month;int day;int year;TimeTime(int

10、 h,int m,int s) /类Time的构造函数hour=h;minute=m;sec=s;void Timedisplay(Date &d) /display的作用是输出年、月、日和时、分、秒coutd.month“/”d.day“/”d.yearendl;/引用Date类对象中的私有数据couthour“:”minute“:”secendl; /引用本类对象中的私有数据DateDate(int m,int d,int y) /类Date的构造函数month=m;day=d;year=y;int main( )Time t1(10,13,56); /定义Time类对象t1Date d1

11、(12,25,2004); /定义Date类对象d1t1.display(d1); /调用t1中的display函数,实参是Date类对象d1return 0;请读者分析和运行此程序,注意友元函数Time:display的作用。将程序中的display函数不放在Time类中,而作为类外的普通函数,然后分别在Time和Date类中将display声明为友元函数。在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的私有数据,输出年、月、日和时、分、秒。修改后上机调试和运行。代码:/ 实验_4.cpp : 定义控制台应用程序的入口点。#includestdaf

12、x.h#includeusingnamespace std;class Date; /对Date类的提前引用声明class Time /定义Time类friendvoid display(Date &,Time &);public:Time(int,int,int);/void display(Date &); /display是成员函数,形参是Date类对象的引用private:int hour;int minute;int sec;class Date /声明Date类public:Date(int,int,int);friendvoid display(Date &,Time &);pu

13、blic:int month;int day;int year;Time:Time(int h,int m,int s) /类Time的构造函数hour=h; minute=m; sec=s;void display(Date &d,Time &t1) /display变为普通函数coutd.month/d.day/d.yearendl; /引用Date类对象中的私有数据coutt1.hour:t1.minute:t1.secendl; Date:Date(int m,int d,int y) /类Date的构造函数month=m;day=d;year=y;int _tmain(int arg

14、c, _TCHAR* argv)Time t1(10,13,56); /定义Time类对象t1Date d1(12,25,2004); /定义Date类对象d1display(d1,t1);return 0;结果:实验项目三 函数重载(1)编写程序,实现普通函数重载练习。 求三角型,矩形,圆形的面积和周长,其中面积函数和周长函数重载。代码:class Zhc /Area.hprivate:double a,b,c;double r;public: Zhc (double a,double b,double c); Zhc (double a,double b); Zhc (double r);

15、class Area /Area.hprivate:double a,b,c;double r;public: Area(double a,double b,double c); Area(double a,double b); Area(double r);/ 三角形等面积及周长.cpp : 定义控制台应用程序的入口点。#includestdafx.h#includeiostreamusingnamespace std;#includeArea.h#includeZhc.h#includemath.hint _tmain(int argc, _TCHAR* argv) double a,b,

16、c;double r;cout请输入三角形的三条边a,b,c: abc; cout三角形的面积为: sqrt(a+b+c)/2*(a+b+c)/2-a)*(a+b+c)/2-b)*(a+b+c)/2-c)endl; cout三角形的周长为:a+b+cendl;cout请输入矩形的两条边a,b:ab; cout矩形的面积为:a*bendl; cout矩形的周长为:2*(a+b)endl;cout请输入圆的半径:r; cout圆的面积为: r*r*3.14endl; cout圆的周长为: 2*r*3.14endl;double Area(double a,double b,double c) re

17、turn sqrt(a+b+c)/2*(a+b+c)/2-a)*(a+b+c)/2-b)*(a+b+c)/2-c); double Area(double a,double b) return a*b;double Area(double r) return r*r*3.14; double Zhc(double a,double b,double c) return a+b+c;double Zhc(double a,double b) return 2*(a+b);double Zhc(double r) return 2*r*3.14;结果:(2)编写程序,(注意复数构造函数的重载及构造

18、函数分带默认参数和不带默认参数的情况) 求两个复数的和,复数考虑有实部和虚部、无实部、无虚部三种情况代码:头文件:#define N 2class Complexprivate:double real;double imag;public:Complex(double r=0,double i=0);void Input();void Print();friend Complex Add(Complex c1,Complex c2);/ 复数相加.cpp : 定义控制台应用程序的入口点。#includestdafx.h#includeiostreamusingnamespace std;#in

19、cludecomplex.hint main()Complex total=0;Complex cN;for(int m=0;mN;m+)cout请输入第m+1个复数:;cm.Input();total=Add(total,cm);coutrealimag; cout0) cout+; coutimagiendl; elseif(imag=0) coutendl;Complex Add(Complex c1,Complex c2)c1.real=c1.real+c2.real;c1.imag=c1.imag+c2.imag;return c1;void Complex:Print()cout0

20、)cout+;if(imag!=0)coutimagiendl;结果:(3)以上实验做完,练习书上的例题。实验项目四 常量与引用1、有关const的上机内容:(1)、运行课本中的例4-5,分析查看 ,然后逐个将程序中的const 删除,分析运行的结果;代码:/student.h#ifndef STUDENT_H_#define STUDENT_H_class Studentint No;char Name20;public :Student();int GetNo();char*GetName();#endif/student.cpp#includestdafx.h#include#inclu

21、destudent.hStudent:Student()No=1;strcpy(Name,wang);int Student:GetNo()return No;char*Student:GetName()return Name;/ 例_5.cpp : 定义控制台应用程序的入口点。#includestdafx.h#includestudent.hint main()Student s1;s1.GetNo();s1.GetName(); Student s2;s2.GetNo();/s2.GetName();return 0;结果:(2)、运行课本中的例4-10与例4-11,对比两个程序中指针和引用。例4-10:/ 例_10.cpp : 定义控制台应用程序的入口点。#includestdafx.h#includeusingnamespace std;void changpointer1(int* x) (*x)+; *x=1;void changpointer2(int*& x)x+; *x=2;int main()int iData3=0;int *p=iData;int i;

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

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