Thread: Classes with Other Classes as Member Data

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Classes with Other Classes as Member Data

    Hi,

    I am learning C++ at the moment and I am struggling with the following:

    Classes with Other Classes as Member Data.

    I am wondering if anyone could show me a quick (commented) example of how this works.


    This is from the book I am learning from BTW:

    It is not uncommon to build up a complex class by declaring simpler classes and including them in the declaration of the more complicated class. For example, you might declare a wheel class, a motor class, a transmission class, and so forth, and then combine them into a car class. This declares a has-a relationship. A car has a motor, it has wheels, and it has a transmission.

    I basically could do with a simpler example so I can get my head around it easier than the example given in the book.


    Thanks in advance.
    Last edited by njd; 09-27-2005 at 09:10 AM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    This is a pretty straight-forwards example of thinking in object-oriented programming.

    Note: This really just acts like a bunch of structs since none of the objects have any behavior. I also didn't bother with encapsulation; however, it's an integral part which I'm sure you'll learn later.
    Code:
    class Monitor
    {
    public:
      int size;
      int resWidth;
      int resHeight;
    };
    
    class Keyboard
    {
    public:
      int keys;
    };
    
    class Mouse
    {
    public:
      int buttons;
    };
    
    class CPU
    {
    public:
      double ghzSpeed;
    };
    
    
    // A computer consists of a monitor, keyboard, mouse and cpu.
    class DesktopComputer
    {
    public:
      Monitor monitor;
      Keyboard keyboard;
      Mouse mouse;
      CPU cpu;
    };
    
    int main()
    {
      DesktopComputer computer;
    
      // Fill our computer class with information.
      computer.monitor.size = 19;
      computer.monitor.resWidth = 1600;
      computer.monitor.resHeight = 1200;
      computer.keyboard.keys = 109;
      computer.mouse.buttons = 3;
      computer.cpu.ghzSpeed = 3.4;
    
      return 0;
    }
    Last edited by Speedy5; 09-27-2005 at 09:32 AM.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    Thanks for that! I just took a look over that and now it all makes sense!!

    Once again, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class with pointers to other classes as member data.
    By Sclorch in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2009, 05:59 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM
  5. Initilize data member
    By xstone in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2001, 05:38 PM