Course list http://www.c-jump.com/bcc/

Home work project 1: GUI class emulator app


  1. Description
  2. Instructions
  3. How to submit

Description



Instructions


  1. Use c255labs/example/Hwk_01_pointers project as your starting point for this homework.

  2. Apply the following changes to the Window class in window.h:

    
        // window.h
        // ...
        class Window {
            char* title;
            Input* inp_box;
            Output* out_box;
            Button* btn_set_text;
        public:
            // constructor
            Window( char* title_ );
            // operations
            void click_btn_set_text();
            void show();
            void run();
        };//class Window
    
    
  3. With this change, the Window class no longer contains Input, Output, and Button objects. Instead, the objects must be created dynamically. Modify Window constructor as follows:

    
    // window.cpp
    // ...
    Window::Window( char* title_ )
    {
        title = title_;
        inp_box = new Input;        // create Input object dynamically
        out_box = new Output;       // create Output object dynamically
        btn_set_text = new Button;  // create Button object dynamically
    
        inp_box->value( "12345" );  // use pointer-to-object dereference operators
        out_box->value( "67890" );
    }
    
    
  4. Carefully examine the rest of the window.cpp file. Replace all member access
    operators ("." dot operators) with the pointer-to-object dereference operator ->. For example,

    
        Old code            New code
        -----------------   -----------------
        inp_box.value()     inp_box->value()
    
    
  5. Compile and run the program.

  6. What happens to dynamically allocated objects at the end of the program? You can use unique_ptr "smart pointer" to automatically delete dynamically allocated objects. Apply the following changes to the Window class in window.h:

    
        // window.h
        // ...
        #include <iostream>
        #include <memory> // for unique_ptr
        // ...
        class Window {
            char* title;
            std::unique_ptr< Input > inp_box;
            std::unique_ptr< Output > out_box;
            std::unique_ptr< Button > btn_set_text;
        public:
            // constructor
            Window( char* title_ );
            // operations
            void click_btn_set_text();
            void show();
            void run();
        };//class Window
    
    
  7. Modify Window constructor as follows:

    
    // window.cpp
    // ...
    Window::Window( char* title_ )
        :
    inp_box( new Input ),
    out_box( new Output ),
    btn_set_text( new Button )
    {
        title = title_;
    
        // inp_box = new Input;        // create Input object dynamically
        // out_box = new Output;       // create Output object dynamically
        // btn_set_text = new Button;  // create Button object dynamically
    
        inp_box->value( "12345" );
        out_box->value( "67890" );
    }
    
    
  8. Compile and run the program.

     


How to submit