summaryrefslogtreecommitdiff
path: root/reference/CPLUSPLUS/CONCEPT/inheritance.html
diff options
context:
space:
mode:
Diffstat (limited to 'reference/CPLUSPLUS/CONCEPT/inheritance.html')
-rw-r--r--reference/CPLUSPLUS/CONCEPT/inheritance.html264
1 files changed, 264 insertions, 0 deletions
diff --git a/reference/CPLUSPLUS/CONCEPT/inheritance.html b/reference/CPLUSPLUS/CONCEPT/inheritance.html
new file mode 100644
index 0000000..0c8822c
--- /dev/null
+++ b/reference/CPLUSPLUS/CONCEPT/inheritance.html
@@ -0,0 +1,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>