CSE332S Object-Oriented Programming in C++ (Lecture 2)
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” errors
Extra credit on Lab 1: compile the cpp program in Linux.
Writing C++
Makefile ASCII text
C++ course files, ASCII text, end it with .cpp
C++ header files, ASCII text, end it with .h
readme, ASCII text (show what program does)
Parts of a C++ Program
Declarations
data types, function signatures, class declarations
- This allows the compiler to check for type safety, correct syntax, and other errors
- Usually kept in header files (e.g., .h)
- Included as needed by other files (to make compiler happy)
// my_class.h
class Simple {
public:
Simple (int i);
void print_i();
private:
int i_;
}
typedef unsigned int UNIT32;
int usage (char * program_name);
struct Point2D {
double x_;
double y_;
};Definitions
Static variables initialization, function implementation
- The part that turns into an executable program
- Usually kept in source files (e.g., .cpp)
// my_class.cpp
#include "my_class.h"
Simple::Simple (int i) : i_(i) {}
void Simple::print_i() {
std::cout << i_ << std::endl;
}Directives
tell complier or preprocessor what to do
A Very Simple C++ Program
#include <iostream> // precompiler directive
using namespace std; // compiler directive
// definition of main function
int main(int, char *[]) {
cout << "Hello, World!" << endl;
return 0;
}What is #include <iostream>?
#includetells the precompiler to include a file- Usually, we include header files that:
- Contain declarations of structs, classes, functions
- Sometimes contain template definitions (template not included in this course)
- Implementation varies from compiler to compiler (advanced topic covered later)
<iostream>is the C++ standard header file for input/output streams
What is using namespace std;?
- The
usingdirective 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) - Contains
cout,cin,cerrstandard iostreams, and much more
- Called the “standard namespace” (written as
- Namespaces reduce collisions between symbols
- Rely on the
::scoping operator to match symbols to them - If another library with namespace
mylibdefinedcoutwe could saystd::coutvs.mylib::cout
- Rely on the
- Can also apply
usingmore selectively:- E.g., just
using std::cout
- E.g., just
What is int main(int, char *[]) { ... }?
-
Defines the main function of any C++ program, it is the entry point of the program
-
Who calls main?
- The runtime environment, specifically a function often called something like
crt0orcrtexe
- The runtime environment, specifically a function often called something like
-
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
mainignores any inputs, we leave off names of the input variables, and only give their types
- A list of types of the input arguments to function
-
What about the stuff in braces?
- It’s the body of function
main, its definition
- It’s the body of function
What is cout << "Hello, World!" << endl;?
- Uses the standard output iostream, named
cout- For standard input, use
cin - For standard error, use
cerr
- For standard input, use
<<is an operator for inserting into the stream- A member operator of the
ostreamclass - Returns a reference to stream on which it’s called
- Can be applied repeatedly to references left-to-right
- A member operator of the
"hello, world!"is a C-style string- A 14-position character array terminated by
'\0'
- A 14-position character array terminated by
endlis an iostream manipulator- Ends the line by inserting end-of-line character(s)
- Also flushes the stream
What about return 0;?
- The
mainfunction must return an integer value - By convention:
- Return
0to indicate successful execution - Return non-zero value to indicate failure
- Return
- The program should exit gracefully through
main’s return - Other ways the program can terminate abnormally:
- Uncaught exceptions propagating out of
main - Division by zero
- Dereferencing null pointers
- Accessing memory not owned by the program
- Array index out of bounds
- Dereferencing invalid/“stray” pointers
- Uncaught exceptions propagating out of
A slightly more complex 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;
}int 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]
What is for (int i = 0; i < argc; i++) { ... }?
Standard C++ for loop syntax:
- Consists of 3 parts:
- Initialization statement (executed once at start)
- Test expression (checked before each iteration)
- Increment expression (executed after each iteration)
Let’s break down each part:
int i = 0:
- Declares integer variable
i(scoped to the loop) - Initializes
ito 0 (initialization, not assignment)
i < argc:
- Tests if we’re within array bounds
- Critical for memory safety - accessing outside array can crash program
++i:
- Increments array position counter
- Uses prefix increment operator
Lifecycle of a C++ Program
Start from the makefile
- The makefile is a text file that tells the compiler how to build the program, it activates the ‘make’ utility to build the program
- The make file turnin/checkin to the WebCAT E-mail
- The makefile complies the gcc compiler to compile the cpp file
- The makefile links the object files to create the executable file
The cpp file
- The cpp file is a text file that contains the source code of the program
- The cpp file is compiled into an object file by the gcc compiler to combined with the link produced by the makefile with the runtime/util library
Finally, the object file is linked with the runtime/util library to create the executable program and ready to debug with Eclipse or Visual Studio.
Development Environment Studio
Course Format
- We’ll follow a similar format most days in the course:
- Around 30 minutes of lecture and discussion
- Then about 60 minutes of studio time
- Except for:
- Open studio/lab days
- Reviews before the midterm and final
- The day of the midterm itself
Studio Guidelines
- Work in groups of 2 or 3
- Exercises are posted on the course web page
- Record your answers and email them to the course account
- Instructors will circulate to answer questions
Purpose of Studios
- Develop skills and understanding
- Explore ideas you can use for labs
- Prepare for exams which test studio material
- Encouraged to try variations beyond exercises