Course list http://www.c-jump.com/bcc/
When I add a new member function to my class, I typically write everything in-line, directly in the body of the class, because it's the easiest thing to do. When the code is new, I may still change function parameters, specify a different return type, and change the executable code, because I am still debugging the program. However, as soon as I am satisfied with the new operation, I move the function definition out of the header file into the implementation file.
In Lab 15, plenty of new code went to the header file CDrawBox.h. Although implementation source file CDrawBox.cpp was also added to the project, each class member function remains written directly in the body of a corresponding C++ class.
In this lab, instead of adding new features, we shall reorganize the code by moving the member function definitions out of CDrawBox.h and place them in CDrawBox.cpp.
Make a copy of Lab 15 subfolder
c255labs\labs\c255_lab12_radio_btn
or download and unzip c255_lab15_scratch.zip
Rename the new folder as c255_lab16_scratch.
Open solution file in Visual Studio and finish the usual steps to rename the project.
Consider a complete decalration-definition of a C++ class:
class Person { string name; public: Person() {} // default constructor Person( string name ) : name( name ) {} // specific constructor string name() const { return name; } // get name void name( string name_ ) { name = name_; } // set name }//class Person
Where should this code be written in the application? It is very likely that as a concept, and as an object - the Person may be needed in different logical parts of the program. It is also likely that multiple C++ source files will require access to the Person class declaration. It is a standard coding practice to place the class declaration in a header file, such as "person.h", and then #include that header on a need-by-need basis.
The member functions of the Person class above are written in-line, that is, directly in the scope of the class definition. When all functions are completely defined in the body of the class, it becomes a complete decalration-definition which provides the executable code for all functions.
Alternatively, just as with non-member functions, C++ allows separation between member function declarations and definitions:
|
|
All definitions of member functions in .CPP file are collectively called class implementation.
The latter arrangement improves code readability and maintainability in the implementation file (person.cpp) and reduces the complexity of the class declaration in the header file (person.h).
Open both CDrawBox.h to CDrawBox.cpp in the Visual Studio editor. Observe that the header file contains many class declarations with the member functions defined in-line. Your task is to move the executable code out of the CDrawBox.h and place it in the implementation file CDrawBox.cpp.
As a rule of thumb,
|
|
When I move the code from one file to another, I prefer to display both files side-by-side and use copy-and-paste, but remember to type a semicolon with declarations,
// person.h
// ...
Person(); // declaration
whereas the definitions need only a set of curly braces:
// person.cpp // ... Person::Person() // definition { }
When dealing with many small functions in CDrawBox.h, be patient and compile the program often after applying small, incremental changes to both files.
Last but not least, don't forget to add the class scope resolution prefix (such as Person::) to your function definitions in the .CPP file:
// person.cpp
// ...
Person::Person() // definition
{
}
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.