Classes: A Simple Example
// Note: C++ doesn’t require the ‘struct’
// and ‘class’ keywords in declarations.
counter *pointer_to_counter = NULL;
counter array_of_counters[10];
// Access a class member just like
// a structure member in C.
// And call functions too!
apples.increment_counter();
apples.increment_counter();
oranges.decrement_counter();
printf(“%d apples and %d oranges!\n”,
apples.query_counter(), // 12
oranges.query_counter()); // 4
// Note this C++ comment. In C++,
// everything from the ‘//’ to the end
// of the line is a comment.
public: // ignore this for now
char some_other_data[100];
// Complete (defined) functions.
void increment_counter() { i++; }
void decrement_counter() { i--; }
int query_counter() { return i; }
// Note, “void” no longer needed for
// functions that take no arguments.
// Just a function prototype; body
// must be defined elsewhere
void set_counter(int new_value);
// Prototype must match. Note “::”!!
void counter::set_counter(int new_value)