1 / 18

C++ Crash Course

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 in 1979 at Bell Labs as an enhancement to the C programming language Developed for backward compatibility with C

rbowes
Download Presentation

C++ Crash Course

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 it’s 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 don’t like C, why should I like C++?” • “Who knows C++?”

  4. Hello World • #include <iostream> • 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<int> vec; • vec.push_back(6); • vector<int> vec2 = vec; //deep copy • vector<int> *vec = new vector<int>(); • vec->push_back(6); • vector<int> *vec2 = vec; //shallow copy • Java • Vector<integer> vec = new Vector<integer>();

  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 won’t 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 <vector> • std::list: include <list> • std::string : include <string> • std::stringstream : include <sstream> • Etc.

  18. More Information • http://www.cplusplus.com/ • http://en.wikipedia.org/wiki/C%2B%2B • http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B

More Related