Top Banner
CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1
25

CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Jul 29, 2018

Download

Documents

vuongliem
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

CS32 - Week 1

Umut Oztok

June 24, 2016

Umut Oztok CS32 - Week 1

Page 2: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Administration

Email:[email protected]

Office hours: R – 09:30am-12:30pm (BH 4663)

Umut Oztok CS32 - Week 1

Page 3: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Constructor

Special member function to initialize an instance of aclass.

Called whenever a class object is created.

Has the same name as the class name.

No return type, so returns nothing.

Can have parameters. A constructor without anyparameter is called ”default constructor”.

A class can have more than one constructor. But theycannot have the exact same parameters/types.

Where to put its implementation?

Inside the class definition (i.e., header file)Outside the class definition (i.e., cpp file)

Umut Oztok CS32 - Week 1

Page 4: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Constructor

Syntax of Constructor

ClassName ();ClassName (Parameter p1, ..., Parameter pn);

class Circ {public:Circ(); //default constructorCirc(float x, float y, float r);...

private:float m_x, m_y, m_r;

};

Umut Oztok CS32 - Week 1

Page 5: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Destructor

Special member function to de-initialize an instance of aclass.

Called whenever a class object is destroyed.

No return type, so returns nothing.

Cannot have parameters.

A class can have at most one destructor.

Where to put its implementation?

Inside the class definition (i.e., header file)Outside the class definition (i.e., cpp file)

When do you need it?

Whenever you need to do any kind of maintenance beforethe object of the class is destroyed.Cleaning the memory allocated, closing a file, etc.

Umut Oztok CS32 - Week 1

Page 6: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Destructor

Syntax of Destructor

~ClassName ();

DON’T FORGET TILDE

class Circ {public:Circ(); //default constructorCirc(float x, float y, float r);

~Circ(); //destructor...

private:float m_x, m_y, m_r;

};

Umut Oztok CS32 - Week 1

Page 7: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Dynamic Memory Usage

Sometimes you won’t know size of some variables beforeruntime.

E.g., the memory needed may depend on user input.

In such cases, you need to dynamically allocate memory.

Also, whenever you are done with allocated memory, youshould deallocate (free) it. Otherwise, you have memoryleaks.

In C++, we use operators new and delete for dynamicmemory management.

In Java, there is automatic garbage collection, so you don’tneed to worry about freeing stuff.

Umut Oztok CS32 - Week 1

Page 8: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Dynamic Memory Allocation

Syntax of new Operator

pointer = new type;pointer = new type [number of elements];

int* foo;foo = new int [10];

Circ* a_ptr;a_ptr = new Circ(1,2,3);

Circ* b_ptr;b_ptr = new Circ[10];

Umut Oztok CS32 - Week 1

Page 9: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Dynamic Memory Deallocation

Syntax of delete Operator

delete pointer;delete[] pointer;

int* foo;foo = new int [10];delete[] foo;

Circ* a_ptr;a_ptr = new Circ(1,2,3);delete a_ptr;

Circ* b_ptr;b_ptr = new Circ[10];delete[] b_ptr;

Umut Oztok CS32 - Week 1

Page 10: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

A Digression

How do you check whether the new operation fails? Thatis, you try to allocate memory but OS cannot performyour request?

int* foo;foo = new int [10];if(foo == nullptr) { //failure }else { //success }

How do you check whether the delete operation fails?That is, you try to free memory but OS cannot performyour request?

You don’t :)

Umut Oztok CS32 - Week 1

Page 11: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Memory Allocation on Class Types

What is really happening when the following code is executed?

Circ* a_ptr;a_ptr = new Circ(1,2,3);delete a_ptr;

new

OS allocates memory.The constructor for class iscalled on this memory (if classhas one).

delete

The destructor for class iscalled (if class has one).OS frees the memory.

Umut Oztok CS32 - Week 1

Page 12: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Copy Constructor

Special member function to initialize an instance of aclass from an existing instance of the same class.

In fact, it is nothing but a special constructor with aparameter of the class type.

When is it being called?

Creating an instance using another instance.

Circ a(1,2,3); Circ b(a);

Assigning an uninitialized instance with another one.

Circ a(1,2,3); Circ b = a;

Passing an argument by value.

compute_area(Circ a);

Umut Oztok CS32 - Week 1

Page 13: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Copy Constructor

In fact C++ always provides you with a default copyconstructor, which basically copies all data members to beexactly the same.

