summaryrefslogtreecommitdiff
path: root/reference/CPLUSPLUS/CONCEPT/inheritance.html
blob: 0c8822ce22dbb42decfd42612e87d0cadf954729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<html>
<head>
<title>Inheritance</title>
</head>
<body bgcolor="#dddddd">
<font color=brown>
<hr>
<center><h1>Inheritance.</h1></center>
<hr>
<p>
Inheritance enables classes to use the properties of other classes and
add to those properties.
<p>
The thing to remember about object programming is that it attempts to
create a software model as close to the real world as possible. Inheritance
forms an important role to meet this need. Take the following example.
<p>
A fabric can have a colour and size and it is also used to make clothes
as well as tents. Now looking at this from the other angle tents are made
from fabric and poles but clothes are made from fabric and buttons.
So, Tents and clothes have something in common, <blink>fabric!</blink> If we described
tents and clothes in C++, we can seperate fabric into a seperate class
that the Tent and Clothes classes can inherit.
<p>
<center>
<table border="1" bgcolor=ivory width=80%>
<tr align=center>
<td>
<pre>

       -----------         
      |  Fabric   |        
       -----------         
          A    A           
          |    |           
       ---      ---        
      |            |       
 ---------     ----------- 
|  Tent   |   |  Clothes  |
 ---------     ----------- 


</pre>
</tr>
</td>
</table>
</center>
<p>
Now for some buzz words:
<ol>
<li>Fabric is a <b>base class</b>
<li>Tent and Clothes are <b>derived classes</b>
</ol>
This is what the code looks like:
<p>
<center>
<table border="1" bgcolor=ivory width=50%>
<tr>
<td>
<pre>

// ... The <b>base</b> class 'Fabric'
// ... is no different to normal.
</pre>
</td></tr>
<tr><td>
<pre>

  class Fabric
  {
  public:

       Fabric() {};
      ~Fabric() {};

      SetSize(int x, int y)
      {
          Length = x;
          Width  = y;
      };

      SetColour(char *C)
      {
         strcpy(Colour, C); 
      }

  private:
      int  Length;
      int  Width;
      char Colour[20];
  };

</pre>
</tr>
</td>
</table>
</center>
<p>

<center>
<table bgcolor=ivory width=80% border="1">
<tr>
<td>
<pre>

// ... The <b>derived</b> class 'Tent'
// ... names 'Fabric' as a base class.
</pre>
</td>
<td>
<pre>

// ... The <b>derived</b> class 'Clothes' also
// ... names 'Fabric' as a base class.
</pre>
</td>
</tr>
<tr><td>
<pre>

  class Tent <font color=red>: public Fabric</font>
  {
  public:

       Tent() {};
      ~Tent() {};

      SetNumOfPoles(int P)
      {
          Poles = P;
      };

  private:
      int  Poles;
  };
        
</pre>
</td>
<td>
<pre>

  class Clothes <font color=red>: public Fabric</font>
  {
  public:

      Clothes() {};
      ~Clothes() {};

      SetNumOfButtons(int B)
      {
          Buttons = B;
      };

  private:
      int  Buttons;
  };
        
</pre>
</tr>
</td>
</table>
</center>
<p>

What we now have are three classes with the following public methods.

<ul>
<li>Fabric
<ol>
<li>SetSize
<li>SetColour
</ol>
<li>Tents
<ol>
<li>SetSize
<li>SetColour
<li>SetNumOfPoles
</ol>
<li>Clothes
<ol>
<li>SetSize
<li>SetColour
<li>SetNumOfButtons
</ol>
</ul>


<h2>Using the classes</h2>

So we have three classes, they are used in the same way as before but
with one extra feature.
<p>
Consider this piece of code.


<center>
<table border="1" bgcolor=ivory width=50%>
<tr>
<td>
<pre>
  void Init(Fabric &Material);

  main()
  {
      Tent       Frame;
      Clothes    Jacket;

      Init(Frame);
      Init(Jacket);
  }

  void Init(Fabric &Material)
  {
      Material.SetColour("Red");
      Material.SetSize  (10, 20);
  }
     
</pre>
</tr>
</td>
</table>
</center>
<p>

The function 'Init' expects an object of type 'Fabric' but we
are passing objects of type 'Tents' and 'Clothes', this is OK as
'Tents' and 'Clothes' are derived from Fabric.

<p>
<hr>
<h2>Examples:</h2>
<img src="../../GRAPHICS/computer.gif" alt="o">
<a href="../EXAMPLES/inherit.cc">The final program.</a>
<p>

<hr>
<h2>See Also:</h2>


<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">Top</a>
</td><td width="25%">
<a href="../../C/master_index.html">Master Index</a>
</td><td width="25%">
<a href="../SYNTAX/keywords.html">Keywords</a>
</td><td width="25%">
<a href="../../C/FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<font color=brown>
<address>Martin Leslie 
29-Dec-97</address><p>
</font>
</body>
</html>