For the purpose of demonstrating a few more OOP-related concepts, we'll use another class. Our new class is named
Table, and it has two public fields (
columns), and one method,
getCellCount() method should return the number of rows multiplied by the number of columns. The class constructor should receive two parameters, used to initialize the
columns fields. This class could be represented by the class diagram in Figure 3-3.
The C# version of this class would look like this:
You'd instantiate and use the class like this:
[ In a production-quality C# implementation you may want to implement
columns as properties with
After having declared the object, we can instantiate it by using the newoperator and use its properties and methods:
Referencing External Functions
Instead of defining member functions ("methods") inside the main function ("class") as shown earlier, you can make references to functions defined outside your main function, like this:
Now, all your
Table objects will share the same instance of
getCellCount(), which is what you will usually want.