Chapter2

# Chapter2 - Chapter 2 Simple C Programs Simple C Programs...

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

9/6/2011 Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Chapter 2 Simple C++ Programs Prof. Xin Wang ([email protected]) Office: 235 Light Engineering building Class time: TuTh 11 :20 am 12:50 pm Office Hour: Tu : 10:00 am- 11:00 am Tu : 3:50 am- 4:50 pm Simple C++ Programs ± Program Structure ± Constants and Variables ± C++ Operators ± Standard Input and Output ± Basic Functions from the C++ Standard Library 9/6/2011 Engineering Problem Solving with C++, Etter,Ingber Second Edition 2 9/6/2011 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/6/2011 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/6/2011 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/6/2011 Engineering Problem Solving with C++ second edition, J. ingber 6 Program structure Comments preprocessor directives using directives Block of code { comments statements } 9/6/2011 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/6/2011 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 defining a specified namespace. ± Example: using namespace std ; std is the name of the Standard C++ namespace . 9/6/2011 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 {…}. ± Example: int main() { //Block defines body of main function double x1(1), x2(4), side1; side1 = x2 – x1; cout << side1 << endl; return 0; // main()returns int value of 0 } //end definition of main ± Every C++ problem solution contains exactly one function named main().

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.

## This note was uploaded on 09/21/2011 for the course ESE 224 taught by Professor Wang during the Fall '08 term at SUNY Stony Brook.

### Page1 / 12

Chapter2 - Chapter 2 Simple C Programs Simple C Programs...

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

View Full Document
Ask a homework question - tutors are online