📄 10exerc.html
字号:
<html>
<head>
<title>Exercises</title>
<link rel="stylesheet" href="../../rs.css">
</head>
<body background="../../images/margin.gif" bgcolor="#FFFFDC">
<!-- Main Table -->
<table cellpadding="6">
<tr>
<td width="78"> </td>
<td>
<h3>Exercises</h3>
<p>Notice: If you have background in C and I haven't scared you off yet, these exercises will seem trivial to you. Please do them anyway. Get used to the new way of thinking and structuring your problems. All the exercises should be solved using only the constructs introduced so far. You may compare your solutions with my <a href="solutions1.html">solutions</a>.
<ol>
<li>Write a program using class <var>World</var>, whose output is "Hello from 1! Good bye from 1. Hello from 2! Good bye from 2." Find at least two different solutions.
<li>Find a programmer's error in this class definition. Give two different ways of correcting it.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>class <span class=method>Glove</span>
{
public:
Glove (int cFingers)
: _n (cFingers), _hand (_n)
{
cout << "Glove with " << _n << " fingers\n";
}
private:
Hand _hand;
int _n;
};</pre>
</table>
<!-- End Code -->
<li>Define class <var>HorBar</var> whose constructor prints the following pattern
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>+----------+</pre>
</table>
<!-- End Code -->
where the number of minus signs is passed as an <var>int</var> argument to the constructor. Similarly class <var>VerBar</var> should print a vertical bar of height <var>n</var>, like this:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>|
|
|
|</pre>
</table>
<!-- End Code -->
<p>
Define class <var>Frame</var> that contains (embedded) the upper horizontal bar, the vertical bar and the lower horizontal bar. Its constructor should take two arguments, the horizontal and the vertical extent of the frame
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>Frame (int hor, int ver)</pre>
</table>
<!-- End Code -->
The <var>Frame</var> object can be defined and initialized in the program using the following syntax:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>Frame myFrame (10, 2);</pre>
</table>
<!-- End Code -->
<li>Define class <var>Ladder</var> that contains two frames and a <var>VerBar</var> between them. The constructor should take two arguments, one for the horizontal extent and one for the vertical distance between horizontal bars. It should print the following pattern when created with (5, 2):
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>+-----+
|
|
+-----+
|
|
+-----+
|
|
+-----+</pre>
</table>
<!-- End Code -->
<li>Write a program that asks for a number and calculates its factorial. Factorial of <i>n</i> is the product of all positive integers up (and including <i>n</i>. <i>n</i>! = 1 * 2 * 3 * ... * <i>n</i> . To multiply two numbers put an asterisk, *, between them.
<li>Define class <var>Planet</var> that inherits from <var>CelestialBody</var> and is characterized by an albedo (how reflective its surface is--use a double to store it).
<li>In the following class definition, replace dummy strings with actual words in such a way that during the construction of the object, the string "program makes objects with class" is printed
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>class <span class=method>One</span>: public <span class=method>Two</span>
{
public:
One ()
{
Three three;
cout << "dummy ";
}
private:
Four _four;
};
</pre>
</table>
<!-- End Code -->
<p>
Each of the classes <var>Two</var>, <var>Three</var>, and <var>Four</var> prints some dummy string in its constructor.
<p>
Design a similar class which, during construction, would print the above string, and during destruction print the same words in reverse order. The constraint is that each class has to have exactly the same code (print the same string) in its constructor and in its destructor.
<li>Design, implement and test class <var>Average</var>. An object of type <var>Average</var> should accept values that are put into it. At any point in time it should be possible to retrieve their cumulative arithmetic average.
<li>Design a class hierarchy (chain of inheritance) that derives a <var>Human</var> from a <var>LivingBeing</var>. Make sure you use the is-a relationship. Now add branches for a <var>Tomato</var> and an <var>Elephant</var>. Write a little test program that defines one of each inside main. Each class should have a constructor that prints a short but pointed message.
<li>Design a <var>Human</var> exploring the has-a relationship. A human has a head, the head has a nose, the nose has a nose hair, etc. Derive a <var>Man</var> and a <var>Woman</var>. Implement simple classes whose constructors print messages and create a couple of <var>Human</var>s.
</ol>
</table>
<!-- End Main Table -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -