DOC PREVIEW
WUSTL CSE 332S - C++_programs

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C++ Program Structure (and tools)Writing a C++ ProgramWhat Goes Into a C++ Program?A Very Simple C++ ProgramWhat is #include <iostream> ?What is using namespace std; ?What is int main (int, char*[]) { ... } ?What’s cout << “hello, world!” << endl; ?What about return 0; ?A Slightly Bigger C++ Programint argc, char * argv[]for (int i = 0; i < argc; ++i){cout << argv[i] << endl;}Lifecycle of a C++ ProgramDevelopment Environment StudioCSE 332: C++ program structure and development environmentC++ Program Structure (and tools)Today we’ll talk generally about C++ development (plus a few platform specifics) •We’ll develop, submit, and grade code in Windows•It’s also helpful to become familiar with Linux –E.g., on shell.cec.wustl.edu•For example, running code through two different compilers can catch a lot more “easy to make” errorsCSE 332: C++ program structure and development environmentWriting a C++ ProgramC++ source files(ASCII text) .cppProgrammer(you)emacseditorC++ header files(ASCII text) .h1 source file = 1 compilation unitMakefile(ASCII text)Also: .C .cxx .ccAlso: .H .hxx .hppreadme(ASCII text)EclipseVisual StudioCSE 332: C++ program structure and development environmentWhat Goes Into a C++ Program?•Declarations: data types, function signatures, classes–Allows the compiler to check for type safety, correct syntax–Usually kept in “header” (.h) files–Included as needed by other files (to keep compiler happy)class Simple { typedef unsigned int UINT32; public: Simple (int i); int usage (char * program_name); void print_i (); private: struct Point2D { int i_; double x_;}; double y_; };•Definitions: static variable initialization, function implementation–The part that turns into an executable program –Usually kept in “source” (.cpp) filesvoid Simple::print_i () {cout << “i_ is ” << i_ << endl;} •Directives: tell compiler (or precompiler) to do something–More on this laterCSE 332: C++ program structure and development environmentA Very Simple C++ Program#include <iostream> // precompiler directiveusing namespace std; // compiler directive// definition of function named “main”int main (int, char *[]){ cout << “hello, world!” << endl; return 0;}CSE 332: C++ program structure and development environmentWhat is #include <iostream> ?•#include tells the precompiler to include a file•Usually, we include header files–Contain declarations of structs, classes, functions•Sometimes we include template definitions–Varies from compiler to compiler–Advanced topic we’ll cover later in the semester•<iostream> is the C++ label for a standard header file for input and output streamsCSE 332: C++ program structure and development environmentWhat is using namespace std; ?•The using directive tells the compiler to include code from libraries that have separate namespaces–Similar idea to “packages” in other languages•C++ provides a namespace for its standard library–Called the “standard namespace” (written as std)–cout, cin, and cerr standard iostreams, and much more•Namespaces reduce collisions between symbols–Rely on the :: scoping operator to match symbols to them–If another library with namespace mylib defined cout we could say std::cout vs. mylib::cout •Can also apply using more selectively:–E.g., just using std::coutCSE 332: C++ program structure and development environmentWhat is int main (int, char*[]) { ... } ?•Defines the main function of any C++ program•Who calls main?–The runtime environment, specifically a function often called something like crt0 or crtexe•What about the stuff in parentheses?–A list of types of the input arguments to function main–With the function name, makes up its signature–Since this version of main ignores any inputs, we leave off names of the input variables, and only give their types•What about the stuff in braces?–It’s the body of function main, its definitionCSE 332: C++ program structure and development environmentWhat’s cout << “hello, world!” << endl; ?•Uses the standard output iostream, named cout–For standard input, use cin–For standard error, use cerr•<< is an operator for inserting into the stream–A member o perator of the ostream class–Returns a reference to stream on which its called–Can be applied repeatedly to references left-to-right •“hello, world!” is a C-style string–A 14-postion character array terminated by ‘\0’•endl is an iostream manipulator–Ends the line, by inserting end-of-line character(s)–Also flushes the streamCSE 332: C++ program structure and development environment What about return 0; ?•The main function should return an integer–By convention it should return 0 for success–And a non-zero value to indicate failure•The program should not exit any other way–Letting an exception propagate uncaught–Dividing by zero–Dereferencing a null pointer–Accessing memory not owned by the program•Indexing an array “out of range” can do this•Dereferencing a “stray” pointer can do thisCSE 332: C++ program structure and development environmentA Slightly Bigger C++ Program#include <iostream>using namespace std;int main (int argc, char * argv[]){ for (int i = 0; i < argc; ++i) { cout << argv[i] << endl; } return 0;}CSE 332: C++ program structure and development environmentint argc, char * argv[]•A way to affect the program’s behavior–Carry parameters with which program was called–Passed as parameters to main from crt0–Passed by value (we’ll discuss what that means)•argc–An integer with the number of parameters (>=1)•argv–An array of pointers to C-style character strings–Its array-length is the value stored in argc–The name of the program is kept in argv[0]CSE 332: C++ program structure and development environmentfor (int i = 0; i < argc; ++i)•Standard (basic) C++ for loop syntax–Initialization statement done once at start of loop–Test expression done before running each time–Expression to increment after running each time•int i = 0–Declares integer i (scope is the loop itself)–Initializes i to hold value 0 (not an assignment!)•i < argc–Tests whether or not we’re still inside the array!–Reading/writing memory we don’t own can crash the program (if we’re really lucky!)•++i –increments the array position (why prefix?)CSE


View Full Document

WUSTL CSE 332S - C++_programs

Download C++_programs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C++_programs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C++_programs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?