Course list http://www.c-jump.com/bcc/
The purpose of this lab is to explore a set of important C++ concepts:
C++ class
member variables
member functions
public and private access to class members
Objects, creation of objects, object lifecycle
Class constructor
C++ pointers
The new operator, dynamic creation of objects, free store memory
Pointer-to-object dereference operator -> for pointers to objects
Application memory model
Use c255labs/example/Hwk_01_pointers project as your starting point for this homework.
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
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" );
}
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()
Compile and run the program.
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
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" );
}
Compile and run the program.
You should submit all source files from your src subdirectory, or ZIP all files in your src and upload them as a single ZIP archive.
Go to CIS-255 online website. You should have already received your login and password for this website. If not, then email me at
and I will reply back with your access credentials.
On the Main Menu, follow the link to Submit Homework, select Project 1: GUI class emulator, and upload your source files.
Note: the system will display an 8-digit confirmation number once the file is successfully uploaded. You can save the confirmation number for your own records, but this is not a required step and no further action is needed on your part.
Let me know if you are experiencing any problems, otherwise -- good luck on your first assignment!