SlideShare une entreprise Scribd logo
1  sur  15
CSE 332: C++ program structure and development environment
C++ 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” errors
CSE 332: C++ program structure and development environment
Writing a C++ Program
C++ source files
(ASCII text) .cpp
Programmer
(you)
emacs
editor
C++ header files
(ASCII text) .h
1 source file
=
1 compilation unit
Makefile
(ASCII text)
Also: .C .cxx .cc
Also: .H .hxx .hpp
readme
(ASCII text)
Eclipse
Visual Studio
CSE 332: C++ program structure and development environment
What 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) files
void Simple::print_i ()
{
cout << “i_ is ” << i_ << endl;
}
• Directives: tell compiler (or precompiler) to do something
– More on this later
CSE 332: C++ program structure and development environment
A Very Simple C++ Program
#include <iostream> // precompiler directive
using 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 environment
What 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 streams
CSE 332: C++ program structure and development environment
What 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::cout
CSE 332: C++ program structure and development environment
What 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 definition
CSE 332: C++ program structure and development environment
What’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 operator 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 stream
CSE 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 this
CSE 332: C++ program structure and development environment
A 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 environment
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]
CSE 332: C++ program structure and development environment
for (int i = 0; i < argc; ++i)
• Standard 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 332: C++ program structure and development environment
{cout << argv[i] << endl;}
• Body of the for loop
• I strongly prefer to use braces with for, if,
while, etc., even w/ single-statement body
– Avoids maintenance errors when
adding/modifying code
– Ensures semantics/indentation say same thing
• argv[i]
– An example of array indexing
– Specifies ith position from start of argv
CSE 332: C++ program structure and development environment
Lifecycle of a C++ Program
C++
source code
Makefile
Programmer
(you)
object code
(binary, one per compilation unit) .o
make
“make” utility
xterm
console/terminal/window
Runtime/utility
libraries
(binary) .lib .a .dll .so
gcc, etc.
compiler
link
linker
E-mail
executable
program
Eclipse
debugger
precompiler
compiler
link
turnin/checkin
An “IDE”
WebCAT
Visual Studio
window
compile
CSE 332: C++ program structure and development environment
Development Environment Studio
• 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 reviews before midterm/final, and midterm itself
• In the studios, please work in groups of 2 or 3
– Exercises are posted on the course web page
– Record your answers to the exercises, and e-mail your
answers to the course account when you’re done
– We’ll migrate throughout the studio to answer questions
• Use studio time to develop skills and understanding
– A good chance to explore ideas you can use for the labs
– Exams will test understanding of the studio material
– You’re encouraged to try variations beyond the exercises

Contenu connexe

Tendances

Kotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language designKotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language designAndrey Breslav
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015Fernando Hamasaki de Amorim
 
HipHop Virtual Machine
HipHop Virtual MachineHipHop Virtual Machine
HipHop Virtual MachineRadu Murzea
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalKent Ohashi
 
POCO C++ Libraries Intro and Overview
POCO C++ Libraries Intro and OverviewPOCO C++ Libraries Intro and Overview
POCO C++ Libraries Intro and OverviewGünter Obiltschnig
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]RootedCON
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...Sang Don Kim
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORSRapidValue
 
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)ngotogenome
 
Manage software dependencies with ioc and aop
Manage software dependencies with ioc and aopManage software dependencies with ioc and aop
Manage software dependencies with ioc and aopStefano Leli
 
HHVM and Hack: A quick introduction
HHVM and Hack: A quick introductionHHVM and Hack: A quick introduction
HHVM and Hack: A quick introductionKuan Yen Heng
 
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 pramode_ce
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScriptDenis Voituron
 
MidwestPHP Symfony2 Internals
MidwestPHP Symfony2 InternalsMidwestPHP Symfony2 Internals
MidwestPHP Symfony2 InternalsRaul Fraile
 
Introducing redis
Introducing redisIntroducing redis
Introducing redisNuno Caneco
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607Kevin Hazzard
 

Tendances (20)

Kotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language designKotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language design
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
 
HipHop Virtual Machine
HipHop Virtual MachineHipHop Virtual Machine
HipHop Virtual Machine
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of Pedestal
 
