CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Guide To UNIX Using Linux Third Edition
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Creating your first C++ program
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Fundamental Programming: Fundamental Programming Introduction to C++
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
C++ Memory Overview 4 major memory segments Key differences from Java
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSE 232: C++ Programming in Visual Studio Graphical Development Environments for C++ Eclipse –Widely available open-source debugging environment Available.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Navigating the C++ Development Environment
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
CSE1222: Lecture 1The Ohio State University1. Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Fundamental Programming Fundamental Programming Introduction to Functions.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Programming Fundamentals Enumerations and Functions.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
A Sample Program #include using namespace std; int main(void) { cout
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Functions A bit of review (things we’ve covered so far)
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 CS 3370 – C++ Functions.
A bit of C programming Lecture 3 Uli Raich.
Completing the Problem-Solving Process
Motivation and Overview
Session 1 - Introduction
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
When your program crashes
Subject:Object oriented programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

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 // 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 ? #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 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 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 (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 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 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 open studio/lab days, reviews before the midterm and final, and the day of the 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 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