Skip to content

Commit d6f4fee

Browse files
authored
Create README.md
1 parent 652c752 commit d6f4fee

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

chapter13/README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
## Chapter 13 "Class Inheritance"
2+
### Programming Exercises
3+
4+
1. Start with the following class declaration:
5+
```cpp
6+
// base class
7+
class Cd { // represents a CD disk
8+
private:
9+
char performers[50];
10+
char label[20];
11+
int selections; // number of selections
12+
double playtime; // playing time in minutes
13+
public:
14+
Cd(char * s1, char * s2, int n, double x);
15+
Cd(const Cd & d);
16+
Cd();
17+
~Cd();
18+
void Report() const; // reports all CD data
19+
Cd & operator=(const Cd & d);
20+
};
21+
```
22+
23+
Derive a `Classic class` that adds an array of `char` members that will hold a string
24+
identifying the primary work on the CD. If the base class requires that any functions be virtual,
25+
modify the base-class declaration to make it so. If a declared
26+
method is not needed, remove it from the definition. Test your product with the
27+
following program:
28+
```cpp
29+
#include <iostream>
30+
using namespace std;
31+
#include "classic.h" // which will contain #include cd.h
32+
void Bravo(const Cd & disk);
33+
int main()
34+
{
35+
Cd c1("Beatles", "Capitol", 14, 35.5);
36+
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C",
37+
"Alfred Brendel", "Philips", 2, 57.17);
38+
39+
Cd *pcd = &c1;
40+
41+
cout << "Using object directly:\n";
42+
c1.Report(); // use Cd method
43+
c2.Report(); // use Classic method
44+
45+
cout << "Using type cd * pointer to objects:\n";
46+
pcd->Report(); // use Cd method for cd object
47+
pcd = &c2;
48+
pcd->Report(); // use Classic method for classic object
49+
50+
cout << "Calling a function with a Cd reference argument:\n";
51+
Bravo(c1);
52+
Bravo(c2);
53+
54+
cout << "Testing assignment: ";
55+
Classic copy;
56+
copy = c2;
57+
copy.Report()
58+
59+
return 0;
60+
}
61+
62+
void Bravo(const Cd & disk)
63+
{
64+
disk.Report();
65+
}
66+
```
67+
68+
2. Do Programming Exercise 1 but use dynamic memory allocation instead of fixedsize arrays
69+
for the various strings tracked by the two classes.
70+
71+
3. Revise the` baseDMA-lacksDMA-hasDMA` class hierarchy so that all three classes are
72+
derived from an ABC. Test the result with a program similar to the one in *Listing
73+
13.10*. That is, it should feature an array of pointers to the ABC and allow the user
74+
to make runtime decisions as to what types of objects are created. Add virtual
75+
`View()` methods to the class definitions to handle displaying the data.
76+
77+
4. The Benevolent Order of Programmers maintains a collection of bottled port. To
78+
describe it, the BOP Portmaster has devised a `Port` class, as declared here:
79+
```cpp
80+
#include <iostream>
81+
using namespace std;
82+
class Port
83+
{
84+
private:
85+
char * brand;
86+
char style[20]; // i.e., tawny, ruby, vintage
87+
int bottles;
88+
public:
89+
Port(const char * br = "none", const char * st = "none", int b = 0);
90+
Port(const Port & p); // copy constructor
91+
virtual ~Port() { delete [] brand; }
92+
Port & operator=(const Port & p);
93+
Port & operator+=(int b); // adds b to bottles
94+
Port & operator-=(int b); // subtracts b from bottles, if available
95+
int BottleCount() const { return bottles; }
96+
virtual void Show() const;
97+
friend ostream & operator<<(ostream & os, const Port & p);
98+
};
99+
```
100+
101+
The `Show()` method presents information in the following format:
102+
```
103+
Brand: Gallo
104+
Kind: tawny
105+
Bottles: 20
106+
```
107+
108+
The `operator<<()` function presents information in the following format (with no
109+
newline character at the end):
110+
```
111+
Gallo, tawny, 20
112+
```
113+
114+
The Portmaster completed the method definitions for the `Port` class and then
115+
derived the `VintagePort` class as follows before being relieved of his position for
116+
accidentally routing a bottle of ’45 Cockburn to someone preparing an experimental barbecue sauce:
117+
118+
```cpp
119+
class VintagePort : public Port // style necessarily = "vintage"
120+
{
121+
private:
122+
char * nickname; // i.e., "The Noble" or "Old Velvet", etc.
123+
int year; // vintage year
124+
public:
125+
VintagePort();
126+
VintagePort(const char * br, int b, const char * nn, int y);
127+
VintagePort(const VintagePort & vp);
128+
~VintagePort() { delete [] nickname; }
129+
VintagePort & operator=(const VintagePort & vp);
130+
void Show() const;
131+
friend ostream & operator<<(ostream & os, const VintagePort & vp);
132+
};
133+
```
134+
135+
You get the job of completing the `VintagePort` work.
136+
137+
a. Your first task is to re-create the `Port` method definitions because the former
138+
Portmaster immolated his upon being relieved.
139+
140+
b. Your second task is to explain why certain methods are redefined and others
141+
are not.
142+
143+
c. Your third task is to explain why `operator=()` and `operator<<()` are not
144+
virtual.
145+
146+
d. Your fourth task is to provide definitions for the `VintagePort` methods

0 commit comments

Comments
 (0)