C++面向对象程序设计实验指导书.docx
- 文档编号:2198787
- 上传时间:2022-10-27
- 格式:DOCX
- 页数:27
- 大小:174.40KB
C++面向对象程序设计实验指导书.docx
《C++面向对象程序设计实验指导书.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计实验指导书.docx(27页珍藏版)》请在冰豆网上搜索。
C++面向对象程序设计实验指导书
《面向对象程序设计(C++)》课程实验指导书
安阳工学院计算机科学与信息工程学院
软件工程教研室
2011.8
编号:
课程总学时:
64实验学时:
32
课程总学分:
3.5实验学分:
先修课程:
C语言程序设计
适用专业:
计算机科学与技术,网络工程,软件工程
一、本课程实验的主要目的与任务
《面向对象程序设计(C++)》是计算机专业学生的一门专业基础课。
C++是一种高效而又实用的程序设计语言,它既可以进行过程化程序设计,也可以进行面向对象程序设计,因此成为了编程人员最广泛使用的工具。
主要任务是介绍C++语言中的数据类型,运算,语句结构及其程序设计的基本方法。
使学生掌握一门高级程序设计语言,了解面向对象程序设计的基本概念与方法,进而学会利用C++语言学会解决一般应用问题,从而掌握面向对象程序设计的基本知识和基本技能。
并为后续的专业课程奠定程序设计基础。
实验1C++基础
一.实验目的
1.加强学生掌握C++的基本知识点
二.准备知识
1.C++基本数据类型、控制结构语句
2.IO流输入/输出语句的使用。
三.实验内容
1.使用IO流输出语句,分三行输出“MynameisJone\n”,“theIDis”,“2”。
2.分别用浮点、定点和指数方式显示22.0/7的计算结果。
3.分别用16、8、10进制显示输出十进展数1001。
4.分别用显示宽度为2,3,4显示输出21,其中用“*”填充间隔。
5.分别用左对齐、右对齐两种方式显示输出1,2,3三个数。
6.输入圆的半径,计算并输出它的周长和面积。
其中π=3.1415926,使用常量定义π。
四.实验指导
1.参考程序:
#include
intmain()
{
cout<<”MynameisJone\n”;
cout<<”theIDis”;
cout<<2;
cout< } 2.参考程序: #include #include voidmain() { doubleamount=22.0/7; cout< cout< < (1)< < (2)< < < cout< : fixed); cout< cout< : scientific)< cout< } 3.参考程序: #include #include intmain() { intnumber=1001; cout<<"Decimal: "< <<"Hexadecimal: "< <<"Octal: "< return0; } 4.参考程序: #include #include intmain() { cout< < (2)<<21< < < cout< return0; } 5.参考程序: #include #include voidmain() { cout< : right) < < < cout< : left) < < < } 6.参考程序: #include voidmain() { constfloatPI=3.1415926f; floatr; floatz,s; cout<<"请输入圆的半径r="; cin>>r; z=2*PI*r; s=PI*r*r; cout<<"圆的周长为: "< cout<<"圆的面积为: "< } 实验2类和对象 1、实验目的: 掌握类的定义,根据具体需求设计类 深入理解C++中类的封装性。 会根据类创建各种对象 掌握对象的各种成员的使用方法。 2、实验内容 定义一个满足如下要求的Date类。 (1)用下面的格式输出日期: 日/月/年 (2)可运行在日前上加一天操作 (3)设置日期。 代码如下: #include classDate{ public: voidDisplay(); voidAddOneDay(); voidSetDay(inty,intm,intd); protected: boolLegal(inty,intm,intd); boolIsLeapYear(inty); intyear; intmonth; intday; }; voidDate: : Display() { cout< } voidDate: : AddOneDay() { if(Legal(year,month,day+1)) day++; elseif(Legal(year,month+1,1)) month++,day=1; elseif(Legal(year+1,1,1)) day=1,month=1,year++; } voidDate: : SetDay(inty,intm,intd) { if(Legal(y,m,d)) day=d,month=m,year=y; } boolDate: : Legal(inty,intm,intd) { if(y>9999||y<1||d<1||m<1||m>12) returnfalse; intdayLimit=31; switch(m)case4: case6: case9: case11: dayLimit--; if(m==2)dayLimit=IsLeapYear(y)? 29: 28; return(d>dayLimit)? false: true; } boolDate: : IsLeapYear(inty) { return! (y%4)&&(y%100)||! (y%400); } intmain() { Dated; d.SetDay(2010,2,28); d.Display(); d.AddOneDay(); d.Display(); system("PAUSE"); } 运行结果: 实验3继承与派生 1、实验目的: 理解继承的概念,学习如何使用基类成员,了解基类成员在派生类中的访问控制;理解类的派生对代码复用的作用; 2、实验内容: 设计一个人员类person和一个日期类date,由人员类派生出学生类Student和教师类professor,学生类和教师类的数据成员birthday为日期类。 #include #include usingnamespacestd; classdate { public: date() { cout<<"Birthday: "; cin>>year>>month>>day; } voiddisplay() { cout< } private: intyear; intmonth; intday; }; classperson { protected: char*name; public: person(); }; person: : person() { charnamestr[50]; cout<<"Name: "; cin>>namestr; name=newchar[strlen(namestr)+1]; strcpy(name,namestr); } classstudent: publicperson { private: intID; intscore; datebirthday; public: student() { cout<<"studentID: "; cin>>ID; cout<<"studentscore: "; cin>>score; } voiddisplay() { cout<<"Thebasicinformation: "< cout< birthday.display(); cout< } }; classprofessor: publicperson { private: intNo; charmajor[10]; datebirthday; public: professor() { cout<<"TeacherID: "; cin>>No; cout<<"schoolteachingmajor: "; cin>>major; } voiddisplay() { cout<<"Thebasicinformation: "< cout<<"\t"< birthday.display(); cout< } }; intmain() { studentstu; stu.display(); professorprof; prof.display(); system("PAUSE"); return0; } 运行结果: 实验4运算符重载 1、实验目的: 掌握运算符重载的概念及使用方法,掌握特殊运算符的重载要求和方法。 2、实验内容: 定义整数集合类intS
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 面向 对象 程序设计 实验 指导书