The definition and application of objects and classes
1.Experimental purpose
To describe objects and classes, and to use classes to model objects .
To use UML graphical notations to describe classes and objects .
To demonstrate defining classes and creating objects .
To create objects using constructors.
To access data fields and invoke functions using the object member access operator (.) .
To separate a class definition from a class implementation.
To prevent multiple inclusions of header files using the #ifndef inclusion guard directive .
To declare private data fields with appropriate get and set functions for data field encapsulation and make classes easy to maintain .
2.Experimental Task
Design a class named Time. The class contains:
Draw the UML diagram for the class. Implement the class. Write a test program that creates two Time objects, one using a no-arg constructor and the other using Time(555550), and display their hour, minute, and second.
(Hint: The first two constructors will extract hour, minute, and second from the elapse time. For example, if the elapse time is 555550 seconds, the hour is 10, the minute is 19, and the second is 10. For the no-arg constructor, the current time can be obtained using time(0), as shown in Listing 2.8, ShowCurrentTime.cpp.)
3. Experimental procedure
(1) Design: (UML diagram for the class.)
(2) Implementation: (Write Source Code here. Attention to the writing standardization of
program )
4.Experimental summary
Describe the main knowledge points.
对象和类的定义和应用
1.实验目的
描述对象和类,并使用类来建模对象。
使用UML图形符号来描述类和对象。
演示定义类和创建对象。
使用构造函数创建对象。
使用对象成员访问操作符(.)访问数据字段和调用函数。
将类定义与类实现分离。
使用#ifndef inclusion guard指令防止头文件的多个包含。
使用适当的get和set函数来声明私有数据字段,以便对数据字段进行封装,并使类易于维护。
2.实验任务
设计一个名为Time的类。该类包含:
•表示时间的小时、分钟和秒数据字段。
•为当前时间创建时间对象的无参数构造函数。
•从1970年1月1日午夜开始,以秒为单位构造具有指定流逝时间的时间对象的构造函数。
•用指定的时、分、秒构造时间对象的构造函数。
•数据字段的三个get函数:小时、分钟和秒。
•一个名为setTime(int elapseTime)的函数,它使用经过的时间为对象设置新时间。
为类绘制UML图。实现类。编写一个测试程序,创建两个时间对象,一个使用无参数构造函数,另一个使用时间(555550),并显示它们的时、分和秒。
(提示:前两个构造函数将从经过的时间中提取小时、分钟和秒。例如,如果经过时间是555550秒,则小时是10,分钟是19,秒是10。对于no arg构造函数,可以使用time(0)获得当前时间,如清单2.8所示,显示当前时间.cpp.)
三。实验程序
(1) 设计:(类的UML图)
(2) 实现:(在这里编写源代码。注意写作规范化
(程序)
4.实验总结
描述主要知识点。
对象和类的定义和应用
1.实验目的
描述对象和类,并使用类来建模对象。
使用UML图形符号来描述类和对象。
演示定义类和创建对象。
使用构造函数创建对象。
使用对象成员访问操作符(.)访问数据字段和调用函数。
将类定义与类实现分离。
使用#ifndef inclusion guard指令防止头文件的多个包含。
使用适当的get和set函数来声明私有数据字段,以便对数据字段进行封装,并使类易于维护。
2.实验任务
设计一个名为Time的类。该类包含:
•表示时间的小时、分钟和秒数据字段。
•为当前时间创建时间对象的无参数构造函数。
•从1970年1月1日午夜开始,以秒为单位构造具有指定流逝时间的时间对象的构造函数。
•用指定的时、分、秒构造时间对象的构造函数。
•数据字段的三个get函数:小时、分钟和秒。
•一个名为setTime(int elapseTime)的函数,它使用经过的时间为对象设置新时间。
为类绘制UML图。实现类。编写一个测试程序,创建两个时间对象,一个使用无参数构造函数,另一个使用时间(555550),并显示它们的时、分和秒。
(提示:前两个构造函数将从经过的时间中提取小时、分钟和秒。例如,如果经过时间是555550秒,则小时是10,分钟是19,秒是10。对于no arg构造函数,可以使用time(0)获得当前时间,如清单2.8所示,显示当前时间.cpp.)
三。实验程序
(1) 设计:(类的UML图)
(2) 实现:(在这里编写源代码。注意写作规范化
(程序)
4.实验总结
描述主要知识点。
不难,但是繁琐