Classes with Pointer Data Members (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
1 Classes with Pointer Data Members (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230.
Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
Debugging: Catching Bugs ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
1 Array, Pointer and Reference ( III ) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures.
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS230 Lectures Series.
1 Constructors and Destructors Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Review of C++ Programming Part II Sheng-Fang Huang.
11 Introduction to Object Oriented Programming (Continued) Cats II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
C++ Review (3) Structs, Classes, Data Abstraction.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 CSC241: Object Oriented Programming Lecture No 22.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
1 C++ Classes (I) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
More C++ Features True object initialisation
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 CS161 Introduction to Computer Science Topic #16.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
1 CSC241: Object Oriented Programming Lecture No 17.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
CS32 Discussion Section 1B Week 2 TA: Hao Yu (Cody)
1 Ugly Realities The Dark Side of C++ Chapter 12.
Overloading C++ supports the concept of overloading Two main types
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Overloading Operator MySting Example
14.4 Copy Constructors.
Memberwise Assignment / Initialization
group work #hifiTeam
Chapter 15 Pointers, Dynamic Data, and Reference Types
Popping Items Off a Stack Lesson xx
9-10 Classes: A Deeper Look.
C++ Pointers and Strings
Passing Arguments and The Big 5
Pointers and References
CS 144 Advanced C++ Programming March 21 Class Meeting
C++ Pointers and Strings
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
Presentation transcript:

Classes with Pointer Data Members (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series

Warming up… char *str_dup_new(const char *str) { return ( strcpy(new char [strlen(str)+1], str) ); } char *strcpy( char *strDestination, const char *strSource ); Example: char string[80]; strcpy( string, "Hello world from " ); size_t strlen( const char *string ); Example: void main( void ) { char buffer[61] = "How long am I?"; int len = strlen( buffer ); cout << buff << “ is “ << len << “characters long\n"; } Output: 'How long am I?' is 14 characters long

Let’s program … Task: construct a class “CPerson” –Name? –Address? –Phone?

1 st version class CPerson{ public: CPerson(); CPerson(const char* n, const char*a, const char* p); void set_name(const char *n) { m_name = str_dup_new(n); }; void set_address(const char *a) { m_address = str_dup_new(a); }; void set_phone(const char *p) { m_phone = str_dup_new(p); }; const char* get_name() const { return m_name; }; const char* get_address() const { return m_address; }; const char* get_phone() const { return m_phone; }; private: char *m_name; char *m_address; char *m_phone; }; Is it a good version?

CPerson::CPerson() { m_name = NULL; m_address = NULL; m_phone = NULL; } CPerson::CPerson(const char* n, const char* a, const char *p) { set_name(n); set_address(a); set_phone(p); }

What to learn today? Why they are troublesome? Troubles and Solutions –destructor –Assignment operation –The this pointer –The copy constructor: initialization vs. assignment

What are special? The class contains pointer data members We have to take care of the memory those pointer data members pointing to –i.e., using dynamic memory management –allocate memory for these pointer data members when instantiate each object –deallocate memory back when destroy objects Where does the trouble come? –initialization –Assignment –copying Special care has to be taken!

A destructor is needed CPerson::~CPerson() { delete [] m_name; delete [] m_address; delete [] m_phone; } void main(){ CPerson kk(“Karel”, “Evanston”, “ ”); CPerson *bb = new CPerson(“Bill Clinton”, “DC”, “ ”); cout << kk.get_name()<< kk.get_address() << kk.get_phone() << endl; cout get_name() get_address() get_phone(); delete bb; }

So, this is the 2 nd version class CPerson{ public: CPerson(); CPerson(const char* n, const char* a, const char* p); ~CPerson(); void set_name(const char *n) { m_name = str_dup_new(n); }; void set_address(const char *a) { m_address = str_dup_new(a); }; void set_phone(const char *p) { m_phone = str_dup_new(p); }; const char* get_name() const { return m_name; }; const char* get_address() const { return m_address; }; const char* get_phone() const { return m_phone; }; private: char *m_name; char *m_address; char *m_phone; };

Trouble 1: Call-by-Value void do_nothing(CPerson p) { // I actually do nothing here // but, unfortunately, I am in trouble! } m_name m_address m_phone mike object p Call-by-value m_name m_address m_phone mike object p After destruction of p m_name m_address m_phone mike object p Before function call ? ? ? void main() { CPerson mike(“Mike”, “Evanston”,”1111”); do_nothing(mike); }

Wild Pointer The deallocated memory will likely become occupied during subsequent memory allocations But the pointer members are still pointing to those memory locations Therefore, these pointer members become “wild” We can not track these pointer any more Wild pointers are very hard to trace and debug! So, try to avoid them when coding

Trouble 2: Return-by-Value CPerson test() { CPerson mike(“mike”,”IL”,”1111”); return mike; } m_name m_address m_phone mike object tmp Return-by-value m_name m_address m_phone tmp mike After destruction of mike m_name m_address m_phone mike object tmp Before return ? ? ? void main() { CPerson t; t = test(); }

3 rd Version: Copy Constructor class CPerson{ public: CPerson(); CPerson(const char*n, const char* a, const char* p); CPerson(const CPerson& person); // copy constructor ~CPerson(); void set_name(const char *n) { m_name = str_dup_new(n); }; void set_address(const char *a) { m_address = str_dup_new(a); }; void set_phone(const char *p) { m_phone = str_dup_new(p); }; const char* get_name() const { return m_name; }; const char* get_address() const { return m_address; }; const char* get_phone() const { return m_phone; }; private: char *m_name; char *m_address; char *m_phone; };

3 rd Version: Copy Constructor CPerson::CPerson(const CPerson & person) { set_name(person.get_name()); set_address(person.get_address()); set_phone(person.get_phone()); }

Trouble still…wild pointer void print_person(const CPerson &p) { CPerson tmp; tmp = p; // assignment operator cout << tmp.get_name() << tmp.get_address() << tmp.get_phone() << endl; } m_name m_address m_phone object p object tmp After the assignment m_name m_address m_phone object p object tmp After destruction of tmp m_name m_address m_phone object p object tmp Before the assignment ? ? ?

Solution to Assignment object tmp Before the assignment ? ? ? m_name m_address m_phone object p After the assignment m_name m_address m_phone object p object tmp m_name m_address m_phone After destruction of tmp m_name m_address m_phone object p object tmp m_name m_address m_phone

4th Version: Assignment class CPerson{ public: CPerson(); CPerson(const char*n, const char* a, const char* p); CPerson(const CPerson& person); // copy constructor ~CPerson(); void set_name(const char *n) { m_name = str_dup_new(n); }; void set_address(const char *a) { m_address = str_dup_new(a); }; void set_phone(const char *p) { m_phone = str_dup_new(p); }; const char* get_name() const { return m_name; }; const char* get_address() const { return m_address; }; const char* get_phone() const { return m_phone; }; voidassign(const CPerson &p); // assignment function private: char *m_name; char *m_address; char *m_phone; };

4 th version: Assignment void CPerson::assign(const CPerson &p) { // delete our own previously used memory delete [] m_name; delete [] m_address; delete [] m_phone; // now copy the new data passed m_name = str_dup_new(p.get_name()); m_address = str_dup_new(p.get_address()); m_phone = str_dup_new(p.get_phone()); } void print_person(const CPerson &p) { CPerson tmp; tmp.assign(p); cout << tmp.get_name() << tmp.get_address() << tmp.get_phone() << endl; }

What if I want = ? void main() { CPerson mike(“Michael”, “Evanston”, “ ”); CPerson tmp; tmp = mike; tmp.print_person(); }

5th Version: Elegant class CPerson{ public: CPerson(); CPerson(const char*n, const char* a, const char* p); CPerson(const CPerson &person); // copy constructor ~CPerson(); void set_name(const char *n) { m_name = str_dup_new(n); }; void set_address(const char *a) { m_address = str_dup_new(a); }; void set_phone(const char *p) { m_phone = str_dup_new(p); }; const char* get_name() const { return m_name; }; const char* get_address() const { return m_address; }; const char* get_phone() const { return m_phone; }; voidassign(const CPerson &p); voidoperator=(const CPerson &p); // overloading = private: char *m_name; char *m_address; char *m_phone; };

5 th version (cont.) void CPerson::operator=(const CPerson &p) { // delete our own previously used memory delete [] m_name; delete [] m_address; delete [] m_phone; // now copy the new data passed m_name = str_dup_new(p.get_name()); m_address = str_dup_new(p.get_address()); m_phone = str_dup_new(p.get_phone()); }

Potential trouble… self-destruction void trouble(const CPerson &p) { p = p; // auto-assignment } When an object is assigned to itself, i.e., auto-assignment, a trouble occurs: the allocated strings of the receiving objects are first released, but this also leads to the release of the strings of the right-hand side variable, which is called “self-destruction”.

6th Version: this class CPerson{ public: CPerson(); CPerson(const char*n, const char* a, const char* p); CPerson(const CPerson &person); // copy constructor ~CPerson(); void set_name(const char *n) { m_name = str_dup_new(n); }; void set_address(const char *a) { m_address = str_dup_new(a); }; void set_phone(const char *p) { m_phone = str_dup_new(p); }; const char* get_name() const { return m_name; }; const char* get_address() const { return m_address; }; const char* get_phone() const { return m_phone; }; voidassign(const CPerson &p); voidoperator=(const CPerson &p); // overloading = private: char *m_name; char *m_address; char *m_phone; };

6 th version: using this void CPerson::operator=(const CPerson &p) { if (this != &p){ // delete our own previously used memory delete [] m_name; delete [] m_address; delete [] m_phone; // now copy the new data passed m_name = str_dup_new(p.get_name()); m_address = str_dup_new(p.get_address()); m_phone = str_dup_new(p.get_phone()); }

What if I want … void main() { CPerson mike(“Michael”, “Evanston”, “ ”); CPerson a, b; a = b = mike; a.print_person(); }

7th Version: Cascading class CPerson{ public: CPerson(); CPerson(const char*n, const char* a, const char* p); CPerson(const CPerson &person); // copy constructor ~CPerson(); void set_name(const char *n) { m_name = str_dup_new(n); }; void set_address(const char *a) { m_address = str_dup_new(a); }; void set_phone(const char *p) { m_phone = str_dup_new(p); }; const char* get_name() const { return m_name; }; const char* get_address() const { return m_address; }; const char* get_phone() const { return m_phone; }; void assign(const CPerson &p); const CPerson& operator=(const CPerson &p); // enable cascading private: char *m_name; char *m_address; char *m_phone; };

7 th version (cont.) const CPerson& CPerson::operator=(const CPerson &p) { if (this != &p){ delete [] m_name; delete [] m_address; delete [] m_phone; m_name = str_dup_new(p.get_name()); m_address = str_dup_new(p.get_address()); m_phone = str_dup_new(p.get_phone()); } return (*this); // enable cascading }

Summary Classes with pointer data members are special, and need more attentions! How to handle it? –Destructor –Copy constructor –Assignment Assignment function Overloading = Using this –Avoiding self-destruction –Enabling cascading