3-4 - Problem Solving with Computers-II CS 24 March 4 2010...

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

View Full Document Right Arrow Icon
Problem Solving with Computers-II CS 24 March 4, 2010 1
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
Announcements Feedback on Midterm 2 Lab sections Extra credit for Programming project Design and Implementation of GetBestDocument 2
Background image of page 2
Recap Tree and BST ADT Recursive implementations 3
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
Lecture Plan C++ language details Copy constructor Templates Standard Template Library Vectors Tree and BST ADT Trace recursive deletion Iterative implementations Heap 4
Background image of page 4
C++ Constructor A class can have multiple constructors Choice made by matching types If a constructor is not defined by user, the compiler supplies a default one. A constructor does not return a value. 5
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
C++ Destructor Only a single destructor per class No parameters If a destructor is not defined by user, the compiler supplies a default one. It is called when the object goes out of scope. It is called when a delete command is issued for dynamically allocated objects (i.e., those created with a new command). 6
Background image of page 6
C++ Copy Constructor A copy constructor is a special constructor that takes as its argument a reference to an object of the same class and creates a new object that is a copy. Examples stack::stack(const StackType & stack2) {…} Implicitly invoked when Passing parameters by value Initializing an object StackType oneStack = anotherStack Returning objects as values 7
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
C++ Copy Constructors A copy constructor is a special constructor that takes as its argument a reference to an object of the same class and creates a new object that is a copy. By default, the compiler provides a copy constructor that performs a member-by-member copy from the original object to the one being created. This is called a shallow copy. Same effect can also be achieved by overloading the assignment operator. Other operators such as < , >, == cal also be overloaded. 8
Background image of page 8
Copy Constructors In many cases a shallow copy is not what we want. The problem is, any dynamic memory your class uses is stored by a pointer. The pointer is copied, not the actual data that it is pointing to. So, you have two objects pointing to the same dynamic memory (linked list, etc.). So any change to one effects both. If your class has a destructor, and it cleans up the dynamic memory, this is big trouble. 9
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
Copy Constructors Let us look at a typical Employee class implementation class Employee { public: Employee(char *name, int id); ~Employee(); char *getName(){return _name;} //Other Accessor methods private: int id; char *name; }; See any dynamic memory? Yep. . the name is stored as a char pointer.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 40

3-4 - Problem Solving with Computers-II CS 24 March 4 2010...

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

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