POCO C++ Libraries Intro and Overview
POCO C++ Libraries Intro and OverviewPOCO C++ Libraries Intro and Overview
POCO C++ Libraries Intro and Overview
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORS
 
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
 
Manage software dependencies with ioc and aop
Manage software dependencies with ioc and aopManage software dependencies with ioc and aop
Manage software dependencies with ioc and aop
 
HHVM and Hack: A quick introduction
HHVM and Hack: A quick introductionHHVM and Hack: A quick introduction
HHVM and Hack: A quick introduction
 
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017
 
Network programming
Network programmingNetwork programming
Network programming
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScript
 
MidwestPHP Symfony2 Internals
MidwestPHP Symfony2 InternalsMidwestPHP Symfony2 Internals
MidwestPHP Symfony2 Internals
 
Introducing redis
Introducing redisIntroducing redis
Introducing redis
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
 
High Performance tDiary
High Performance tDiaryHigh Performance tDiary
High Performance tDiary
 
x86
x86x86
x86
 

En vedette

Raspberry pi intro workshop
Raspberry pi intro workshopRaspberry pi intro workshop
Raspberry pi intro workshoprasen58
 
Robert Cormier
Robert CormierRobert Cormier
Robert Cormierrasen58
 
Clean water act
Clean water actClean water act
Clean water actrasen58
 
Linear Algebra: Application to Chemistry
Linear Algebra: Application to ChemistryLinear Algebra: Application to Chemistry
Linear Algebra: Application to Chemistryrasen58
 
Seminar Presentation on raspberry pi
Seminar Presentation on raspberry piSeminar Presentation on raspberry pi
Seminar Presentation on raspberry piGeorgekutty Francis
 
A seminar report on Raspberry Pi
A seminar report on Raspberry PiA seminar report on Raspberry Pi
A seminar report on Raspberry Pinipunmaster
 
Raspberry Pi Presentation
Raspberry Pi PresentationRaspberry Pi Presentation
Raspberry Pi PresentationGeekizer
 

En vedette (8)

Raspberry pi intro workshop
Raspberry pi intro workshopRaspberry pi intro workshop
Raspberry pi intro workshop
 
Robert Cormier
Robert CormierRobert Cormier
Robert Cormier
 
Clean water act
Clean water actClean water act
Clean water act
 
Linear Algebra: Application to Chemistry
Linear Algebra: Application to ChemistryLinear Algebra: Application to Chemistry
Linear Algebra: Application to Chemistry
 
Seminar Presentation on raspberry pi
Seminar Presentation on raspberry piSeminar Presentation on raspberry pi
Seminar Presentation on raspberry pi
 
A seminar report on Raspberry Pi
A seminar report on Raspberry PiA seminar report on Raspberry Pi
A seminar report on Raspberry Pi
 
Raspberry Pi Presentation
Raspberry Pi PresentationRaspberry Pi Presentation
Raspberry Pi Presentation
 
Raspberry pi
Raspberry pi Raspberry pi
Raspberry pi
 

Similaire à C++ Program Structure and Development Tools

C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptJayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptJayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptEPORI
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptyp02
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptInfotech27
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptTeacherOnat
 
Object oriented programming 7 first steps in oop using c++
Object oriented programming 7 first steps in oop using  c++Object oriented programming 7 first steps in oop using  c++
Object oriented programming 7 first steps in oop using c++Vaibhav Khanna
 
C++ programming intro
C++ programming introC++ programming intro
C++ programming intromarklaloo
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itPushkarNiroula1
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 

Similaire à C++ Program Structure and Development Tools (20)

C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
Cpp-c++.pptx
Cpp-c++.pptxCpp-c++.pptx
Cpp-c++.pptx
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
 
Intro to .NET and Core C#
Intro to .NET and Core C#Intro to .NET and Core C#
Intro to .NET and Core C#
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Object oriented programming 7 first steps in oop using c++
Object oriented programming 7 first steps in oop using  c++Object oriented programming 7 first steps in oop using  c++
Object oriented programming 7 first steps in oop using c++
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
C++ programming intro
C++ programming introC++ programming intro
C++ programming intro
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 

Dernier

