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
|
<title>CLASS keyword</title>
<!-- Changed by: Martin Leslie, 14-Mar-1996 -->
<body bgcolor=#dddddd>
<font color=brown>
<hr>
<center><h1>class keyword</h1></center>
<hr>
<p>
The concept of a class
allows you to
<a href=../../C/glossary.html#encapsulation>encapsulate</a>
a set of related variables and only
allow access to them via predetermined functions.
<p>
The <b>class</b> keyword is used to declare the group of variables
and functions and the visability of the variables and functions.
Here is a basic example.
<p>
<center>
<table width="80%" border="1" bgcolor="ivory">
<tr>
<td>
<pre>
class String
{
public:
void Set(char *InputStr) // Declare an Access function
{
strcpy(Str, InputStr);
}
private:
char Str[80]; // Declare a hidden variable.
};
</pre>
</td>
</tr>
</table>
</center>
<p>
<ul>
<li><b>private:</b> means that all variables and functions that
follow are only visable from within the class.
<li><b>public:</b> all functions and variables that follow this
statement can be accessed from outside the class.
</ul>
Now we have declared a class called <b>String</b> we need to
use it.
<p>
<center>
<table width="80%" border="1" bgcolor="ivory">
<tr>
<td>
<pre>
main()
{
String Title;
Title.Set("My First Masterpiece.");
}
</pre>
</td>
</tr>
</table>
</center>
<p>
This code creates a String
<a href=../../C/glossary.html#class_object>object</a>
called Title and then uses the <b>Set</b> member function
to initialise the value of <b>Str</b>.
<p>
In C++ the member function <b>Set</b> is also known as a
<a href=../../C/glossary.html#method>method</a>
<p>
This is the strength
of object oriented programming, the ONLY way to change
<b>Str</b> is via the <b>Set</b> method.
<p>
The final code example brings the last two examples together
ands a new method (called Get) that shows the contents of
<b>Str</b>
<p>
<center>
<table width="80%" border="1" bgcolor="ivory">
<tr>
<td>
<pre>
#include <stdlib.h>
#include <iostream.h> // Instead of stdio.h
class String
{
public:
void Set(char *InputStr) // Declare an Access function
{
strcpy(Str, InputStr);
}
<b>
char *Get(void) // Declare an Access function
{
return(Str);
}
</b>
private:
char Str[80]; // Declare a hidden variable.
};
main()
{
String Title;
Title.Set("My First Masterpiece.");
<b>cout << Title.Get() << endl;</b>
}
</pre>
</td>
</tr>
</table>
</center>
<p>
<hr>
<h2>Examples:</h2>
<img src="../../GRAPHICS/computer.gif">
<a href="../EXAMPLES/class1.cc">Example program.</a>
<hr>
<h2>See Also:</h2>
<img src="../../GRAPHICS/whiteball.gif">
<a href="../CONCEPT/constructor.html">Constructors and destructors.</a>
<p>
<img src="../../GRAPHICS/whiteball.gif">
<a href="../CONCEPT/inheritance.html">Class Inheritance.</a>
<hr>
</font>
<font color=black>
<h2>C References</h2>
<p>
<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="keywords.html">C++ 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
08-Feb-96</address><p>
</font>
|