Why do we need our own copy constructor?

Allocate dynamic memory.

Open system resources, e.g., opening a file.

Umut Oztok CS32 - Week 1

Page 14: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Copy Constructor

Steps to implement a copy constructor:

Determine how much memory is allocated in the existingvariable.

Allocate the same amount of memory for the newvariable.

Copy the content from the existing variable to new one.

Umut Oztok CS32 - Week 1

Page 15: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Copy Constructor

Syntax of Copy Constructor

ClassName (const ClassName&);

class Circ {public:...//copy constructorCirc(const Circ& oldVar) {m_x = oldVar.m_x;m_y = oldVar.m_y;m_r = oldVar.m_r;

}...

private:float m_x, m_y, m_r;

}

Umut Oztok CS32 - Week 1

Page 16: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Real use of Copy Constructor

class Circles {private:int size;Circ* circles;

public:...~Circles() {delete[] circles;} //destructorCircles(const Circles& src) { //copy constructorsize = src.size;circles = new Circ[size];for(int i=0; i<size; i++)circles[i] = src.circles[i];

}};

If you rely on default constructor, you do shallow copy. But,you need deep copy. So, you should write your own defaultconstructor.

Umut Oztok CS32 - Week 1

Page 17: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Assignment Operator

Assignment operator "=".

C++ always provides you with a default assignment operator.

However, default one copies all the data members. So,shallow copy again.

As for copy constructor, when you allocate dynamic memory,you need to override default assignment operator.

Umut Oztok CS32 - Week 1

Page 18: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Assignment Operator

Steps to override an assignment operator.

Free all dynamic memory used by target instance.

Re-allocate memory for target instance.

Copy all contents from source instance to target instance

Return a reference to target instance.

So, more work to do, compared to copy constructor. Thereason is target instance is already initialized, so need to takecare of allocated memory.

Umut Oztok CS32 - Week 1

Page 19: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Assignment Operator

Syntax of Assignment Operator

ClassName& operator= (const ClassName& src);

class Circ {public:...Circ& operator=(const Circ& src) { //assignment op.if(this != &src) {m_x = src.m_x;m_y = src.my;m_r = src.m_r;

}return (*this);

}private:float m_x, m_y, m_r;

};

Umut Oztok CS32 - Week 1

Page 20: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Real use of Assignment Operator

class Circles {private:int size;Circ* circles;

public:...//assignment operatorCircles& operator=(const Circles& src) {if(this != &src) {delete[] circles;size = src.size;circles = new Circ[size];for(int i=0; i<size; i++)circles[i] = src.circles[i];

}return (*this);

}};

Umut Oztok CS32 - Week 1

Page 21: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Copy Constructor vs. Assignment Operator

// #1main () {Circles a(4), b(5);b = a;

}

// #2main () {Circles c(4), d(c);c = d;

}

// #3Circles print() {Circles f(10);return f;

}

main () {Circles g = print();

}

Umut Oztok CS32 - Week 1

Page 22: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Assignment Operator Revisited

Current approach:

//assignment operatorCircles& operator=(const Circles& src) {if(this != &src) {delete[] circles;size = src.size;circles = new Circ[size];for(int i=0; i<size; i++)circles[i] = src.circles[i];

}return (*this);

}

That is, delete memory first, reallocate it, and then copy.

What is not good with that method?

Code duplication (destructor and copy constructor).

Umut Oztok CS32 - Week 1

Page 23: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Assignment Operator Revisited

How to prevent code duplication?

Copy-and-swap approach.

Use swap function.

void swap(Circles& c) {... //exchange size and c.size (integers)... //exchange circles and c.circles (pointers)

}

Umut Oztok CS32 - Week 1

Page 24: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Assignment Operator Revisited

//assignment operatorCircles& operator=(const Circles& src) {if(this != &src) {Circles temp(src);swap(temp);

}return (*this);

}

This provides you no code duplication:

Deallocation - automatically done by destructor.

Reallocation and copy - automatically done by copyconstructor.

Umut Oztok CS32 - Week 1

Page 25: CS32 - Week 1 - UCLAweb.cs.ucla.edu/~umut/cs32/slides/w1/w1.pdf · CS32 - Week 1 Umut Oztok June 24, 2016 Umut Oztok CS32 - Week 1

Slides

Slides are available at http://www.cs.ucla.edu/~umut/cs32

Umut Oztok CS32 - Week 1