Zoo_championship_Wrestling (action/comedy sample)
Zoo_championship_Wrestling (action/comedy sample)Zoo_championship_Wrestling (action/comedy sample)
Zoo_championship_Wrestling (action/comedy sample)DavonBrooks
 
怎么办理美国UC Davis毕业证加州大学戴维斯分校学位证书一手渠道
怎么办理美国UC Davis毕业证加州大学戴维斯分校学位证书一手渠道怎么办理美国UC Davis毕业证加州大学戴维斯分校学位证书一手渠道
怎么办理美国UC Davis毕业证加州大学戴维斯分校学位证书一手渠道7283h7lh
 
The Hooper Talk (drama/comedy board sample)
The Hooper Talk (drama/comedy board sample)The Hooper Talk (drama/comedy board sample)
The Hooper Talk (drama/comedy board sample)DavonBrooks
 
Rückenfigur ... back figure in paintings.ppsx
Rückenfigur ... back figure in paintings.ppsxRückenfigur ... back figure in paintings.ppsx
Rückenfigur ... back figure in paintings.ppsxguimera
 
Escort Service in Ajman +971509530047 UAE
Escort Service in Ajman +971509530047 UAEEscort Service in Ajman +971509530047 UAE
Escort Service in Ajman +971509530047 UAEvecevep119
 
Escort Service in Al Jaddaf +971509530047 UAE
Escort Service in Al Jaddaf +971509530047 UAEEscort Service in Al Jaddaf +971509530047 UAE
Escort Service in Al Jaddaf +971509530047 UAEvecevep119
 
layered-cardboard-sculptures-miika-nyyssonen.pdf
layered-cardboard-sculptures-miika-nyyssonen.pdflayered-cardboard-sculptures-miika-nyyssonen.pdf
layered-cardboard-sculptures-miika-nyyssonen.pdfbaroquemodernist
 
Mapeh Music QUARTER FOUR Grade nine haha
Mapeh Music QUARTER FOUR Grade nine hahaMapeh Music QUARTER FOUR Grade nine haha
Mapeh Music QUARTER FOUR Grade nine hahaJoshuaAcido2
 
Olympia Cafe, Restaurants-cafes, Albuquerque, NM
Olympia Cafe, Restaurants-cafes, Albuquerque, NMOlympia Cafe, Restaurants-cafes, Albuquerque, NM
Olympia Cafe, Restaurants-cafes, Albuquerque, NMroute66connected
 
Roadrunner Motel, Motel/Residence. Tucumcari, NM
Roadrunner Motel, Motel/Residence. Tucumcari, NMRoadrunner Motel, Motel/Residence. Tucumcari, NM
Roadrunner Motel, Motel/Residence. Tucumcari, NMroute66connected
 
Escort Service in Al Qusais +971509530047 UAE
Escort Service in Al Qusais +971509530047 UAEEscort Service in Al Qusais +971509530047 UAE
Escort Service in Al Qusais +971509530047 UAEvecevep119
 
FORTH QUARTER MAPEH7-PHILIPPINE FESTIVALS.pptx
FORTH QUARTER MAPEH7-PHILIPPINE FESTIVALS.pptxFORTH QUARTER MAPEH7-PHILIPPINE FESTIVALS.pptx
FORTH QUARTER MAPEH7-PHILIPPINE FESTIVALS.pptxJadeTamme
 
New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)DavonBrooks
 
Subway Stand OFF storyboard by Raquel Acosta
Subway Stand OFF storyboard by Raquel AcostaSubway Stand OFF storyboard by Raquel Acosta
Subway Stand OFF storyboard by Raquel Acostaracosta58
 
Hiway Motel, Motel/Residence, Albuquerque NM
Hiway Motel, Motel/Residence, Albuquerque NMHiway Motel, Motel/Residence, Albuquerque NM
Hiway Motel, Motel/Residence, Albuquerque NMroute66connected
 
Escort Service in Al Nahda +971509530047 UAE
Escort Service in Al Nahda +971509530047 UAEEscort Service in Al Nahda +971509530047 UAE
Escort Service in Al Nahda +971509530047 UAEvecevep119
 
