[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: Pointers Up: Aggregation Previous: arrays

structures and classes

Variables of different types can be grouped into a structure or a class.

class person {
public:
     int age;
     int height;
     string surname;
};

person fred, jane;

defines 2 objects of type person each of 3 fields. Fields are accessed using the `.' operator. For example, fred.age is an integer which can be used in assignments just as a simple variable can.

Structure and Class objects may be assigned, passed to functions and returned, but they cannot (by default) be compared, so continuing from the above fragment fred = jane; is possible (the fields of jane being copied into fred) but you can't then go on to do

if (fred == jane)
  cout << "The copying worked ok\n";

- you have to compare field by field.

Classes can contain member functions as well as data. The data can be made private so that other objects can't access it directly.



Tim Love
2001-07-05