Computer Science II for Majors Lecture 13 Friends and More Dr. - - PowerPoint PPT Presentation

computer science ii for majors
SMART_READER_LITE
LIVE PREVIEW

Computer Science II for Majors Lecture 13 Friends and More Dr. - - PowerPoint PPT Presentation

CMSC202 Computer Science II for Majors Lecture 13 Friends and More Dr. Katherine Gibson www.umbc.edu Last Class We Covered Linked Lists Traversal Creation Insertion Deletion 2 www.umbc.edu Any Questions from Last


slide-1
SLIDE 1

www.umbc.edu

CMSC202 Computer Science II for Majors

Lecture 13 –

Friends and More

  • Dr. Katherine Gibson
slide-2
SLIDE 2

www.umbc.edu

Last Class We Covered

  • Linked Lists

–Traversal –Creation –Insertion –Deletion

2

slide-3
SLIDE 3

www.umbc.edu

Any Questions from Last Time?

slide-4
SLIDE 4

www.umbc.edu

Today’s Objectives

  • To cover some miscellaneous topics:

–Friends –Destructors

  • Freeing memory in a structure

–Copy Constructors –Assignment Operators

4

slide-5
SLIDE 5

www.umbc.edu

Friend Functions and Classes

slide-6
SLIDE 6

www.umbc.edu

Why Have Friends?

  • Giving direct access to private variables is not

possible if the function is not a class method

  • But using accessors can be cumbersome,

especially for something like an overloaded insertion operator (<<)

  • Use a “friend” function to give direct access,

even though the function is not called on an

  • bject of that class

6

slide-7
SLIDE 7

www.umbc.edu

Friend Functions

  • Non-member functions that have

member-style access

  • Function is declared inside the class

– Will be public regardless of specifier

  • Designate using the friend keyword

friend void aFriendFunction();

7

slide-8
SLIDE 8

www.umbc.edu

Friend Classes

  • Classes can also be declared to be friends of

another class

class Milo { public: friend class Otis; }; class Otis { ... };

8

the Otis class now has access to all of the private members

  • f the Milo class
slide-9
SLIDE 9

www.umbc.edu

Forward Declarations

  • When one class references another in its

definition, we need a forward declaration

– Tell the compiler it exists, without defining it

  • In order to reference the Otis class before

it’s defined, we need something similar:

class Otis; – before the Milo class declaration

9

slide-10
SLIDE 10

www.umbc.edu

Using Friends

  • Why give access to private member variables?
  • Useful for testing functionality
  • Increased speed
  • Operator overloading
  • Enhances encapsulation

– A function being a friend is specified in the class

10

slide-11
SLIDE 11

www.umbc.edu

Destructors

slide-12
SLIDE 12

www.umbc.edu

Destructors

  • Destructors are the opposite of constructors
  • Used when delete() is called on an

instance of a user-created class

  • Compiler automatically provides one for you

– Does not take into account dynamic memory – If your class uses dynamic memory, you must write a better destructor to prevent memory leaks!

12

slide-13
SLIDE 13

www.umbc.edu

Destructor Example: Date

  • Let’s say we have a new member variable of
  • ur Date class called ‘m_next_holiday’

– Pointer to a string with name of the next holiday class Date { private: int m_month; int m_day; int m_year; string *m_next_holiday ; };

13

slide-14
SLIDE 14

www.umbc.edu

Destructor Example: Date

  • We will need to update the constructor

Date::Date (int m, int d, int y, string next_holiday) { SetMonth(m); SetDay(d); SetYear(y); m_next_holiday = new string; *m_next_holiday = next_holiday; }

14

What other changes do we need to make to a class when adding a new member variable?

slide-15
SLIDE 15

www.umbc.edu

Creating a Destructor

  • We also now need to create a destructor of
  • ur own:

~Date(); // our destructor

  • Destructors must have a tilde at the front
  • Similar to a constructor:

– Destructor has no return type – Same name as the class

15

slide-16
SLIDE 16

www.umbc.edu

Basic Destructor Definition

  • The destructor needs to free all of the

dynamically allocated memory

– Otherwise we will have memory leaks

  • Most basic version of a destructor

Date::~Date() { delete m_next_holiday; }

16

slide-17
SLIDE 17

www.umbc.edu

Security and Carefulness

Date::~Date() { delete m_next_holiday; }

  • This works, but it isn’t very secure for the data,

and it isn’t very careful with our pointers

– What if someone gets access to this memory later? – What if my code tries to access m_next_holiday after it’s been deleted?

17

slide-18
SLIDE 18

www.umbc.edu

Ideal Destructor Definition

  • Clears all information and sets pointers to NULL

