From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/CPLUSPLUS/CONCEPT/inheritance.html | 264 +++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 reference/CPLUSPLUS/CONCEPT/inheritance.html (limited to 'reference/CPLUSPLUS/CONCEPT/inheritance.html') 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 @@ + + +Inheritance + + + +
+

Inheritance.

+
+

+Inheritance enables classes to use the properties of other classes and +add to those properties. +

+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. +

+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, fabric! If we described +tents and clothes in C++, we can seperate fabric into a seperate class +that the Tent and Clothes classes can inherit. +

+

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

+Now for some buzz words: +

    +
  1. Fabric is a base class +
  2. Tent and Clothes are derived classes +
+This is what the code looks like: +

+

+ + + + + +
+
+
+// ... The base class 'Fabric'
+// ... is no different to normal.
+
+
+
+
+  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];
+  };
+
+
+
+
+

+ +

+ + + + + + + + +
+
+
+// ... The derived class 'Tent'
+// ... names 'Fabric' as a base class.
+
+
+
+
+// ... The derived class 'Clothes' also
+// ... names 'Fabric' as a base class.
+
+
+
+
+  class Tent : public Fabric
+  {
+  public:
+
+       Tent() {};
+      ~Tent() {};
+
+      SetNumOfPoles(int P)
+      {
+          Poles = P;
+      };
+
+  private:
+      int  Poles;
+  };
+        
+
+
+
+
+  class Clothes : public Fabric
+  {
+  public:
+
+      Clothes() {};
+      ~Clothes() {};
+
+      SetNumOfButtons(int B)
+      {
+          Buttons = B;
+      };
+
+  private:
+      int  Buttons;
+  };
+        
+
+
+
+

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

+ + +

Using the classes

+ +So we have three classes, they are used in the same way as before but +with one extra feature. +

+Consider this piece of code. + + +

+ + + + +
+
+  void Init(Fabric &Material);
+
+  main()
+  {
+      Tent       Frame;
+      Clothes    Jacket;
+
+      Init(Frame);
+      Init(Jacket);
+  }
+
+  void Init(Fabric &Material)
+  {
+      Material.SetColour("Red");
+      Material.SetSize  (10, 20);
+  }
+     
+
+
+
+

+ +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. + +

+


+

Examples:

+o +The final program. +

+ +


+

See Also:

+ + +
+

+

+ + + + +
+Top + +Master Index + +Keywords + +Functions +
+
+

+


+ +
Martin Leslie +29-Dec-97

+ + + -- cgit v1.2.3-54-g00ecf