CMSC 202 - PowerPoint PPT Presentation

About This Presentation
Title:

CMSC 202

Description:

CMSC 202. Lesson 14. Copy and Assignment. Warmup. Write the ... class CellPhone. public: CellPhone( int number, const string& name ); private: int* m_number; ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 18
Provided by: DanaWo3
Category:

less

Transcript and Presenter's Notes

Title: CMSC 202


1
CMSC 202
  • Lesson 14
  • Copy and Assignment

2
Warmup
  • Write the constructor for the following class
  • class CellPhone
  • public
  • CellPhone( int number,
  • const string name )
  • private
  • int m_number
  • string m_name

3
Announcements
  • Midterms
  • Grades on Blackboard
  • Pick up in lab this week
  • Office Hours
  • ITE 352, again

4
Copying Objects
  • When do we make copies?
  • Pass by value
  • Return by value
  • Assignment
  • New object initialized with existing object
  • Havent seen this.yet

5
Copy Constructor
  • Initialize an object based on an existing object
  • Examples
  • int a 7
  • int b(a) // Copy constructor
  • Shoe shoeOfMJ( Nike, 16 )
  • Shoe myShoe( shoeOfMJ ) // Copy

6
Copy Constructor
  • Use when dynamic memory is allocated
  • Syntax
  • Prototype
  • ClassName( const ClassName obj )
  • Implementation
  • ClassNameClassName( const ClassName obj )
  • // code to dynamically allocate data

7
Why do we care?
  • Remember
  • Assignment (by default) makes a direct copy of
    data members
  • With dynamic memory this would be copying
    pointers

7
Class ------------- int data1 string
data2 Object data3
Class ------------- int data1 string
data2 Object data3
abc
Foo bar
8
What do we want?
  • Each object should have own memory allocated to
    members

7
Class ------------- int data1 string
data2 Object data3
7
Class ------------- int data1 string
data2 Object data3
abc
abc
Foo bar
Foo bar
9
Example
  • class Shoe
  • public
  • Shoe( const Shoe shoe )
  • private
  • int m_size
  • string m_brand
  • ShoeShoe( const Shoe shoe )
  • m_size new int( shoe.m_size )
  • m_brand new string( shoe.m_brand )

Whats going on here?
10
What else?
  • Assignment Operator
  • Define if using dynamic memory
  • Syntax
  • Prototype
  • ClassName operator( const ClassName obj )
  • Definition
  • ClassName ClassNameoperator( const ClassName
    obj )
  • // Deallocate existing memory, if necessary
  • // Allocate new memory

11
Whats wrong with this?
7
  • Shoe Shoeoperator(
  • const Shoe shoe )
  • m_size
  • new int(shoe.m_size)
  • m_brand
  • new string(shoe.m_brand)
  • // In main()
  • Shoe a(7, abc)
  • Shoe b(4, edf)
  • b a

Shoe a ------------- int m_size string m_brand
abc
Shoe b ------------- int m_size string m_brand
4
edf
What happened to the memory b was pointing to
first???
12
Whats wrong with this?
  • void Shoeoperator( const Shoe shoe )
  • m_size shoe.m_size
  • m_brand shoe.m_brand
  • Shoe a(7, abc)
  • Shoe b(4, edf)
  • Shoe c(9, ghi)
  • c b a

How does the c b work, when b a returns
nothing??
13
Fixed
  • Shoe Shoeoperator( const Shoe shoe )
  • m_size shoe.m_size
  • m_brand shoe.m_brand
  • return this
  • Shoe a(7, abc)
  • Shoe b(4, edf)
  • Shoe c(9, ghi)
  • c b a

Whats this? this a pointer to the current
object
14
Self-Assignment
  • class RentalSystem
  • public
  • // Assume constructor, other methods
  • RentalSystem operator(
  • const RentalSystem rs )
  • private
  • Customer m_customers
  • int m_nbrOfCustomers
  • RentalSystem RentalSystemoperator(
  • const RentalSystem rs )
  • delete m_customers
  • m_customers new Customerrs.m_nbrOfCustomers
  • for (int i 0 i lt rs.m_nbrOfCustomers i)
  • m_customersi rs.m_customersi

What happens when you do the following? RentalSys
tem r // Add customers r r
15
Protect from Self-assignment
  • RentalSystem RentalSystemoperator(
  • const RentalSystem rs )
  • // If this is NOT the same object as rs
  • if ( this ! rs )
  • delete m_customers
  • m_customers new Customerrs.m_nbrOfCustomers
  • for (int i 0 i lt rs.m_nbrOfCustomers i)
  • m_customersi rs.m_customersi
  • return this

16
Practice
  • Implement copy constructor and operator
  • class Stapler
  • public
  • _______________ // copy constructor
  • _______________ // operator
  • private
  • int m_nbrStaples

17
Challenge
  • Create a BoxOfStaplers class
  • Implement copy constructor
  • Implement operator
  • Hint
  • Take a look at the lecture notes for the
    SmartArray class!
Write a Comment
User Comments (0)
About PowerShow.com