Thread: problem with the has-a relationship (aggregation)

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    problem with the has-a relationship (aggregation)

    im having trouble with this project i need to do

    i was supplied with a bunch of code and told to finish it, i need to implement the has-a relationship between 4 different cpp files, but ill just paste 2 here cause once i figure 1 out i can do the rest

    heres the picture of a UML image if it helps any:
    http://webhost.bridgew.edu/jsantore/...2/Lab2-uml.jpg

    im thinking its suppose to be something like this:

    _something = new LeftBar();

    but its not working

    heres Flag.cpp, the file that i am suppose to implement the has-relationship with:

    Code:
    #include "Flag.h"
    #include <iostream>
    using namespace std;
    Flag::Flag(){
      /* This is called in all GTK applications.
         We don't care about command line arguments so pass bogus ones*/
      gtk_init (0, NULL); 
      _mainWin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      _box = gtk_hbox_new(TRUE, 0);
      //add the has-a relationships here
    
    
    
      //all your code should be above this line.
    }
    
    void Flag::initWindow(){
     
        /* GtkWidget is the storage type for widgets */
        /* create a new window */
        //    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
          /* When the window is given the "delete_event" signal (this is given
         * by the window manager, usually by the "close" option, or on the
         * titlebar), we ask it to call the delete_event () function
         * as defined above. The data passed to the callback
         * function is NULL and is ignored in the callback function. */
        g_signal_connect (G_OBJECT (_mainWin), "delete_event",
    		      G_CALLBACK (delete_event), NULL);
    
        //add the container
        gtk_container_add(GTK_CONTAINER (_mainWin), _box);
        //show the container
        gtk_widget_show (_box);
        //change the window size to be something large enough
        gtk_window_set_default_size(GTK_WINDOW(_mainWin), 600, 400);
        /* show the window */
        gtk_widget_show (_mainWin);
        /* All GTK applications must have a gtk_main(). Control ends here
         * and waits for an event to occur (like a key press or
         * mouse event). */
        gtk_main ();
    }
    
    
    //don't worry about this function, it just hides some of the ugly details of
    //the underlying libraries that are being used.
    void Flag::destroy( GtkWidget *widget,
                         gpointer   data )
    {
        gtk_main_quit ();
    }
    
    //you do not need to worry about this method, it is written for you
    //and simply hides some ugly details of the library we are using.
    gboolean Flag::delete_event( GtkWidget *widget,
                                  GdkEvent  *event,
                                  gpointer   data )
    {
        /* If you return FALSE in the "delete_event" signal handler,
         * GTK will emit the "destroy" signal. Returning TRUE means
         * you don't want the window to be destroyed.
         * This is useful for popping up 'are you sure you want to quit?'
         * type dialogs. */
    
        g_print ("delete event occurred\n");
    
        /* Change TRUE to FALSE and the main window will be destroyed with
         * a "delete_event". */
        return FALSE;
    }
    
    int main()
    {
    
      Flag myFlag;
    
      myFlag.initWindow();
    
    }


    and here is one of the cpp files that im suppose to have the has-a relationship from (LeftBar.cpp):

    Code:
     
    #include "LeftBar.h"
    
    LeftBar::LeftBar(Flag* p){
      _parent = p;
      //now setup the underlying button functionality
      _realBar =  gtk_button_new();
      //try some coloring
      GdkColor leftC;
      gdk_color_parse ("green", &leftC);
      gtk_widget_modify_bg (_realBar, GTK_STATE_NORMAL, &leftC);
      gtk_box_pack_start(GTK_BOX (_parent->getBox()), _realBar, TRUE, TRUE, 0);
      gtk_widget_show(_realBar);
    }

    any help with this would be greatly appreciated, thanks in advance

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    Flag::Flag() {
    ...
        _something = new LeftBar( this );
    }
    Flag::~Flag() {delete _something;}
    It's a somewhat awkward design, because LeftBar's constructor is going to be calling get_box() on flag, so you have to make sure that everything you need for get_box() is handled in the ...
    Flag should also probably have a private copy ctor and assignment operator. the penalties of using new.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM