{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

02-cpp - Spring 2009 CS216 Program and Data Representation...

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

View Full Document Right Arrow Icon
CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield C++
Background image of page 1

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

View Full Document Right Arrow Icon
Intro to C++
Background image of page 2
3 Why C++ and not Java? • It’s good to learn a second language • C++ is widely used –Can be more efficient –More control • C++ will let us “get under the hood” more –Data and program representation in memory –Memory allocation
Background image of page 3

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

View Full Document Right Arrow Icon
4 A brief history lesson • C was created in 1972 by Dennis Ritchie –Intended to be terse, quick to write, and efficient • C++ was created in 1985 by Bjarne Stroustrup –Added classes while being backwards compatible –Has pretty terrible syntax!
Background image of page 4
5 Hello World – Java vs. C++ // Java public class HelloWorld { public static void main(String [] args) { System.out.println("Hello World!"); } } // C++ #include <iostream> using namespace std; int main() { cout << “Hello World!” << endl; return 0; }
Background image of page 5

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

View Full Document Right Arrow Icon
6 Differences 1. main() 2. Preprocessor #include 3. using namespace std; 4. Output
Background image of page 6
7 main() • Not a part of any class –called a function • Must be global • Must have a return type of int –By convention, main returns 0
Background image of page 7

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

View Full Document Right Arrow Icon
8 Preprocessor • Examples –#include <iostream> // System file –#include “ListNode.h” // user-defined file • What this does –Compiler inserts the contents of the file in the place where the #include statement appears
Background image of page 8
9 C++ Compilation Process Overview 1. Preprocess source file handle #includes and any other # statements 2. Compile resulting file 3. Link the resulting files from Step 2 (more on this later…)
Background image of page 9

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

View Full Document Right Arrow Icon
10 using Directive • Similar to Java’s import –Allows the programmer to not have to type the full class name // C++ #include <iostream> using namespace std; int main() { cout << “Hello World!” << endl; return 0; } // C++ #include <iostream> int main() { std::cout << “Hello World!” << std::endl; return 0; }
Background image of page 10
11 I/O • Basic I/O // use iostream library #include <iostream> using namespace std; int main() { int x; cout << “Enter a value for x: “; cin >> x; }
Background image of page 11

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

View Full Document Right Arrow Icon
12 C++ Primitive Types • int –Can be 16, 32, 64 bits depending on the platform • float • double • char –C++ generally uses 8 bit ASCII encoding (more on this later) • bool
Background image of page 12
13 C++ Operators and Expressions • if statement –in C++ condition can be either int or bool –if ( x = 0 ) •This will NOT cause a compiler error in C++ –if ( i ) •This is valid
Background image of page 13

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

View Full Document Right Arrow Icon
14 Operators and Expressions • loops, same as Java –while, for, do while, break, continue
Background image of page 14
Functions
Background image of page 15

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

View Full Document Right Arrow Icon
16 Functions • Methods not member of a class #include <iostream> using namespace std; ret_type func_name(int a, int b, …) { <function body> } int main() {… z = func_name(x,y, …)… return 0;}
Background image of page 16
17 Declaring mutually recursive functions bool even (int x); bool odd (int x) { if ( x == 0 ) return false; else return even (x-1); } bool even (int x) { if ( x == 0 ) return true; else return odd (x-1); }
Background image of page 17

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

View Full Document Right Arrow Icon
18 Function Prototypes • C++ compiler process files top to bottom –order of appearance matters… without a function prototype • General form of a prototype ret_type func_name(int a, int b, …);
Background image of page 18
19 Example #include <iostream> using namespace std; int max(int a, int b); // prototype int main(){ int x=37; int y=52; cout << max(x,y) << endl; return 0; } // actual function implementation int max(int a, int b) { return (a>b) ? a : b;
Background image of page 19

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

View Full Document Right Arrow Icon
Image of page 20
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}