<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 27 Relationships Between Classes In C++_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 27: Relationships Between Classes In C++
Generalization, Aggregation and Association
 Related Resources
• Passing Arguments to Functions
• Pointer Tutorial
• Tip on Variable Names
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial - See all lessons
 
 From Other Guides
• Unix Find Command
• Object-Oriented JavaScript
• HTML Form Validation Using JavaScript
• Parsing Text In Files with Perl
 
 Elsewhere on the Web
• Object Oriented Computing
• Road Map For OOAD
 

by John Kopp

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 Articles



Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.