02-cpp

02-cpp - CS216 Program and Data Representation University...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield C++ Intro to C++ 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 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! 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; } 6 Differences 1. main() 2. Preprocessor • #include 3. using namespace std; 4. Output 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 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 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…) 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; } 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; } 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 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 14 Operators and Expressions • loops, same as Java –while, for, do while, break, continue Functions 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;} 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); }...
View Full Document

This note was uploaded on 09/11/2009 for the course CS 216 taught by Professor Bloomfield during the Spring '08 term at UVA.

Page1 / 119

02-cpp - CS216 Program and Data Representation University...

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

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