1 / 15

CS222

Week 13 - Thursday. CS222. Last time. What did we talk about last time? Function pointers Introduction to C++ Input and output Functions Overloadable Default parameters Pass by reference. Questions?. Project 6. More C++. The new keyword.

euclid
Download Presentation

CS222

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. Week 13 - Thursday CS222

  2. Last time • What did we talk about last time? • Function pointers • Introduction to C++ • Input and output • Functions • Overloadable • Default parameters • Pass by reference

  3. Questions?

  4. Project 6

  5. More C++

  6. The new keyword • When you want to dynamically allocate memory in C++, you use new (instead of malloc()) • No cast needed • It "feels" a lot like Java int* value = new int(); //make an int int* array = new int[100]; //array of ints Wombat* wombat = new Wombat(); //make a Wombat Wombat* zoo = new Wombat[100]; //makes 100 Wombats with the default constructor

  7. The deletekeyword • When you want to free dynamically allocated memory in C++, use delete (instead of free()) • If an array was allocated, you have to use delete[] int* value = new int(); //make an int delete value; Wombat* wombat = new Wombat(); delete wombat; Wombat* zoo = new Wombat[100]; delete[] zoo;//array delete needed

  8. C standard libraries • You can compile C code with C++ • Weird things can happen, but we aren't going to going into those subtle issues • However, you now know and love the standard C libraries • You can use them in C++ too • You just have to include different header files

  9. Structs in C++ • A struct in C++ is actually just a class where all the members are public • You can even put methods in a struct in C++ • Otherwise, it looks pretty similar • You don't have to use the struct keyword when declaring struct variables • Except in cases when it is needed for disambiguation

  10. Example • Here's a TreeNodestruct in C++ • Write a tree insertion with the following signature structTreeNode { intvalue; TreeNode* left; TreeNode* right; }; void insert(TreeNode* &root, int data);

  11. Lab 13

  12. Upcoming

  13. Next time… • Object orientation in C++ • virtual methods

  14. Reminders • Keep working on Project 6 • No class on Monday!

More Related