Date::~Date() { // clear member variable info m_day = m_month = m_year = 0; *m_next_holiday = ""; // free and set pointers to NULL delete m_next_holiday; m_next_holiday = NULL; }

18

Why aren’t we using the mutator functions here?

slide-19
SLIDE 19

www.umbc.edu

Freeing Memory

  • Done using the delete() function

– Takes a pointer as an argument: delete(grades); delete(letters);

  • delete() does not work recursively

– For each individual allocation, there must be an individual call to free that allocated memory – Called in a sensible order

19

slide-20
SLIDE 20

www.umbc.edu

Freeing in Order

In what order would you free the nodes of this linked list?

20

A B C D E

slide-21
SLIDE 21

www.umbc.edu

Freeing in Order

In what order would you free the nodes of this binary tree?

21

A C B D F E H G

slide-22
SLIDE 22

www.umbc.edu

Copy Constructors and Assignment Operators

slide-23
SLIDE 23

www.umbc.edu

Copying Objects…

  • When does C++ make copies of objects?

– Pass by value – Return by value – Assignment – and… – New object initialized from existing object

slide-24
SLIDE 24

www.umbc.edu

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

slide-25
SLIDE 25

www.umbc.edu

Copy Constructor

  • Use when dynamic memory is allocated
  • Syntax:

– Prototype:

ClassName( const ClassName& obj );

– Implementation:

ClassName::ClassName( const ClassName& obj ) { // code to dynamically allocate data }

slide-26
SLIDE 26

www.umbc.edu

Why do we care?

  • Remember

– Assignment (by default) makes a direct copy of data members… – With dynamic memory – this would be copying pointers

Class

  • int *data1

string *data2 Object *data3 7 abc Foo bar Class

  • int *data1

string *data2 Object *data3

slide-27
SLIDE 27

www.umbc.edu

What do we want?

  • Each object should have own memory

allocated to members…

Class

  • int *data1

string *data2 Object *data3 7 abc Foo bar Class

  • int *data1

string *data2 Object *data3 7 abc Foo bar

slide-28
SLIDE 28

www.umbc.edu

Example

class Shoe {

public: Shoe( const Shoe& shoe ); private: int *m_size; string *m_brand;

}; Shoe::Shoe( const Shoe& shoe ) {

m_size = new int( *shoe.m_size ); m_brand = new string( *shoe.m_brand );

}

What’s going on here?

slide-29
SLIDE 29

www.umbc.edu

What else?

  • Assignment Operator

– Define if using dynamic memory

  • Syntax:

– Prototype:

ClassName& operator=( const ClassName& obj );

– Definition:

ClassName& ClassName::operator=( const ClassName& obj ) { // Deallocate existing memory, if necessary // Allocate new memory }

slide-30
SLIDE 30

www.umbc.edu

What’s Wrong With This?

Shoe& Shoe::operator=( 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 7 abc Shoe b

  • int *m_size

string *m_brand 4 edf What happened to the memory b was pointing to first???

slide-31
SLIDE 31

www.umbc.edu

What’s wrong with this?

void Shoe::operator=( 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??

slide-32
SLIDE 32

www.umbc.edu

Fixed

Shoe& Shoe::operator=( 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;

What’s this? this – a pointer to the current object

slide-33
SLIDE 33

www.umbc.edu

Self-Assignment

class RentalSystem { public: // Assume constructor, other methods… RentalSystem& operator=( const RentalSystem & rs ) private: Customer *m_customers; int m_nbrOfCustomers; }; RentalSystem& RentalSystem::operator=( const RentalSystem & rs ) { delete [] m_customers; m_customers = new Customer[rs.m_nbrOfCustomers]; for (int i = 0; i < rs.m_nbrOfCustomers; ++i) m_customers[i] = rs.m_customers[i]; return *this; }

What happens when you do the following? RentalSystem r; // Add customers… r = r;

slide-34
SLIDE 34

www.umbc.edu

Protect from Self-Assignment

RentalSystem& RentalSystem::operator=( const RentalSystem & rs ) { // If this is NOT the same object as rs if ( this != &rs ) { delete [] m_customers; m_customers = new Customer[rs.m_nbrOfCustomers]; for (int i = 0; i < rs.m_nbrOfCustomers; ++i) m_customers[i] = rs.m_customers[i]; } return *this; }

slide-35
SLIDE 35

www.umbc.edu

Announcements

  • Project 3 is out – get started now!

–Due Thursday, March 31st

  • Exam 2 is in 2 weeks

– Will focus heavily on:

  • Classes
  • Inheritance
  • Linked Lists
  • Dynamic Memory

35