Lab 8:
Class Construction, Initialization and Use of Doxygen
Download Lab slides
Topics
- Constructors
- Data member assignment
- Function overloading and
default arguments
- Member initialization lists
- The Doxygen
documentation system
Review
- Constructors are
special member functions having the same name as the class and they are
used to initialize the data members of objects of the class.
- Constructors are
automatically invoked when a class object is created.
- A constructor must not specify a return type or
explicitly return a value.
- A class can have more than
one constructor, but different constructors should take different
parameters.
Default Constructor
- A default constructor is a constructor that is called
with NO argument. E.g. X::X() for class X.
- It is given by the compiler
automatically if no other constructor is defined by creating an object
with enough space for the data members, but their initial values CANNOT be
trusted.
- If you overload the default
constructor, you can specify default values as initial values for the data
members.
Copy Constructor
- A copy constructor creates a clone of an existing
object by initializing a new object with an existing object of the same
class.
- If you have no copy
constructor defined, the compiler will supply a default "copy
constructor" to
create the clone, which simply copy data members over.
- Copying an object using the
default copy constructor may work for simple objects. However, if there are
pointers in the original object, only the pointers will be duplicated, not
the data that are pointed to.
Data Member Assignment (Memberwise
Assignment)
- If you DO NOT write your own memberwise assignment, the compiler will provide the
default memberwise
copy.
- Memberwise assignment/copy does NOT work whenever memory allocation
is required for the class members.
- Both default copy
constructor and default memberwise assignment
are shallow
copy (i.e., they
don't copy data pointed by pointers).
Example:
class Word {
public:
Word (const char* s, int k=0);
// ...
};
Default copy
constructor:
Word movie("Titanic"); // Word movie = "Titanic";
Word song(movie); // Word song = movie;
There is associated
default memberwise assignment:
Word movie("Titanic");
Word song;
song = movie;
Function
Overloading and Default Arguments
- Function overloading allows
functions to have the same name but with different numbers of parameters
and/or parameter types.
- However, specifying default arguments is a better option than using
overloading.
- All parameters that do not
specify default values MUST appear before the
default arguments.
Member
Initialization Lists
- Class
members may be initialized using member initialization lists or inside the
body of constructors.
- Here is an example.
class Word
{
public:
Word () { cout << "D" <<endl;}
Word (char* s1) { cout << s1 << endl;}
Word (const Word & w) { cout << "C\n";}
Word & operator= (const Word & w) { cout << "A\n"; return *this;}
};
class Word_Pair
{
public:
Word w1;
Word w2;
// Word_Pair (char *s1, char *s2) : w1(s1), w2(s2) {}
Word_Pair (char *s1, char *s2)
{
w1 = s1;
w2 = s2;
}
};
int main ()
{
Word_Pair p("hello", "world");
return 0;
}
The Doxygen documentation system
- Doxygen is a documentation system
for C++, C, Java, Objective-C, Python, IDL (Corba and Microsoft flavors) and, to some extent, PHP,
C#, and D.
- It can generate
documentations in HTML or LaTeX format from a
set of documented source files. There is also support for generating
output in RTF (MS Word), PostScript, hyperlinked PDF, compressed HTML, and
Unix man pages.
- It is distributed under GNU
General Public License. You are permitted to use, copy, modify, and
distribute this software and its documentation under the terms of this
license.
Using
Doxygen via command line
- Step 1: Creating a
configuration file
- To
create a template configuration file, you can enter:
doxygen
-g <config-filename>
If you omit the file
name, a file named Doxyfile will be created.
Now, you can edit the configuration file using a text editor. The configuration
file has a format that is similar to that of a (simple) Makefile.
It consists of a number of assignments (tags) of the form:
TAGNAME
= VALUE
TAGNAME = VALUE1 VALUE2 ...
You can leave most of
the tags in the template configuration file to their default values.
Here are some tags that you probably need to update:
PROJECT_NAME
The PROJECT_NAME tag is a single word
(or a sequence of words surrounded by double-quotes) that should identify the
project for which the documentation is generated. This name is used in the
title of most generated pages and in a few other places.
PROJECT_NUMBER
The PROJECT_NUMBER tag can be used to
enter a project or revision number. This could be handy for archiving the
generated documentation or if some version control system is used.
OUTPUT_DIRECTORY
The OUTPUT_DIRECTORY tag is used to
specify the (relative or absolute) path into which the generated documentation
will be written. If a relative path is entered, it will be relative to the
location where doxygen was started. If left blank the
current directory will be used.
GENERATE_HTML
If the GENERATE_HTML tag is set to YES
(the default) Doxygen will generate HTML output.
GENERATE_LATEX
If the GENERATE_LATEX tag is set to YES
(the default) Doxygen will generate LaTeX output.
For more details
about each tag, please read this page.
- Step 2:
Running Doxygen
- To
generate the documentation you can enter:
doxygen
<config-filename>
Depending on your
settings, Doxygen will create HTML, RTF, LaTeX, XML and/or man directories inside the output
directory. As the names suggest, these directories contain the generated
documentation in HTML, RTF, LaTeX, XML and Unix-man
page format.
Example workflow:
In the Linux console, type:
doxygen -g
Then edit the default configuration
file:
pico Doxyfile
(or use vi/emacs)
Change Project_Name to
Test_Project, Project_Number to
1, Output_Directory to test_project,
and save file.
Generate
documentation by typing:
doxygen Doxyfile
A directory called “test_project”
would appear, and by default Doxygen generates both html and latex documentations.
Go into the html directory, open index.html, and you should see
a piece of documentation like this:
Lab Task
Part
I:
Project - Money:
Download the
following files: Money.h, Money.cpp main.cpp
and place them in a folder named “money”.
Your
task:
- Overload
the default constructor of class
MoneyType to set the initial value of the
data members to be 0. (You need to declare the constructor in Money.h and
implement it in Money.cpp.)
- Modify
the member function Initialize
to be another constructor. (You need to modify it in both Money.h and Money.cpp.)
- Implement
the member function Normalize to normalize the cents to be
between 0 and 99 and adjust the dollar amount accordingly. (e.g. 1 dollar and 150 cents should be 2 dollars and 50
cents after normalization.)
Project - Grid:
Download the
following files: grid3d.h, grid3d.cpp and main.cpp
and place them in a folder named “grid”.
In main.cpp:
- findCentroid is used to compute the
centroid of a triangle, which is just the "average" of its three
vertices.
- findArea is used to compute the area
of a triangle using Heron's formula.
- When an
array of objects is created, each element is initialized by the default
constructor. An error message will be given by the compiler if no default
constructor is provided, assuming that you have provided at least one type
of constructor.
Your
task:
1.
Implement the following functions in grid3d.cpp.
·
grid3d::grid3d()
·
grid3d::grid3d(const grid3d& g)
·
grid3d::operator=(const grid3d& g)
·
grid3d::~grid3d()
·
grid3d::getx()
·
grid3d::gety()
·
grid3d::getz()
The output should look like this:
Point A is created by typical constructor |
Part
II:
Your
task:
1.
Use Doxygen on Unix
to generate HTML documentation for Project
Grid in Part I. (Refer to the instructions above or in the slides.)
You can try exploring
other tags not covered above. Usage of the tags are explained in the default
configuration file.
Expected Doxygen HTML output for Class grid3d: html\classgrid3d.html