1 / 16

C++ class basics and Debugging

C++ class basics and Debugging. Jungseock Joo. C++ Class Basics. Member data and functions. Like struct Control over accessibility to its members. ( ie , public, private, friend, etc). Code example #1 – class Circle. Declaration (interface)

duy
Download Presentation

C++ class basics and Debugging

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++ class basics and Debugging JungseockJoo

  2. C++ Class Basics • Member data and functions. • Like struct • Control over accessibility to its members. (ie, public, private, friend, etc)

  3. Code example #1 – class Circle • Declaration (interface) • Identify its members and their forms (type, arguments, …)

  4. Code example #1 – class Circle • Definition (implementation) • Specify the actions that the member functions must carry out.

  5. Code example #1 – class Circle • Instantiation and usage

  6. Compilation dependency

  7. Constructor • Function to be called upon creation of an object. • Initializes values of the member variables. • Default implicit constructors don’t. • NOTE : Always check to initialize variables. Some compilers do it for you, but many other don’t. Then you have undetermined garbage values, painful to debug.

  8. Destructor • Function to be called upon destroying of an object. • Releases dynamic resources allocated(memory). • Default implicit destructors don’t. • If we don’t, “Memory Leak”, painful to debug.

  9. Copy Constructor • A constructor that copies from an existing instance of the same class given as an argument. • String new_string ( old_string ); • Creates a new instance, new_string, and initialize the values of member variables from old_string. • “strcpy” used to copy the contents, not the pointer. • Implicit copy constructors perform pointer-copy: (m_text = other.m_text), may cause a problem.

  10. Assignment Operator = • Assign the values of an instance (rhs) to the other (lhs) • Just like “a = 5;”, you can define a customized assignment operator for your class. • String old_string ( “aaa” ); String new_string = old_string; // handled by copy constructor • String new_string2; new_string2 = old_string; // assignment operator - Again, implicit assignment operator performs pointer-copy.

  11. Assignment Operator = • Version 1

  12. Assignment Operator = • Version 1 • What’s wrong? • Self-assignment • string1 = string1;

  13. Assignment Operator = • Version 2 • Add a protection against self-assignment

  14. Assignment Operator = • Version 2 • Add a protection against self-assignment • Not exception-safe

  15. Assignment Operator = • Version 3 • Swap-and-copy

  16. Debugging in Visual Studio • demo

More Related