www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C++ Tutorial - Lesson 27: Relationships Between Classes In C++

by John Kopp

Support this site at no cost to you

You have learned the language, studied its syntax, perhaps written small programs as assignments from classes, web tutorials or books and are now ready to tackle a bigger project. What is the first step? Before any programs are written, really before a single character is typed in a file, some design work must be done. For an object-oriented language, such as C++, design starts with choosing classes and defining their relationships.

The three main types of relationships between classes are generalization (inheritance), aggregation, and association.

  • Generalization - This implies an "is a" relationship. One class is derived from another, the base class. Generalization is implemented as inheritance in C++. The derived class has more specialization. It may either override the methods of the base, or add new methods. Examples are a poodle class derived from a dog class, or a paperback class derived from a book class.
  • Aggregation - This implies a "has a" relationship. One class is constructed from other classes, that is, it contains objects of any component classes. For example, a car class would contain objects such as tires, doors, engine, and seats.
  • Association - Two or more classes interact in some manner. They may extract information from each other, or update each other in some way. As an example, a car class may need to interact with a road class, or if you live near any metropolitan area, the car class may need to pay a toll collector class.

Assume that you've been asked by the local zoo to write a program that will be used to study animals in order to create better habitats. What classes might be needed? An animal class is a good starting point.

class Animal {
private:
     int itsAge;
     float itsWeight;
public:
     // Accessor methods have been left out as a simplification
     void move() {cout << "Animal Moving\n";}
     void speak() {cout << "Animal Speaking\n";}
     void eat() {cout << "Animal Eating\n";}
}

To better study a particular type of animal, it is necessary to generalize.

class Duck: public Animal {
private:
public:
     // Accessor methods have been left out as a simplification
     void move() {cout << "Waddle\n";}
     void speak() {cout << "Quack\n";}
}

The Duck class has inherited some methods and members from Animal, and has made some of its methods more specialized. Not everyone can waddle and quack.

An animal consists of certain parts: for instance, head, body, and skin. It is an aggregation of these parts. The animal class, likewise, can be an aggregation of many other classes.

class Animal {
private:
     int itsAge;
     float itsWeight;
     Head itsHead;
     Body itsBod;
     Heart itsHeart;
public:
     // Accessor methods have been left out as a simplification
     void move() {cout << "Animal Moving\n";}
     void speak() {cout << "Animal Speaking\n";}
     void eat() {cout << "Animal Eating\n";}
}

Association is a logical relationship. The classes need to know each others interfaces and need to interact but there is no formal "is a" or "has a" relationship as in generalization and aggregation, respectively. As an example, a zookeeper class would need associations with the animal classes. Interactions between these classes would be seen through out the zoo program.

Previous Page       Next Page