These coding standards deal specifically with C++ programs that use classes.
main
function. This file should be named
after the project and have a .C file extension. For example, the main
function for Project 3 would be in a file named proj3.C
.
.H
file extension.
.H
file, but use a .C
extension.
.H
file
extension.
.H
) file.
Name each file after the class and give it a .C
file extension.
#ifndef FOO_H #define FOO_H . . #endifstyle. This style is used to avoid multiple inclusions of the header file.
Foo.H
refers to an
ifstream
entity and a string
entity. Then,
definitely do include fstream.h
and string.h
in the Foo.H
. Now, suppose the implementation file
Foo.C
refers to an ifstream
entity, but not
a string
entity. Then Foo.C
should include
Foo.H
and fstream.h
, but not
string.h
. Note that fstream.h
is included
even though its inclusion is also caused by including Foo.H
.
Guarded header files keep this from causing a multiple definition error and your
code becomes cleaner and more readable.
Proj3
file
(notice the capital "P"). This is very important because the grader will look for
that file to run your program. The grader will not run a.out
or any
other executable. The executable name is controlled by your makefile - get it right.
.H
or .C
file, must contain a file
header comment as described in the General C++ Coding Standards.
The header file for a class presents the public interface for the class. Therefore, we adopt the convention that class member function header comments are done in the header file. However, implementation file member function bodies should still contain comments.
For an example of header file documentation, see the heap.H
file that was given to
you for Project 2. You are encouraged to adopt this sample style. If you prefer your own style, that is acceptable, but it must meet the specifications laid out here.
private
, protected
and
public
section in a class definition. public
comes first, followed by protected
and then private
.
friend
function prototypes before the public
section.
friend
designation is to be used judiciously.
You should not use friend
simply to avoid using the proper accessor and mutator functions. Projects
which abuse friend
will lose points proportionate to the
degree of abuse.
private
.
const
), but not global
variables.
const
for constants, not #define
.
virtual
methods, provide a virtual
destructor.
const
method. Example:
int getSize() const; void printSum() const;