Style Guidelines
·
main.cpp
· Functions.h
· Functions.cpp
· Class.h
· Class.cpp
·
main.cpp
// this is a single line comment
/*
* this is a block comment that
* can span several lines
*/
// place all header files you've written together, in alphabetical order
// include on the header files you need
#include "Functions.h" // be sure to ONLY include the header
// files and not the implementation files
#include "Class.h"
// place all C++ standard libraries together, in alphabetical order
// include only the libraries you need
#include <fstream> // for file streams (ifstream, ofstream)
#include <iomanip> // for I/O Manipulators (precision, aligning, etc.)
#include <iostream> // for standard input/output
#include <string> // for string library
#include <vector> // for vector library
using namespace std; // we are using the standard namespace
// place all C standard libraries together, in alphabetical order
// include only the libraries you need
#include <cctype> // for tolower(), isalpha(), etc.
#include <cmath> // for sqrt(), pow(), sin(), etc.
#include <cstdlib> // for srand(), rand()
#include <ctime> // for time()
/*
* any variables defined above main() are in global scope and can be
* accessed anywhere. This is BAD. Only declare & define constants
* in global scope.
*/
const double PI_VALUE = 3.1415926535;
// structs are named using UpperCamelCase to denote it is a datatype
struct ClassRoom {
string buildingName;
int roomNumber;
};
// every program must have a main() function
// - it is the entry point to our program
int main() { // the curly brace begins a new code block
srand( time(0) ); // seed the RNG - do this once per program as first statement
// indent the contents of a code block a single tab length
// Template for variable declaration
// anything inside of [brackets] is optional
// Version1: [modifiers] dataType identifierName [= initialValue];
// Version2: [modifiers] dataType identifierName[(initialValue)];
int ageOfColosseum; // declare a variable
// use lowerCamelCase to make a descriptive variable name
ageOfColosseum = 1940; // define a variable (assign a value)
int numRomanEmperors = 71; // declare and define a variable in one line
const int VATICAN_BUILT = 1626; // declare and define a constant
// must define a constant when it is defined
// use UPPER_SNAKE_CASE to make a descriptive constant name
// declare (and define) multiple variables of the same type at once
char firstInitial = 'I', secondInitial('T'), currentEmperorInitial;
cout << "The Colosseum was built in 70-80 A.D. and is "
<< ageOfColosseum << " years old." << endl;
cout << endl;
// when prompting the user to enter a value via cin, preceed the input with a prompt
// using cout so the user knows what to enter
int currentYear;
cout << "Please enter the current 4 digit year (e.g. 1999): ";
cin >> currentYear;
cout << "St. Peter's Basilica was built in " << VATICAN_BUILT
<< " and is " << (currentYear - VATICAN_BUILT) << " years old." << endl;
if( currentYear >= 2400 ) {
// indent the contents of a new code block
cout << "Duck Dodgers of the 24th and a Half Century!" << endl;
} else if( currentYear >= 2000 ) { // place else if and else on the same line the prior code block ends
cout << "In the year 2000, robots will do 80% of the work. - Conan O'Brien." << endl;
} else {
// use curly braces to denote a code block for if/else if/else even if the code
// block only has one statement
cout << "Let's party like it's 1999. - Prince" << endl;
}
for( int i = 0; i < 10; i++ ) {
// indent the contents of a new code block
// use a code block for a for loop even if it contains only a single statement
cout << i << endl;
}
char userResponse;
do {
cout << "Enter a letter (q to quit): ";
cin >> userResponse;
} while( userResponse != 'q' ); // place the while on the same line as the end of the do
// for function calls, add spaces within the () and after each argument
cout << "5 + 3 = " << add( 5, 3 ) << endl;
cout << "5 - 3 = " << sub( 5, 3 ) << endl;
ClassRoom lectureLab; // create a variable of our custom struct type
lectureLab.building = "Brown"; // access components of a structure using dot operator
lectureLab.roomNumber = 316;
cout << "The length of the building name is: "
<< lectureLab.building.length() // access string functions with dot operator
<< endl;
// Template for vector declaration
// anything inside of [brackets] is optional
// Version1: [const] vector< dataType > identifierName;
// Version2: [const] vector< dataType > identifierName[(initialSize)];
// Version3: [const] vector< dataType > identifierName[(initialSize, initialValue)];
vector<int> numbers; // create an empty vector of integers
numbers.push_back( 5 ); // add the value 5 to the vector
numbers.at( 0 ) = 6; // access individual elements using the at() function
Calculator fourFunctionCalc( 4.5, 3.5 ); // object names follow lowerCamelCase
cout << fourFunctionCalc.getLeftHandSide() << " + " << fourFunctionCalc.getRightHandSide() << " = "
<< fourFunctionCalc.add() << endl;
fourFunctionCalc.setLeftHandSide( 13.0 );
fourFunctionCalc.setRightHandSide( 1.5 );
cout << fourFunctionCalc.getLeftHandSide() << " / " << fourFunctionCalc.getRightHandSide() << " = "
<< fourFunctionCalc.divide() << endl
return 0; // alert the OS our program exited with result 0 (success)
} // the curly brace ends the code block that it corresponds to
// every open brace needs a matching end brace
Functions.h
// place header guards around all your header files
// make the name of the file the name of the value to test for
// use UPPER_SNAKE_CASE for your definition value
#ifndef FUNCTIONS_H // ask compiler if FUNCTIONS_H has been
#define FUNCTIONS_H // if not, define FUNCTIONS_H
// place ALL function prototypes into the header file
/** @brief adds two integers together
* @desc Returns the integer sum of two integer parameters
* @param x first operand to add
* @param y second operand to add
* @return sum of x and y
*/
int add( int x, int y );
/** @brief subtracts two integers
* @desc Returns the integer difference of y from x (x - y)
* @param value to start with
* @param value to subtract
* @return difference of x and y
*/
int sub( int x, int y );
#endif // FUNCTIONS_H // ends the corresponding #ifndef
Functions.cpp
#include "Functions.h" // include the file with the corresponding prototypes
int add( int x, int y ) { // function definition
return x + y;
}
int sub( int x, int y ) {
return x - y;
}
Class.h
// place header guards around all your header files
// make the name of the file the name of the value to test for
// use UPPER_SNAKE_CASE for your definition value
#ifndef CLASSNAME_H // ask compiler if CLASSNAME_H has been
#define CLASSNAME_H // if not, define CLASSNAME_H
// place a single Class declaration into its own file
class Calculator { // class names follow UpperCamelCase
public:
/** @brief creates a default calculator
* @desc Creates a default calculator with both operands set to 1
*/
Calculator(); // provide a default constructor
/** @brief creates a default calculator
* @desc Creates a default calculator with both operands set to 1
* @param LHS left hand side of calculation
* @param RHS right hand side of calculation
*/
Calculator(const double, const double); // provide a parameterized constructor
// create getters and setters (if appropriate) for your private data members
/** @brief return the left hand side operand
* @return value of left hand side operand
*/
double getLeftHandSide() const; // getters are const functions
/** @brief set the left hand side operand
* @param LHS value of left hand side operand
*/
void setLeftHandSide(const double);
/** @brief return the right hand side operand
* @return value of right hand side operand
*/
double getRightHandSide() const;
/** @brief set the right hand side operand
* @param RHS value of right hand side operand
*/
void setRightHandSide(const double);
// add any other related functions and mark as const if they do not change the callee
/** @brief return the sum of the operands
* @return LHS + RHS
*/
double add() const;
/** @brief return the difference of the operands
* @return LHS - RHS
*/
double subtract() const;
/** @brief return the product of the operands
* @return LHS * RHS
*/
double multiply() const;
/** @brief return the division of the operands
* @return LHS / RHS
*/
double divide() const;
private:
double _leftHandSide; // private data members begin with underscore _
double _rightHandSide;
};
#endif // CLASSNAME_H // ends the corresponding #ifndef
Class.cpp
#include "Class.h" // include the file with the corresponding prototypes
Calculator::Calculator() {
_leftHandSide = 1.0;
_rightHandSide = 1.0;
}
Calculator::Calculator(const double LHS, const double RHS) {
_leftHandSide = LHS;
_rightHandSide = RHS;
}
double Calculator::getLeftHandSide() const {
return _leftHandSide;
}
void Calculator::setLeftHandSide(const double LHS) {
_leftHandSide = LHS;
}
double Calculator::getRightHandSide() const {
return _rightHandSide;
}
void Calculator::setRightHandSide(const double RHS) {
_rightHandSide = RHS;
}
double Calculator::add() const {
return _leftHandSide + _rightHandSide;
}
double Calculator::subtract() const {
return _leftHandSide - _rightHandSide;
}
double Calculator::multiply() const {
return _leftHandSide * _rightHandSide;
}
double Calculator::divide() const {
if( _rightHandSide != 0 ) {
return _leftHandSide / _rightHandSide;
} else {
return 0.0;
}
}