没有合适的资源?快使用搜索试试~ 我知道了~
1. Description of the Problem 2. 结果示例 1. Description of the Problem 2. 结果示例 1. D
资源详情
资源评论
资源推荐
程序设计基础及语言(II)
实验指导手册
东南大学
计算机科学与工程学院
软件学院
2012 年 2 月
目 录
Lab1 Classes:A Deeper Look, PartI ...................................................................................1
Lab2 Classes:A DeeperLook,Part 2 ..........................................................................................4
Lab3 Operator Overloading; String and Array Objects...........................................................15
Lab4 Object-Oriented Programming: Inheritance...................................................................21
Lab5 Object-Oriented Programming:Polymorphism..............................................................27
Lab6 Templates .....................................................................................................................30
Lab7 Exception Handling .......................................................................................................34
Lab8 File Processing...............................................................................................................37
实验及习题指导手册
实验作业及课后习题
Lab1 Classes:A Deeper Look, PartI
OBJECTIVES :
1.掌握类的定义和操作(包括常成员函数)
2.掌握对常量对象的访问
3.掌握使用友元访问对象的私有数据成员
4.掌握静态数据成员和成员函数
Experiments
作业一(习题 9.5,复数类)
1. DescriptionoftheProblem
(Complex Class) Create a class called Complex for performing arithmetic with complex numbers.
Write a program to test your class.
Complex numbers have the form
realPart + imaginaryPart * i
where i is
1−
Use double variables to represent the private data of the class. Provide a constructor that enables
an object of this class to be initialized when it is declared. The constructor should contain default
values in case no initializers are provided. Provide public member functions that perform the
following tasks:
(a) Adding two Complex numbers: The real parts (实部) are added together and the imaginary
parts (虚部) are added together.
(b) Subtracting two Complex numbers: The real part of the right operand is subtracted from the
real part of the left operand, and the imaginary part of the right operand is subtracted from the
imaginary part of the left operand.
Printing Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.
2. 结果示例
作业二(习题 9.7,增强的 Time 类)
1.
DescriptionoftheProblem
(Enhancing Class Time) Modify the Time class of Figs. 9.89.9 to include a tick member function
that increments the time stored in a Time object by one second. The Time object should always
remain in a consistent state. Write a program that tests the tick member function in a loop that
1
实验及习题指导手册
prints the time in standard format during each iteration of the loop to illustrate that the tick
member function works correctly. Be sure to test the following cases:
z Incrementing into the next minute.
z Incrementing into the next hour.
z Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
注意以下额外要求:
Then change the tick member function to a friend function of class Time, which will access the
private data member of Time directly. You should get the same output as above.
friend void tick(Time &t); // increment one second
2
. 结果示例
作业三(习题 9.14,大整数类)
1.
DescriptionoftheProblem
(HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits
to store integers as large as 40 digits each. Provide member functions:
(a) Constructor, destructor
(b) input, output, add and substract
(c) For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo,
isGreaterThan, isLessThan, isGreaterThanOrEqualTo and
isLessThanOrEqualToeach of these is a "predicate" function that simply returns
TRue if the relationship holds between the two HugeIntegers and returns false if
the relationship does not hold. Also, provide a predicate function isZero.
If you feel ambitious, provide member functions multiply, divide and modulus.
注:不考虑负数情况,即 hugeintA-hugeintB 确保 hugeintA 大于 hugeintB;而
hugeintA+hugeintB,确保不溢出
2. HugeInteger 类定义
#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H
2
实验及习题指导手册
class HugeInteger
{
public:
HugeInteger( int = 0 ); // conversion/default constructor
HugeInteger( const char * ); // conversion constructor
// addition operator; HugeInteger + HugeInteger
HugeInteger add( const HugeInteger & );
// addition operator; HugeInteger + int
HugeInteger add( int );
// addition operator;
// HugeInteger + string that represents large integer value
HugeInteger add( const char * );
// subtraction operator; HugeInteger - HugeInteger
HugeInteger subtract( const HugeInteger & );
// subtraction operator; HugeInteger - int
HugeInteger subtract( int );
// subtraction operator;
// HugeInteger - string that represents large integer value
HugeInteger subtract( const char * );
bool isEqualTo( HugeInteger & ); // is equal to
bool isNotEqualTo( HugeInteger & ); // not equal to
bool isGreaterThan(HugeInteger & ); // greater than
bool isLessThan( HugeInteger & ); // less than
bool isGreaterThanOrEqualTo( HugeInteger & ); // greater than
// or equal to
bool isLessThanOrEqualTo( HugeInteger & ); // less than or equal
bool isZero(); // is zero
void input( const char * ); // input
void output(); // output
private:
int integer[ 40 ]; // 40 element array
}; // end class HugeInteger
#endif
3
剩余40页未读,继续阅读
RandyRhoads
- 粉丝: 26
- 资源: 296
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0