Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Crash Course For CS184 Sp08 Trevor Standley. C++ Is ~(C+Java)/2 Comprises a combination of both high level and low level language features Developed.

Similar presentations


Presentation on theme: "C++ Crash Course For CS184 Sp08 Trevor Standley. C++ Is ~(C+Java)/2 Comprises a combination of both high level and low level language features Developed."— Presentation transcript:

1 C++ Crash Course For CS184 Sp08 Trevor Standley

2 C++ Is ~(C+Java)/2 Comprises a combination of both high level and low level language features Developed in 1979 at Bell Labs as an enhancement to the C programming language Developed for backward compatibility with C First named, C with Classes Blazing fast, just like C. Direct access to RAM Classes, polymorphism, exception handling, strong typing, templates, operator overloading etc No memory management

3 Common Complaints C++ is not a high level language. Memory management… Yuck! I hear that its complicated, and messy. It was designed for compatibility with an ancient language, certainly the designers of modern languages could have done better without this restriction. I dont like C, why should I like C++? Who knows C++?

4 Hello World #include using namespace std; int main() { cout << Hello World << endl; return 0; }

5 Object vs Reference Consider the following: swap(int &a, int &b) { int t = a; a = b; b = t; }

6 Creating an Object C++ vector vec; vec.push_back(6); vector vec2 = vec; //deep copy vector *vec = new vector (); vec->push_back(6); vector *vec2 = vec; //shallow copy Java Vector vec = new Vector ();

7 classes

8 Inheritance

9 Operator Overloading

10 Default Class Pieces foo has a destructor ~foo(){} a copy constructor foo(const foo &f){x = f.x;} an assignment operator foo operator = (const foo &f) { x = f.x; } Possibly others

11 Templates

12 Memory Management Take a deep breath, you probably wont have to do any. C++ uses the Resource Acquisition Is Initialization paradigm Clean up after yourself.

13 Memory Management 2 Most memory is managed automatically when an object is initialized or goes out of scope The only exception is when the new operator is used. Standard libraries manage their own memory If you use new, use delete

14 The Syntax new returns a pointer to a location in memory where the requested object is int *x = new int[5]; // returns a pointer to memory where 5 ints are delete [] x; x = new vector2d(3,5) delete x;

15 Memory Management Example 1

16 Memory Management Example 2

17 The Standard Template Library C++ has multiple inheritance, which has little meaning in practice except that the STL is awesome! std::vector: include std::list: include std::string : include std::stringstream : include Etc.

18 More Information 2B 2B son_of_Java_and_C%2B%2B


Download ppt "C++ Crash Course For CS184 Sp08 Trevor Standley. C++ Is ~(C+Java)/2 Comprises a combination of both high level and low level language features Developed."

Similar presentations


Ads by Google