Please send questions to
st10@humboldt.edu .
CS 131 - Week 12, Lecture 1
(but Thursday, November 11 is Veteran's Day holiday)
C++ program:
* a C++ program consists of a function named
main and whatever other functions and/or
classes and/or other beasties are used in
that program
* when this collection of functions is compiled,
linked, loaded, and turned into an executable
file -- then when this executable file/executable
program is run, the main function's actions are
done; (and it may call other functions, which
may in turn call other functions, and so on...)
...but the action starts at main;
* according to Wikipedia: (looked up in 2007)
the following 3 headers are OK for this main
function:
int main(void)
int main()
int main(int argc, char* argv[])
* what is that int?
* for CS 131, your main functions should return
one of the named constants which you
happen to get by #include'ing iostream
EXIT_SUCCESS
EXIT_FAILURE
* typically, in here, the basic main skeleton
(so far) would be:
// save this in a file with suffix .cpp
// whose name is (by convention) the name you
// want for the final executable file
/*-----
signature: main: void -> int
purpose: either:
<describe the program being written> OR
testing program for the function/class blah
examples: <describe, in prose, the effect of running
this main>
by:
last modified:
-----*/
#include <iostream>
#include <string>
#include "blah.h"
using namespace std;
int main()
{
// do stuff
return EXIT_SUCCESS;
}
* a few words about #include
* # means it is a pre-processor directive;
#include really copies in the contents of
of the file specified,
and then compilation proceeds...
* for C++ standard libraries, in ANSI-standard
C++, you put just the name of the library
in angle brackets:
#include <iostream>
#include <string>
#include <cmath>
* for functions or classes you have written
and want
to use in another function, and that
are in the same directory as this function
(or in a standard path of some sort...)
write the .h file's name in double quotes
#include "boa.h"
#include "tank_volume.h"
* we are, by default, using the standard
namespace -- so, by the C++ ANSI standard,
we will conclude our list of #include's
with:
using namespace std;
* let's write a main using the main_template
that'll try out sum_array from Friday;
I'm going to call this:
try_sum_array.cpp
* how do you turn a collection of C++ functions (one of which has the
name main) into an executable program?
... you compile and link them together.
(you must put ALL the functions involved in the program in the
compile-and-link command!!)
g++ funct1.cpp funct2.cpp ... main_funct.cpp -o program_name
or
g++ funct1.o funct2.o ... main_funct.o -o program_name
(if you JUST want to compile a function, main or not, JUST type
g++ -c funct.cpp
...it produces a .o file)
* C++ STYLE (course style too):
name the file with the main function the name you'd
like the program to have
g++ program_name.cpp funct_used.cpp -o program_name
^ main is in here
* void functions
some functions just don't return anything;
they have a side-effect, and they're done;
* like a void method (like our modifier methods),
these have a return type of void
void my_void_function()
* like a void method, they don't need
a return statement
* (but you CAN have a return with no expression
following it --
return;
...although we usually won't)