天津理工大学C++期末考试.doc





《天津理工大学C++期末考试.doc》由会员分享,可在线阅读,更多相关《天津理工大学C++期末考试.doc(35页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验一 一、 明确的概念:(1)理解类和对象的概念;(具体见附一) 类是抽象的 对象是具体的 例如:你、我、他 都是人 那么 你、我、他就是对象 人就是类(2)掌握类与对象的定义方法; 类的定义:class 类名 /*定义这个类*/; /(别忘了最后的分号) 对象的定义: 类名 对象名(参数);(3)理解类的成员的访问控制的含义,公有和私有成员的区别; public:/访问属性为公有 通过外部直接访问 即:对象名.成员名或成员函数 private:/访问属性为私有 不能通过外部直接访问(不声明访问属性时 系统默认为私有) 当出现对象名.成员(私有)时 编译器报错 protected:/访问属性
2、为保护 与private类似 但protected属性的成员可以继承 private不能继承(4)掌握构造函数和析构函数的含义与作用、定义方式和实现; 构造函数:为数据成员开辟存储空间函数名必须与类名相同 之前绝对不许说明返回值类型 void也不行构造函数不能由用户调用 即:对象名.构造函数名(参数)是错误的 构造函数是随着对象的声明而调用的例如:class StudentStudent()cout”Hello.”endl;Int main(void)cout”Hello.”endl;Student A;运行结果:Hello./-这个Hello是主函数中的Hello./-是声明A对象时调用的构
3、造函数 构造函数中的Hello 析构函数功能与构造函数相反 注意调用时间和顺序(看书)附:如何知道 该程序析构函数的调用顺序是p3 p2 p1 ?答:将拷贝构造函数中 改为x=2*p.x; y=2*p.y析构函数中 在cout”Destructor is called.”之后添加”x=”x” y=”y后面的不变运行时 析构函数输出结果变为:Destructor is called. x=12 y=16Destructor is called. x=6 y=8Destructor is called. x=3 y=4Press any key to continue由此可知其调用顺序或者在拷贝构
4、造函数和析构函数中的cout语句的 endl; 之前加入this 来输出对象地址 再对号入座也可知调运顺序二、 练习项目1.输入下列程序,按要求进行实验,并记录实验的结果。#include using namespace std;class Coordinatepublic:Coordinate(int x1, int y1)x=x1; y=y1;Coordinate(Coordinate &p);Coordinate()coutDestructor is called.endl;int getx()return x;int gety()return y;private:int x, y;Co
5、ordinate:Coordinate(Coordinate &p)/拷贝构造函数x=p.x; y=p.y;coutCopy initianization constructor is called.endl;运行结果:Copy initianization constructor is called.Copy initianization constructor is called.p3=(3,4)Destructor is called.Destructor is called.Destructor is called.Press any key to continue调用构造函数调用拷贝
6、构造函数(见书P89)int main()Coordinate p1(3,4);Coordinate p2(p1);Coordinate p3=p2;coutp3=(p3.getx(),p3.gety()endl;程序结束时 调用析构函数 注意析构函数的调用顺序 从上至下 依此为 p3 p2 p1的析构函数return 0;(1) 记录程序的运行结果(2) 将Coordinate类中带有两个参数的构造函数进行修改,在函数体内增加如下语句:cout”Constructor is called.”endl;重新记录程序的运行结果,并解释输出结果。(3) 按下列要求进行测试。在主函数体内,添加如下语
7、句:Coordinate p4; 错误原因:构造函数没有参数 构造函数为Coordinate:Coordinate(int ,int)Coordinate p5(2); 错误原因:参数数量不对2.根据注释语句的提示实现类Date的成员函数,并将完整的程序编译、连接成功以保证程序能够正确运行。#include class Datepublic:void printDate();/显示日期分析:1.题目中 Date类的数据成员为day, month, year 且均为整型访问属性为私有 主函数中声明了testDay对象2.数据成员均为私有 则不能通过tesDay.day=的方法来设置 题目中给出了
8、 void setDay(int) void setMonth(int) 和void setYear(int)三个成员函数去是指day month 和 year 的值 还有一个void printDate()来输出3.准备工作结束 开始编程 看程序 仅差4个成员函数未定义 类外定义 要写出作用域 即该函数属于哪个类: 格式: 返回值类型 作用域:函数名 参数表 void Date:setDay (int d)/*定义时必须要给形参一个名字 这里给的是*/d day=d/设置了day的值 其他函数依此类推 输出函数就不用再唠叨了4.验证结果 出现溢出数据时查看是否写反了 例如写成d=day 了解
9、设置函数的作用机制 见附二void setDay(int);/设置日的值void setMonth(int); /设置月的值void setYear(int); /设置年的值private:int day, month, year;int main()Date testDay;testDay.setDay(5);testDay.setMonth(10);testDay.setYear(2010);testDay.printDate();void Date:setDay(int d)day=d;void Date:setMonth(int m)month=m;void Date:setYear(
10、int y)year=y;void Date:printDate()coutyear/month/dayendl;3.下面是一个计算器类的定义,请完成该类的成员函数的实现,并设计一个主函数使用该类和相关成员函数以测试该类设计的正确性。#include using namespace std;class counterpublic:counter(int number=0)/构造函数value=number; void increment();/给value原值加1void decrement();/给value原值减1int getvalue();/取得计数器值int print();/显示计
11、数private:int value;/数据成员;void counter:increment()value+;coutThe counter has increased by 1endl;/(其中的cout语句可以不写)void counter:decrement()value-;coutThe counter has decreased by 1endl;int counter:getvalue()return value;int counter:print()coutnDisplay:getvalue()endlendl;return 0;int main()couta;counter
12、c(a); /输入a 并将a作为参数 通过构造函数为对象中的value赋值c.increment();c.print();/题目要求验证 所以使用输出函数一次验证value的值c.decrement();c.print();return 0;4.定义一个类ClsName,要求该类设计如下:(1)该类有两个整型数据成员x和y;(2)为该类重载三个不同的构造函数:分别为无参数、带一个参数和带两个参数的构造函数,要求在构造函数中输出必要的信息以示区别;(3)设计2个成员函数用来读取数据成员x和y;(4)设计2个成员函数用来设置数据成员x和y;(5)设计1个成员函数用来在屏幕上打印输出数据成员x和y;
13、(6)在main()函数中用三个不同的构造函数创建3个对象,并使用所有的成员函数对这些对象进行必要的操作。class ClsNamepublic:int a,b;ClsName(int a,int b)coutConstruct by two numbers.endl;x=a,y=b;/以下三个(2)中要求的重载构造函数ClsName(int a)coutConstruct by 1 number.endl;x=a,y=b;/题目要求加以区别,所以cout输出不同内容加以区分ClsName(void)coutConstruct without a number.endl;x=a,y=b;int
14、 getx()return x;因为x,y的访问属性为私有,不能由主函数直接访问 所以会设置读取函数专门负责返回相应的值重载:函数名相同,参数的类型不同或者个数不同、或者都不同。 即根据带入参数的类型、数量的不同,来实现不同的功能int gety()return y;void setx()coutx;void sety()couty;void print()coutX=getx()tY=gety()endl;private:int x,y;(1)中要求的两个整型数据成员;int main()int a,b;coutTest1:Input with 2 numbers, now please i
15、nput two numbers:a;cinb;ClsName t1(a,b);t1.print();t1.setx();t1.sety();t1.print();couta;ClsName t2(a);t2.print();t2.setx();t2.sety();t2.print();coutnTest3:Without numbers;ClsName t3;t3.print();t3.setx();t3.sety();t3.print();return 0;5.建立类cylinder,包括两个数据成员radius和height,分别表示圆柱体的半径和高度, cylinder类的构造函数被传
16、递了两个double值来初始化这两个成员;定义成员函数area()和volume用来求圆柱体的表面积和体积,定义成员函数print()打印cylinder类的相关信息。最后,在主函数中对cylinder类的设计进行测试。这题很简单 没啥说的#includeclass cylinderpublic:void set(double r,double h)radius=r,height=h;double area()return (radius*3.14);double volume()return (height*area();void print()coutArea=area()tVolume=
17、volume()endl;private:double radius,height;int main()int a,b;cylinder v;couta;coutb;v.set(a,b);v.print();return 0;6.定义一个学生类CStudent实现简单的学籍管理功能,要求该类至少实现以下功能:(1) 录入学生的基本信息(包括姓名,学号,性别,年龄,专业,入学时间,各门功课成绩)(2) 输出学生的基本信息(3) 选课(输入时间和所选的课程)(4) 统计每门课的学分绩点(5) 统计每学期的平均学分绩点(6) 统计重考科目(7) 统计重修科目#include#includeusing
18、 namespace std;const MAX=4;class CStudentstring name,sex,ID,object,jtime;/分别存储姓名 性别 学号 专业 入学时间int age;/存储年龄int markMAX;/设立一个数组存放成绩string sbjMAX;/设立string数组来存放科目名称int retry,redo;/统计重考重修数量string t_chose,s_chose;/存放选课时间和选课名称int m_pointMAX,m_markMAX;/存放每门功课的学分和绩点int markerMAX;/存放重修科目的角标(有时不止挂掉一科 可能会很多 所
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 天津 理工大学 C+ 期末考试

限制150内