SLIDESHARE. ART OF THE ROMANTIC PERIOD/ROMANTICISM Art
SLIDESHARE. ART OF THE ROMANTIC PERIOD/ROMANTICISM ArtSLIDESHARE. ART OF THE ROMANTIC PERIOD/ROMANTICISM Art
SLIDESHARE. ART OF THE ROMANTIC PERIOD/ROMANTICISM ArtChum26
 
Teepee Curios, Curio shop, Tucumcari, NM
Teepee Curios, Curio shop, Tucumcari, NMTeepee Curios, Curio shop, Tucumcari, NM
Teepee Curios, Curio shop, Tucumcari, NMroute66connected
 
Cat & Art100 A collection of cat paintings
Cat & Art100 A collection of cat paintingsCat & Art100 A collection of cat paintings
Cat & Art100 A collection of cat paintingssandamichaela *
 

Dernier (20)

Zoo_championship_Wrestling (action/comedy sample)
Zoo_championship_Wrestling (action/comedy sample)Zoo_championship_Wrestling (action/comedy sample)
Zoo_championship_Wrestling (action/comedy sample)
 
怎么办理美国UC Davis毕业证加州大学戴维斯分校学位证书一手渠道
怎么办理美国UC Davis毕业证加州大学戴维斯分校学位证书一手渠道怎么办理美国UC Davis毕业证加州大学戴维斯分校学位证书一手渠道
怎么办理美国UC Davis毕业证加州大学戴维斯分校学位证书一手渠道
 
School :)
School                                 :)School                                 :)
School :)
 
The Hooper Talk (drama/comedy board sample)
The Hooper Talk (drama/comedy board sample)The Hooper Talk (drama/comedy board sample)
The Hooper Talk (drama/comedy board sample)
 
Rückenfigur ... back figure in paintings.ppsx
Rückenfigur ... back figure in paintings.ppsxRückenfigur ... back figure in paintings.ppsx
Rückenfigur ... back figure in paintings.ppsx
 
Escort Service in Ajman +971509530047 UAE
Escort Service in Ajman +971509530047 UAEEscort Service in Ajman +971509530047 UAE
Escort Service in Ajman +971509530047 UAE
 
Escort Service in Al Jaddaf +971509530047 UAE
Escort Service in Al Jaddaf +971509530047 UAEEscort Service in Al Jaddaf +971509530047 UAE
Escort Service in Al Jaddaf +971509530047 UAE
 
layered-cardboard-sculptures-miika-nyyssonen.pdf
layered-cardboard-sculptures-miika-nyyssonen.pdflayered-cardboard-sculptures-miika-nyyssonen.pdf
layered-cardboard-sculptures-miika-nyyssonen.pdf
 
Mapeh Music QUARTER FOUR Grade nine haha
Mapeh Music QUARTER FOUR Grade nine hahaMapeh Music QUARTER FOUR Grade nine haha
Mapeh Music QUARTER FOUR Grade nine haha
 
Olympia Cafe, Restaurants-cafes, Albuquerque, NM
Olympia Cafe, Restaurants-cafes, Albuquerque, NMOlympia Cafe, Restaurants-cafes, Albuquerque, NM
Olympia Cafe, Restaurants-cafes, Albuquerque, NM
 
Roadrunner Motel, Motel/Residence. Tucumcari, NM
Roadrunner Motel, Motel/Residence. Tucumcari, NMRoadrunner Motel, Motel/Residence. Tucumcari, NM
Roadrunner Motel, Motel/Residence. Tucumcari, NM
 
Escort Service in Al Qusais +971509530047 UAE
Escort Service in Al Qusais +971509530047 UAEEscort Service in Al Qusais +971509530047 UAE
Escort Service in Al Qusais +971509530047 UAE
 
FORTH QUARTER MAPEH7-PHILIPPINE FESTIVALS.pptx
FORTH QUARTER MAPEH7-PHILIPPINE FESTIVALS.pptxFORTH QUARTER MAPEH7-PHILIPPINE FESTIVALS.pptx
FORTH QUARTER MAPEH7-PHILIPPINE FESTIVALS.pptx
 
New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)
 
Subway Stand OFF storyboard by Raquel Acosta
Subway Stand OFF storyboard by Raquel AcostaSubway Stand OFF storyboard by Raquel Acosta
Subway Stand OFF storyboard by Raquel Acosta
 
