Course list http://www.c-jump.com/bcc/
In this lab we continue using C++ classes to control and manipulate our GUI window. In object-oriented programming, a class implementation file (.CPP or .CXX) often contains the implementation code for the member functions of a class, placed outside of the class declaration in the header file (.H):
|
|
The motivation for this arrangement is driven by three important principals:
the header file can be included in multiple C++ source files
class declaration in .H has a clean interface, not cluttered by the implementation
member function definitions in .CPP are compiled into a single .OBJ file
These three rules make the build process extremely happy, since the ambiguity of function bodies scattered in multiple .OBJ files may severely confuse the linker.
In this lab, to lay out a good foundation for our future projects, we make the necessary adjustments and separate the function declarations from the function definitions.
The prototype for this exercise is Lab 3a:
c255labs\labs\c255_lab03a_win_close
To get started, make a copy of Lab 3a subfolder (or download c255_lab03a_win_close.zip and unzip it under the c255labs\labs subdirectory.)
Rename the new folder as c255_lab04.
Open solution file in Visual Studio and follow the usual steps to finish renaming the project.
Rename file CDemoWindow.h as CMainWindow.h
Rename file CDemoWindow class as CMainWindow
To separate CMainWindow declaration and implementation, add new source file named CMainWindow.cpp to the project. Store the new file under the src subdirectory:
c255labs\labs\c255_lab04\src\CMainWindow.cpp
Open CMainWindow.h in text editor, then copy and paste the following code:
// CMainWindow.h #ifndef _CMAINWINDOW_H_INCLUDED_ #define _CMAINWINDOW_H_INCLUDED_ #include "../fluid_project/CFluidWindow.h" class CMainWindow : public CFluidWindow { public: void show(); // callback functions static void callback_window_closing(Fl_Widget* widg, void* userdata_); };//class CMainWindow #endif // _CMAINWINDOW_H_INCLUDED_
Open CMainWindow.cpp and paste the following code:
// CMainWindow.cpp #include "CMainWindow.h" void CMainWindow::show() { // Window callbacks: win_app->callback( (Fl_Callback*)CMainWindow::callback_window_closing, (void*)(this) ); // Make the window visible: win_app->show(); } // callback functions void CMainWindow::callback_window_closing(Fl_Widget* widg, void* userdata_) { std::cout << "X button clicked -- exiting the program\n"; while( Fl::first_window() ) { Fl::first_window()->hide(); } }
For debugging purposes, it is convenient to access std::cout from anywhere in the application. The CFluidWindow.h header is included in every file, so this is a good place for
#include <iostream>
directive. Because CFluidWindow.h is a generated file, we have to make this change in FLUID. Start FLUID program and open project
c255labs\labs\c255_lab04\fluid_project\CFluidWindow.fl
Use FLUID menu
Edit/Select None New/Code/Declaration...
select option "in header file only" and type
#include <iostream>
Recall that the order of items in the FLUID project tree can be rearranged by F2 (up) and F3 (down). Move the newly added #include to the top by hitting F2 key a few times.
This is a self-learning lab. There are no files to submit. Make sure to complete all required steps to build and test the project on your own home computer.