CS 171 Unit 2 Simple C++

CS 171 Unit 2 Simple C++ - CS 171 Unit 2 Simple C+ Furman...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 171 Unit 2 Simple C++ Furman Haddix Assistant Professor Minnesota State University, Mankato Spring 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Reading Assignment 2 Chapter 2 Text Errors: Practice! Exercises, p. 24 2 nd Exercise is impossible, unless you use 16 bits (2 bytes). Using 16 bits answer is 1111 1111 0100 0000 3 rd Exercise has incorrect answer. Correct answer is 1101 1011
Background image of page 2
Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input and Output Basic Functions from the C++ Standard Library Text, Chapter 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
The C++ Programming Language § A superset of C § C++ compilers can be used to compile C programs § There are some minor syntactic differences § Supports § Programmer Defined Types, in addition to built-in data types and predefined (C++ Library) data types § Templates § Overloading of functions and operators § Best to think of C++ as its own language
Background image of page 4
/* 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; } C++ Program Structure Comments Compiler Directives Using Directive Blocks of Code
Background image of page 5

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

View Full DocumentRight Arrow Icon
Program structure Comments preprocessor directives using directives Block of code { comments declarations statements }
Background image of page 6
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. (aka C++-style comments) § Delimited comments begin with /* and end with */ (aka C-style comments)
Background image of page 7

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

View Full DocumentRight Arrow Icon
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 system library file iostream . Another type of preprocessor directive is called a macro. Among other capabilities, macros facilitate conditional compilation.
Background image of page 8
using Directive The using directive instructs the compiler to use objects named in a specified namespace. Example: using namespace std ; // std is the name of the Standard C++ namespace . Why? This helps the compiler find the functions and operators that you are using, e.g., cout, <<
Background image of page 9

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

View Full DocumentRight Arrow Icon
Block of Code A block of code is defined by a set of curly braces {…} . Example:
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/28/2011 for the course CS 171 taught by Professor Ravi during the Spring '11 term at Minnesota.

Page1 / 53

CS 171 Unit 2 Simple C++ - CS 171 Unit 2 Simple C+ Furman...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online