3.3 CLASSES AND OVERLOADING
Classes are the core concept behind object oriented programing. They basically allow data and functions to be grouped together into a complex data type. The example code below shows a class definition, and a program that uses it.
Figure 3.11 - A Simple Class Definition
The class is defined to have a public integer called'value' and a public function called 'result'. The function 'result' is defined separately outside of the class definition. In the 'main' program the class has two instances 'A' and 'B'. The 'value' values in the classes are set, and then the result function is then called.
A more sophisticated example of a class definition follows. The program shown does exactly the same as the last program, but with some useful differences. This class now includes a constructor function 'bill'. This function is automatically called when a new instance of 'bill' is created. In the main program the instances are not created initially, but pointers '*A' and '*B' are created. These are then assigned instances with the calls to 'new bill()'. At this point the constructor functions are called. Finally, when the instances are used, because they are pointers, the '->' are used instead of '.'.