summaryrefslogtreecommitdiff
path: root/reference/CPLUSPLUS/EXAMPLES/inherit.cc
blob: 47365f9cb265e5ea207706e94fc7923ba77b8b37 (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
/************************************************************************
 *
 * Purpose:
 * Author:  M J Leslie
 * Date:    26-Oct-98
 *
 ************************************************************************/


#include <stdlib.h>
#include <iostream.h>               // Instead of stdio.h


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


class Tent : public Fabric
{
public:

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

    SetNumOfPoles(int P)
    {
        Poles = P;
    }

private:
    int  Poles;
};


// ... The derived class 'Clothes' also
// ... names 'Fabric' as a base class.


class Clothes : public Fabric
{
    public:

    Clothes() {};
    ~Clothes() {};
    
    void SetNumOfButtons(int B)
    {
    Buttons = B;
    };
    
    int GetNumOfButtons(void)
    {
    return (Buttons);
    };
    
    private:
    int  Buttons;
};


// ...    Function definitions.

void Init(Fabric &Material);

main()
{  
    Tent       Frame;
    Clothes    Jacket;

    // ... Initialise using the derived methods.
    
    Init(Frame);
    Init(Jacket);

    // .. Initialise using the unique methods.
    
    Frame.SetNumOfPoles(5);
    Jacket.SetNumOfButtons(2);

}

void Init(Fabric &Material)
{
    Material.SetColour("Red");
    Material.SetSize  (10, 20);
}