{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter2

# Chapter2 - Engineering Problem Solving with C Etter/Ingber...

This preview shows pages 1–4. Sign up to view the full content.

9/3/2009 Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs Prof. Xin Wang ([email protected]) Office: 235 Light Engineering building Class time: TuTh 3 :50 pm 5:10 pm Office Hour: Tu : 10 am- 12:00 am TA: Shekhar B Sastry ( Wednesday: 2:00pm ~ 4:00pm Friday: 2:00am ~ 4:00pm Office Place: Light Engineering Building Room 208 Simple C++ Programs ± Program Structure ± Constants and Variables ± C++ Operators ± Standard Input and Output ± Basic Functions from the C++ Standard Library 9/3/2009 Engineering Problem Solving with C++, Etter,Ingber Second Edition 2 9/3/2009 Engineering Problem Solving with C++ second edition, J. ingber 3 The C++ Programming Language § A superset of C § C++ compilers can be used to compile C programs § Supports § Programmer Defined Types § Templates § Overloading of functions and operators § Best to think of C++ as its own language PROGRAM STRUCTURE Comments Compiler Directives Using Directive Blocks of Code 9/3/2009 Engineering Problem Solving with C++, Etter,Ingber Second Edition 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/3/2009 Engineering Problem Solving with C++, Etter,Ingber Second Edition 5 /* Simple C++ Program */ /* This program computes the */ /* distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; } 9/3/2009 Engineering Problem Solving with C++ second edition, J. ingber 6 Program structure Comments preprocessor directives using directives Block of code { comments statements } 9/3/2009 Engineering Problem Solving with C++ second edition, J. ingber 7 Comments § Comments help people read programs, but are ignored by the compiler. § In C++ there are two types of comments. § Line comments begin with // and continue for the rest of the line. § Delimited comments begin with /* and end with */ 9/3/2009 Engineering Problem Solving with C++ second edition, J. ingber 8 Preprocessor Directives § Provide instructions to the compiler that are performed before the program is compiled. § Begin with a # § Example: #include <iostream> The #include directive instructs the compiler to include statements from the file iostream .
using Directive ± The using directive instructs the compiler to use files defined a specified namespace. ± Example: using namespace std ; std is the name of the Standard C++ namespace . 9/3/2009 Engineering Problem Solving with C++, Etter,Ingber Second Edition 9 Block of Code ± A block of code is defined by a set of curly braces {…}.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

Chapter2 - Engineering Problem Solving with C Etter/Ingber...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online