Hiway Motel, Motel/Residence, Albuquerque NM
Hiway Motel, Motel/Residence, Albuquerque NMHiway Motel, Motel/Residence, Albuquerque NM
Hiway Motel, Motel/Residence, Albuquerque NM
 
Escort Service in Al Nahda +971509530047 UAE
Escort Service in Al Nahda +971509530047 UAEEscort Service in Al Nahda +971509530047 UAE
Escort Service in Al Nahda +971509530047 UAE
 
SLIDESHARE. ART OF THE ROMANTIC PERIOD/ROMANTICISM Art
SLIDESHARE. ART OF THE ROMANTIC PERIOD/ROMANTICISM ArtSLIDESHARE. ART OF THE ROMANTIC PERIOD/ROMANTICISM Art
SLIDESHARE. ART OF THE ROMANTIC PERIOD/ROMANTICISM Art
 
Teepee Curios, Curio shop, Tucumcari, NM
Teepee Curios, Curio shop, Tucumcari, NMTeepee Curios, Curio shop, Tucumcari, NM
Teepee Curios, Curio shop, Tucumcari, NM
 
Cat & Art100 A collection of cat paintings
Cat & Art100 A collection of cat paintingsCat & Art100 A collection of cat paintings
Cat & Art100 A collection of cat paintings
 

C++ Program Structure and Development Tools

  • 1. CSE 332: C++ program structure and development environment C++ 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” errors
  • 2. CSE 332: C++ program structure and development environment Writing a C++ Program C++ source files (ASCII text) .cpp Programmer (you) emacs editor C++ header files (ASCII text) .h 1 source file = 1 compilation unit Makefile (ASCII text) Also: .C .cxx .cc Also: .H .hxx .hpp readme (ASCII text) Eclipse Visual Studio
  • 3. CSE 332: C++ program structure and development environment What 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) files void Simple::print_i () { cout << “i_ is ” << i_ << endl; } • Directives: tell compiler (or precompiler) to do something – More on this later
  • 4. CSE 332: C++ program structure and development environment A Very Simple C++ Program #include <iostream> // precompiler directive using namespace std; // compiler directive // definition of function named “main” int main (int, char *[]) { cout << “hello, world!” << endl; return 0; }
  • 5. CSE 332: C++ program structure and development environment What 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 streams
  • 6. CSE 332: C++ program structure and development environment What 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::cout
  • 7. CSE 332: C++ program structure and development environment What 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 definition
  • 8. CSE 332: C++ program structure and development environment What’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 operator 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 stream
  • 9. CSE 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 this
  • 10. CSE 332: C++ program structure and development environment A 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; }
  • 11. CSE 332: C++ program structure and development environment 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]
  • 12. CSE 332: C++ program structure and development environment for (int i = 0; i < argc; ++i) • Standard 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?)
  • 13. CSE 332: C++ program structure and development environment {cout << argv[i] << endl;} • Body of the for loop • I strongly prefer to use braces with for, if, while, etc., even w/ single-statement body – Avoids maintenance errors when adding/modifying code – Ensures semantics/indentation say same thing • argv[i] – An example of array indexing – Specifies ith position from start of argv
  • 14. CSE 332: C++ program structure and development environment Lifecycle of a C++ Program C++ source code Makefile Programmer (you) object code (binary, one per compilation unit) .o make “make” utility xterm console/terminal/window Runtime/utility libraries (binary) .lib .a .dll .so gcc, etc. compiler link linker E-mail executable program Eclipse debugger precompiler compiler link turnin/checkin An “IDE” WebCAT Visual Studio window compile
  • 15. CSE 332: C++ program structure and development environment Development Environment Studio • 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 reviews before midterm/final, and midterm itself • In the studios, please work in groups of 2 or 3 – Exercises are posted on the course web page – Record your answers to the exercises, and e-mail your answers to the course account when you’re done – We’ll migrate throughout the studio to answer questions • Use studio time to develop skills and understanding – A good chance to explore ideas you can use for the labs – Exams will test understanding of the studio material – You’re encouraged to try variations beyond the exercises