cppProgramStructure-web
cppProgramStructure-web
Steven Zeil
May 25, 2013
Contents
1 Separate Compilation 2
1.1 Separate Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Pre-processing 5
2.1 #include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Other Pre-processing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Modules 17
4.1 Coupling and Cohesion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1
The Structure of a C++ Program
1 Separate Compilation
• C++ programs can range from a handful of statements to hundreds of thousands
.............................................
CS333 2
The Structure of a C++ Program
Compiling
• All object code files are linked to produce the exe- : fb(x3000)
cutable Linking
foo.exe
Executable code: x2000(x1000)
: x2000(1)
: x12A0(x1000,x3000)
: x2000(x1000)
: x2000(x3000)
.............................................
Object Code
CS333 3
The Structure of a C++ Program
Compiling
Linking
foo.exe
Executable code: x2000(x1000)
: x2000(1)
: x12A0(x1000,x3000)
: x2000(x1000)
: x2000(x3000)
.............................................
Linking
CS333 4
The Structure of a C++ Program
2 Pre-processing
The # Preprocessor
CS333 5
The Structure of a C++ Program
Compilation Units Headers
The preprocessor runs before the compiler proper. The pre- Pre-processing
processor:
• processes preprocessor instructions calls: fb(a) calls: fb(1) calls: fa(a,b), fb(a)
: fb(b)
Compiling
– lines beginning with #
A.o B.o C.o
Object
• strips out comments Code
a: x1000
fa: x12A0
fb: x2000
code: fb(1)
b: x3000
main: x3400
code: fb(x1000) code: fa(a,x3000), fb(a)
: fb(x3000)
Linking
foo.exe
Executable
code: x2000(x1000)
: x2000(1)
: x12A0(x1000,x3000)
: x2000(x1000)
.............................................
: x2000(x3000)
Pre-Processor Instructions
The common pre-processor instructions are
• #include
– insert a file
• #define
– define a macro
– check to see if a macro has been defined
CS333 6
The Structure of a C++ Program
.............................................
2.1 #include
#include
• Two versions
– #include <headerName>
* inserts a system header file from a location defined when the compiler was installed
– #include " fileName "
.............................................
CS333 7
The Structure of a C++ Program
• We ask the compiler to only run the preprocessor and save the result:
g++ −E C. cpp > C. i
# 1 "C.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "C.cpp"
# 1 "A.h" 1
# 1 "A.h" 1
CS333 8
The Structure of a C++ Program
#include <iostream>
int main() {
cout << "Hello World" << endl;
return 0;
}
.............................................
Deja-Vu
CS333 9
The Structure of a C++ Program
Compilation Units Headers
Pre-processing
Compiling
Linking
foo.exe
Executable
code: x2000(x1000)
: x2000(1)
: x12A0(x1000,x3000)
: x2000(x1000)
.............................................
: x2000(x3000)
• Used to define macros (symbols that the preprocessor will later substitute for)
i nt main ( ) {
cout << "Running version "
<< VersionNumber
<< endl ;
CS333 10
The Structure of a C++ Program
– Much more elaborate macros are possible, including ones with parameters
.............................................
.............................................
• All of these macros are used to reduce the amount of code seen by the actual compiler
#ifndef A2_H
#define A2_H
#endif
CS333 11
The Structure of a C++ Program
#ifndef B2_H
#define B2_H
#endif
• We ask the compiler to only run the preprocessor and save the result:
g++ −E C2 . cpp > C2 . i
.............................................
# 1 "C2.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "C2.cpp"
CS333 12
The Structure of a C++ Program
# 1 "A2.h" 1
• Imagine now, how much we would have saved if if that were iostream instead of A2.h
.............................................
– . . . is undeclared
– . . . is undefined
CS333 13
The Structure of a C++ Program
• Fixing these requires that you understand the difference between declarations and definitions
.............................................
Declarations
A declaration in C++
.............................................
Definitions
A definition in C++
• gives the compiler enough information to generate this and assign it an address
.............................................
CS333 14
The Structure of a C++ Program
• A name must be declared before you can write any code that uses it.
• A name can be declared any number of times, as long as the declarations are identical.
• A name must be defined exactly once, somewhere within all the separately compiled files making up a program.
.............................................
.............................................
CS333 15
The Structure of a C++ Program
• Declaration:
i nt myFunction ( int x , int y ) ;
• Definition
• The declaration provides only the header. The definition adds the body.
.............................................
typedef f l o a t Weight ;
typedef s t r i n g * S t r i n g P o i n t e r ;
enum Colors { red , blue , green } ;
CS333 16
The Structure of a C++ Program
str uct S { . . . } ;
class C { . . . } ;
.............................................
4 Modules
Organizing Decls & Defs into Files
– A name must be declared before you can write any code that uses it.
– A name can be declared any number of times, as long as the declarations are identical.
– A name must be defined exactly once, somewhere within all the separately compiled files making up a program.
.............................................
CS333 17
The Structure of a C++ Program
.............................................
– specifically, declarations that need to be shared with different parts of the code
.............................................
CS333 18
The Structure of a C++ Program
.............................................
Coupling
CS333 19
The Structure of a C++ Program
.............................................
Cohesion
In something with high cohesion, all the pieces contribute to a well-defined, common goal.
CS333 20
The Structure of a C++ Program
In something with low cohesion, the pieces have little relation to one an-
other
.............................................
CS333 21
The Structure of a C++ Program
.............................................
CS333 22
The Structure of a C++ Program
.............................................
Online Auction
The overall algorithm is pretty simple
main ( fileNames [ ] ) {
readItems ;
readBidders ;
readBids ;
for each item {
resolveAuction ( item , bidders , bids )
}
}
We read in all the information about the auction, then resolve the bidding on each item, one at a time (in order by closing time
of the bidding for the items).
.............................................
CS333 23
The Structure of a C++ Program
#include <iostream>
#include <string>
#include <fstream>
int nItems;
const int MaxItems = 100;
// Names of items
std::string itemNames[MaxItems];
int nBidders;
const int MaxBidders = 100;
// Names of bidders
std::string bidderNames[MaxBidders];
// Account balances
double bidderBalances[MaxBidders];
CS333 24
The Structure of a C++ Program
int nBids;
const int MaxBids = 1000;
// Names of bidders
std::string bidBidder[MaxBids];
// Bid Amounts
double bidAmounts[MaxBids];
// Names of items
std::string bidItems[MaxBids];
/**
* Times in this program are represented by three integers: H, M, & S,
* representing
* the hours, minutes, and seconds, respecitvely.
*/
/**
* Print a time in the format HH:MM:SS (two digits each)
CS333 25
The Structure of a C++ Program
*/
void printTime (std::ostream& out, int h, int m, int s)
{
.
.
.
}
/**
* Compare to times. Return true iff time h1:m1:s1 is earlier than or equal to
* time time h2:m2:s2.
*
* Pre: Both times are normalized: s1, s2, m1, m2 are in the range 0..59,
* h1 and h2 are non-negative
* /
bool noLaterThan(int h1, int m1, int s1, int h2, int m2, int s2)
{
.
.
.
}
/**
* Read all bidders from the indicated file
*/
void readBidders (std::string fileName)
{
.
.
.
}
/**
* Read one item from the indicated file
CS333 26
The Structure of a C++ Program
*/
void readItem (istream& in, int itemNum)
{
.
.
.
}
/**
* Read all items from the indicated file
*/
void readItems (std::string fileName)
{
.
.
.
}
/**
* Find the index of the bidder with the given name. If no such bidder exists,
* return nBidders.
*/
int findBidder (std::string name)
{
.
.
.
}
/**
* Read all bids from the indicated file
*/
void readBids (std::string fileName)
{
CS333 27
The Structure of a C++ Program
.
.
.
}
/**
* Determine the winner of the auction for item #i.
* Announce the winner and remove money from winner’s account.
*/
void resolveAuction (int itemNum)
{
.
.
.
}
readItems (argv[1]);
readBidders (argv[2]);
readBids (argv[3]);
CS333 28
The Structure of a C++ Program
A Possible Modularization
From the description of the program and from a glance through the code, we might guess that the key modules would be :1
Bidders Data and functions related to the people bidding in the auction
Bids Data and functions related to the bids placed by those people
.............................................
Module Files
And we would then expect to divide the program into files corresponding to those modules:
CS333 29
The Structure of a C++ Program
• Put the main program and the core auction algorithm into auctionMain.cpp.
.............................................
Making a List. . .
If we then make a list of the functions and data in this program. . .
Functions Data
Functions Data
printTime nItems
bidderBalances
noLaterThan MaxItems
nBids
readBidders itemNames
MaxBids
readItem itemReservedPrice
bidBidder
readItems itemEndHours
bidAmounts
findBidder itemEndMinutes
bidItems
readBids itemEndSeconds
bidHours
resolveAuction nBidders
bidMinutes
main MaxBidders
bidSeconds
bidderNames
.............................................
CS333 30
The Structure of a C++ Program
• A “times” module was introduced when we saw that two of our functions were for manipulating time values.
CS333 31
The Structure of a C++ Program
.............................................
The “application”
The final two functions, resolveAuction and main, constitute the core algorithm, the "main application" for this particular
program, and so can be kept in the main cpp file.
.............................................
So our final division looks like:
CS333 32
The Structure of a C++ Program
CS333 33
The Structure of a C++ Program
.............................................
• checking to see if any of these declarations would only be used internally within a single module.
– A declaration only needs to appear in the header file if some code outside the module is using it.
– If it is only used from within its own module. it can be kept "hidden" inside that module’s .cpp file.
#include <iostream>
#include <string>
#include <fstream>
int nItems;
const int MaxItems = 100;
// Names of items
std::string itemNames[MaxItems];
int itemEndHours[MaxItems];
CS333 34
The Structure of a C++ Program
int itemEndMinutes[MaxItems];
int itemEndSeconds[MaxItems];
int nBidders;
const int MaxBidders = 100;
// Names of bidders
std::string bidderNames[MaxBidders];
// Account balances
double bidderBalances[MaxBidders];
int nBids;
const int MaxBids = 1000;
// Names of bidders
std::string bidBidder[MaxBids];
// Bid Amounts
double bidAmounts[MaxBids];
// Names of items
std::string bidItems[MaxBids];
CS333 35
The Structure of a C++ Program
/**
* Times in this program are represented by three integers: H, M, & S,
* representing
* the hours, minutes, and seconds, respecitvely.
*/
/**
* Print a time in the format HH:MM:SS (two digits each)
*/
void printTime (std::ostream& out, int h, int m, int s)
{
if (h < 10)
out << ’0’;
out << h << ’:’;
if (m < 10)
out << ’0’;
out << m << ’:’;
if (s < 10)
out << ’0’;
out << s;
}
/**
* Compare to times. Return true iff time h1:m1:s1 is earlier than or equal to
* time time h2:m2:s2.
*
* Pre: Both times are normalized: s1, s2, m1, m2 are in the range 0..59,
CS333 36
The Structure of a C++ Program
/**
* Read all bidders from the indicated file
*/
void readBidders (std::string fileName)
{
ifstream in (fileName.c_str());
in >> nBidders;
for (int i = 0; i < nBidders; ++i)
{
CS333 37
The Structure of a C++ Program
/**
* Read one item from the indicated file
*/
void readItem (istream& in, int itemNum)
{
in >> itemReservedPrice[itemNum];
in >> itemEndHours[itemNum];
char c;
in >> c; // Ignore the ’:’ between hours and minutes
in >> itemEndMinutes[itemNum];
in >> c; // Ignore the ’:’ between minutes and seconds
in >> itemEndSeconds[itemNum];
/**
* Read all items from the indicated file
*/
void readItems (std::string fileName)
{
CS333 38
The Structure of a C++ Program
ifstream in (fileName.c_str());
in >> nItems;
for (int i = 0; i < nItems; ++i)
readItem (in, i);
}
/**
* Find the index of the bidder with the given name. If no such bidder exists,
* return nBidders.
*/
int findBidder (std::string name)
{
int found = nBidders;
for (int i = 0; i < nBidders && found == nBidders; ++i)
{
if (name == bidderNames[i])
found = i;
}
return found;
}
/**
* Read all bids from the indicated file
*/
void readBids (std::string fileName)
{
ifstream in (fileName.c_str());
in >> nBids;
for (int i = 0; i < nBids; ++i)
CS333 39
The Structure of a C++ Program
{
char c;
in >> bidBidder[i] >> bidAmounts[i];
in >> bidHours[i] >> c >> bidMinutes[i] >> c >> bidSeconds[i];
string word, line;
in >> word; // First word of itemName
getline (in, line); // rest of item name
bidItems[i] = word + line;
}
}
/**
* Determine the winner of the auction for item #i.
* Announce the winner and remove money from winner’s account.
*/
void resolveAuction (int itemNum)
{
string itemName = itemNames[itemNum];
int itemH = itemEndHours[itemNum];
int itemM = itemEndMinutes[itemNum];
int itemS = itemEndSeconds[itemNum];
double itemReserve = itemReservedPrice[itemNum];
CS333 40
The Structure of a C++ Program
CS333 41
The Structure of a C++ Program
readItems (argv[1]);
readBidders (argv[2]);
readBids (argv[3]);
CS333 42
The Structure of a C++ Program
.............................................
Not Shared
For example, in the Items module, the function readItem, which reads a single item, is only called from inside the function
readItems, which reads an entire array of items.
• Because readItem is only called by a function within its own module, it does not need to be listed in items.h.
.............................................
#ifndef ITEMS_H
#define ITEMS_H
#include <string>
//
// Items up for auction
//
CS333 43
The Structure of a C++ Program
// Names of items
extern std::string itemNames[];
/**
* Read all items from the indicated file
*/
void readItems (std::string fileName);
#endif
#include <iostream>
#include <fstream>
#include "items.h"
//
// Items up for auction
//
CS333 44
The Structure of a C++ Program
int nItems;
// Names of items
std::string itemNames[MaxItems];
/**
* Read one item from the indicated file
*/
void readItem (istream& in, int itemNum)
{
in >> itemReservedPrice[itemNum];
in >> itemEndHours[itemNum];
char c;
in >> c; // Ignore the ’:’ between hours and minutes
in >> itemEndMinutes[itemNum];
in >> c; // Ignore the ’:’ between minutes and seconds
in >> itemEndSeconds[itemNum];
CS333 45
The Structure of a C++ Program
/**
* Read all items from the indicated file
*/
void readItems (std::string fileName)
{
ifstream in (fileName.c_str());
in >> nItems;
for (int i = 0; i < nItems; ++i)
readItem (in, i);
}
.............................................
Bidders
#ifndef BIDDERS_H
#define BIDDERS_H
#include <string>
//
CS333 46
The Structure of a C++ Program
// Names of bidders
extern std::string bidderNames[];
// Account balances
extern double bidderBalances[];
/**
* Read all bidders from the indicated file
*/
void readBidders (std::string fileName);
/**
* Find the index of the bidder with the given name. If no such bidder exists,
* return nBidders.
*/
int findBidder (std::string name);
#endif
CS333 47
The Structure of a C++ Program
#include <string>
#include <fstream>
#include <iostream>
//
// Bidders Registered for auction
//
#include "bidders.h"
int nBidders;
// Names of bidders
std::string bidderNames[MaxBidders];
// Account balances
double bidderBalances[MaxBidders];
/**
* Read all bidders from the indicated file
*/
void readBidders (std::string fileName)
{
ifstream in (fileName.c_str());
in >> nBidders;
for (int i = 0; i < nBidders; ++i)
CS333 48
The Structure of a C++ Program
{
in >> bidderNames[i] >> bidderBalances[i];
}
}
/**
* Find the index of the bidder with the given name. If no such bidder exists,
* return nBidders.
*/
int findBidder (std::string name)
{
int found = nBidders;
for (int i = 0; i < nBidders && found == nBidders; ++i)
{
if (name == bidderNames[i])
found = i;
}
return found;
}
.............................................
Bids
#ifndef BIDS_H
#define BIDS_H
#include <string>
//
CS333 49
The Structure of a C++ Program
// Names of bidders
extern std::string bidBidder[];
// Bid Amounts
extern double bidAmounts[];
// Names of items
extern std::string bidItems[];
/**
* Read all bids from the indicated file
*/
void readBids (std::string fileName);
CS333 50
The Structure of a C++ Program
#endif
#include <string>
#include <fstream>
//
// Bids Received During Auction
//
#include "bids.h"
int nBids;
// Names of bidders
std::string bidBidder[MaxBids];
// Bid Amounts
double bidAmounts[MaxBids];
// Names of items
std::string bidItems[MaxBids];
CS333 51
The Structure of a C++ Program
int bidSeconds[MaxBids];
/**
* Read all bids from the indicated file
*/
void readBids (std::string fileName)
{
ifstream in (fileName.c_str());
in >> nBids;
for (int i = 0; i < nBids; ++i)
{
char c;
in >> bidBidder[i] >> bidAmounts[i];
in >> bidHours[i] >> c >> bidMinutes[i] >> c >> bidSeconds[i];
string word, line;
in >> word; // First word of itemName
getline (in, line); // rest of item name
bidItems[i] = word + line;
}
}
.............................................
Times
#ifndef TIMES_H
#define TIMES_H
CS333 52
The Structure of a C++ Program
#include <iostream>
/**
* Times in this program are represented by three integers: H, M, & S, representing
* the hours, minutes, and seconds, respecitvely.
*/
/**
* Print a time in the format HH:MM:SS (two digits each)
*/
void printTime (std::ostream& out, int h, int m, int s);
/**
* Compare to times. Return true iff time h1:m1:s1 is earlier than or equal to
* time time h2:m2:s2.
*
* Pre: Both times are normalized: s1, s2, m1, m2 are in the range 0..59,
* h1 and h2 are non-negative
* /
bool noLaterThan(int h1, int m1, int s1, int h2, int m2, int s2);
#endif // TIMES_H
#include "times.h"
/**
* Times in this program are represented by three integers: H, M, & S, representing
* the hours, minutes, and seconds, respecitvely.
CS333 53
The Structure of a C++ Program
*/
/**
* Print a time in the format HH:MM:SS (two digits each)
*/
void printTime (std::ostream& out, int h, int m, int s)
{
if (h < 10)
out << ’0’;
out << h << ’:’;
if (m < 10)
out << ’0’;
out << m << ’:’;
if (s < 10)
out << ’0’;
out << s;
}
/**
* Compare to times. Return true iff time h1:m1:s1 is earlier than or equal to
* time time h2:m2:s2.
*
* Pre: Both times are normalized: s1, s2, m1, m2 are in the range 0..59,
* h1 and h2 are non-negative
* /
bool noLaterThan(int h1, int m1, int s1, int h2, int m2, int s2)
{
// First check the hours
if (h1 > h2)
return false;
if (h1 < h2)
CS333 54
The Structure of a C++ Program
return true;
// If hours are the same, compare the minutes
if (m1 > m2)
return false;
if (m1 < m2)
return true;
// If hours and minutes are the same, compare the seconds
if (s1 > s2)
return false;
return true;
}
.............................................
#include <iostream>
#include <string>
#include "items.h"
#include "bidders.h"
#include "bids.h"
#include "times.h"
/**
* Determine the winner of the auction for item #i.
* Announce the winner and remove money from winner’s account.
*/
CS333 55
The Structure of a C++ Program
readItems (argv[1]);
readBidders (argv[2]);
readBids (argv[3]);
/**
* Determine the winner of the auction for item #i.
* Announce the winner and remove money from winner’s account.
*/
void resolveAuction (int itemNum)
{
string itemName = itemNames[itemNum];
int itemH = itemEndHours[itemNum];
CS333 56
The Structure of a C++ Program
CS333 57
The Structure of a C++ Program
.............................................
CS333 58