📄 solutions1.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>Exercise Solutions</h3>
<ol>
<li> The first solution is to create the two worlds in a loop:
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>for (int i = 1; i < 3; ++i)
World world (i);
</pre>
</td></tr></table><!-- End Code -->
The second possibility is to open a separate local scope for each world:
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>int main ()
{
{
World world (1);
}
{
World world (2);
}
}</pre>
</td></tr></table><!-- End Code -->
<li> The problem is in the order of initialization of data members. Despite the ordering of initializers in the preamble to the constructor, _hand will be initialized before _n because that抯 the order in which they are embedded in Glove. Therefore _n shouldn抰 be passed as an argument to the Hand抯 constructor because its value hasn抰 been set at that point. One way of fixing this bug is to change the order of embeddings. The other is to use numFingers instead of _n to initialize the _hand.
<li> Bars.
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>class HorBar
{
public:
HorBar (int n)
{
std::cout << "+";
for (int i = 0; i < n; ++i)
std::cout << "-";
std::cout << "+\n";
}
};
class VerBar
{
public:
VerBar (int n)
{
for (int i = 0; i < n; ++i)
std::cout << "|\n";
}
};
class Frame
{
public:
Frame (int hor, int ver)
: _upper (hor),
_ver (ver),
_lower (hor)
{}
private:
HorBar _upper;
VerBar _ver;
HorBar _lower;
};</pre>
</td></tr></table><!-- End Code -->
<li> Ladder
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>class Ladder
{
public:
Ladder (int hor, int ver)
: _upper (hor, ver),
_middle (ver),
_lower (hor, ver)
{}
private:
Frame _upper;
VerBar _middle;
Frame _lower;
};
</pre>
</td></tr></table><!-- End Code -->
<li> This particular solution that uses only the constructs introduced in chapter 1.
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>int main ()
{
InputNum num;
int factorial = 1;
for (int i = 2; i < num.GetValue (); ++i)
factorial = factorial * i;
factorial = factorial * num.GetValue ();
std::cout << num.GetValue () << "! = "
<< factorial << std::endl;
}</pre>
</td></tr></table><!-- End Code -->
<li>Planet
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>class Planet: public CelestialBody
{
public:
Planet (double mass, double albedo)
: CelestialBody (mass),
_albedo (albedo)
{
std::cout << "Creating a planet with albedo "
<< _albedo << std::endl;
}
~Planet ()
{
std::cout << "Destroying a planet with albedo "
<< _albedo << std::endl;
}
private:
double _albedo;
};
</pre>
</td></tr></table><!-- End Code -->
<li> The first part:
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>class Two
{
public:
Two ()
{
std::cout << "Program ";
}
};
class Three
{
public:
Three ()
{
std::cout << "objects ";
}
};
class Four
{
public:
Four ()
{
std::cout << "makes ";
}
};
class One: public Two
{
public:
One ()
{
Three three;
std::cout << "with class. ";
}
private:
Four _four;
};
</pre>
</td></tr></table><!-- End Code -->
The second part can be implemented like this:
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>class One: public Two
{
public:
One ()
{
std::cout << "class ";
}
~One ()
{
std::cout << "class ";
}
private:
Three _three;
Four _four;
Five _five;
};
int main ()
{
One one;
std::cout << std::endl;
}
</pre>
</td></tr></table><!-- End Code -->
where the classes Two, Three, Four and Five print 揚rogram
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -