g++ -g -Wall -Werror -O2 -std=c++11 prog1.cc -o prog1
This produces an executable file called prog1 which you can run using
the command:
./prog1
The -g flag tells the compiler to include information needed by the GDB debugger. When given the -Wall flag, the compiler will print out every warning that it can generate for your code. The -Werror flag causes the compiler to consider every warning to be an error. Properly formatted code does not produce warnings. For most purposes, you should compile with the -O2 flag. The compiler will produce more warnings this way. However, the -O2 flag can cause unpredictable behavior in GDB, so when stepping through code in the debugger, compile with -O0 instead. Finally the -std=c++11 tells the compiler to use c++ version 11. Other values of this flag are c++14, etc. If you require more advanced features present in more recent versions of C++ be sure to use the appropriate std.
For production code, a more common compile command would look like:
g++ -Wall -Werror -O2 -std=c++11 prog1.cc -o prog1
In this case, we do not generate debug symbols.
The use of Makefiles to aid the compilation process is highly recommended in general, and required for CS3610/5610D. Makefiles contain a set of instructions to build your program. The file is read when you type 'make' at the command line. Makefiles can save time by checking dependencies and compiling only modified code.
Help page for makefiles. Each class should usually be defined in a separate file. Template functions should be in a file with a suffix of .template or .temp. Template functions are encouraged since they allow greater code reuse.
Each set of functions and/or classes that implements a different algorithm or data structure should be in a separate set of files. This allows better organization of your code, and eases code reuse.
Template files should be included at the end of header (interface definition) files (.h).
Recall that abstract data types are defined by describing the interface to the data type in the .h file, the implementation in a .cc or .template file, and are used by including the .h file.
INTRODUCTORY DOCUMENTATION
At the beginning of each file there should be a comment block that
contains:
The format to be used is based on doxygen standards, and an example is
given below:
/*******************************************************************
* \file
The following is an example of an introductory comment block that
follows the format above:
/********************************************************************
* \file prog1.cc
* \brief Project 1 -- Video Calculator
*
* Author: Elwood Scuggins
* Email: Clueless@ace.cs.ohio.edu
*
* Lab Section: 10 (Chris Hayes)
*
* Description: This program inputs the total number of minutes,
* the number of minutes for commercials, and the
* number of minutes for episodes. The program
* computes the total number of minutes for videos,
* the number of videos, and the number of seconds
* left over.
*
* Date: January 26, 2020
*
********************************************************************/
DECLARATIONS
const float BASE = 2.0; ///< divisor to obtain base 2 const char HYPHEN = '-'; ///< signals word continued on next line const int NAME_LENGTH = 20; ///< names can be 20 characters
It is permissible to capitalize the first letter of words in an identifier with class, structure, union, or type names. For example, you may wish to use Student, StudentName, or ListNode as names for your classes.
float volts; ///< voltage in the circuit int amps; ///< amperage in the circuit char circuit_name[NAME_LENGTH]; ///< name of the circuit
left = next_left; right = next_right;use
left = next_left; ///< move to the left right = next_right; ///< move to the right
/****************************************************************** * * Class: Complex * * Purpose: To allow a programmer to use complex variables as a data * type. * * Functions: * Constructors * Complex(double, double) general version * Complex(double) if the Complex variable only has a real part * Complex() default constructor initializes to 0. * setters * set (double, double) general version * set (double) if the Complex variable only has a real part * getters * real_part to return the real part of the complex variable * imag_part to return the imaginary part of the complex variable * output outputs a human readable version to an open output stream * *******************************************************************/
/****************************************************************** * * Function: find_space_cost * * Purpose: calculates and returns the charge for shipping cargo * between two planets. * * Parameters: @param distance - distance in miles between two planets * @param weight - weight in pounds of item being shipped * * @return - the charge for shipping cargo between * two planets * * Member/Global Variables: none * * Pre Conditions: variables distance and weight have valid values * * Post Conditions: returns the cost in dollars of shipping a * package that weighs weight pounds a distance * of distance miles. * * Calls: function cargo_rates * *******************************************************************/
This detailed comment should be placed in the implementation file, as which functions are called, etc. is not of general interest to someone who merely wants to use your class/function. A short version of the above comment should be placed in the .h file.
/// Space Complexity: O(number of items in list) /// /// Time Complexity: O((number of items in list)^2) ///
int main ()
cout << "Today's date is " << today << endl;instead of
cout << "Today's date is " << today << "\n";
If any statement or block of statements was at all difficult for you to develop, or if the purpose of the statement or block is not obvious, consider commenting in-line (any non-header comment) describing your thought process, so that it is easier to read your code, and for you to recall what it is supposed to do when debugging. Remember the purpose of comments is to improve the clarity and readability of your code. In-line comments are encouraged where they meet this goal.
The body of a control structure, such as an if, a switch, a while, a do, or a for statement, should be indented. Always use curly braces even if there is only one statement:
if (expression) {
statements
}
if (expression) {
statements
} else {
statements
}
switch (selector variable) {
case case-1-value: case 1 statements;
break;
case case-2-value: case 2 statements;
break;
...
case case-n-value: case n statements;
break;
default: default statements;
break;
}
while (expression) {
statements
}
do {
statements
} while (expression);
for (initialization; test; increment) {
statements
}
As an alternative to the above formats, the use of the formats below
is also acceptable:
if (expression)
{
statements
}
if (expression)
{
statements
}
else
{
statements
}
switch (selector variable)
{
case case-1-value: case 1 statements;
break;
case case-2-value: case 2 statements;
break;
...
case case-n-value: case n statements;
break;
default: default statements;
break;
}
while (expression)
{
statements
}
do
{
statements
} while (expression);
for (initialization; test; increment)
{
statements
}
PROGRAM DESIGN