INTRO TO COMPUTER SCIENCE II
THE BIG THREE
CS162
Last time…
Passing objects Special pointers Const
Static
Destructors
Special class member function that is called automatically when the object is destroyed
“Opposite” of the constructor
Syntax
Must have same name as class
Must have “~” at the beginning
Can’t take arguments, can’t return anything
class Point { private:
int x;
int y; public:
Point(); Point(int,int); ~Point();
void move_left(int);
};
Point::~Point(){
} cout << "Destructor called!" << endl;
int main() { Point p1;
Point *p2 = new Point(4,10); //print P1, P2
delete p2;
return 0;
}
Destructors
Called when object goes out of scope When the function ends
When the program ends
A block containing local variables ends A delete operator is called
Automatically created if one is not supplied
But won’t handle dynamic memory!
Generally used to clean up dynamic memory usage, file I/O handles, database connections, etc.
}
class Point { private:
int x;
int y; public:
Point(); Point(int,int); ~Point();
}; void move_left(int); Point::~Point(){
cout << "Destructor called!" << endl;
}
int main() { Point p1;
Point *p2 = new Point(4,10); //print P1, P2
delete p2;
return 0;
Copy Constructor
Constructor that has one parameter that is of the same type as the class
Has to accept reference as parameter (normally const)
Allows for distinct copies, changes to one does not impact the other
class Point { private:
int x;
int y; public:
Point();
Point(int, int); ~Point(); Point(const Point&); void move_left(int);
};
Point::Point(const Point& old_obj) {
x = old_obj.x;
y = old_obj.y;
}
int main() {
Point p1(1,2);
Point p2 = p1;
return 0; }
Called automatically in three cases
When a class object is being declared and
initialized by another object of same type
Whenever an argument of the class type is “plugged in” for a call by value parameter
When a function returns a value of class type
Assignment Operator Overload
Predefined assignment operator returns a reference
Allows us to chain assignments together a = b = c First set “b=c” and return a reference to b. Now set “a=b”
Need to make sure the assignment operator returns something of the same type as its left hand side
Overloading assignment operator “=“ Must be a member of the class
If a new object doesn’t have to be created before copying, use assignment operator
If a new object has to be created before copying, use copy constructor
class Point { private:
int x;
int y; public:
Point();
Point(int, int);
~Point();
Point(const Point&);
Point& operator= (const Point&); void move_left(int);
};
Point& Point::operator= (const Point &p){ x = p.x;
y = p.y;
return *this;
}
int main() {
Point p1(1,2);
Point p2 = p1; //calls copy constructor Point p3;
p3 = p1; //calls overloaded assignment return 0;
}
Shallow Copy vs. Deep Copy
Shallow
Member-wise copy
Copy the contents of member variables from one object to another Default behavior when objects are copied or assigned
AKA, copy constructor and default assignment operators
OK for simple classes, but be careful when using classes with dynamically allocated
memory
Shallow copies of a pointer just copy the address of the pointer...doesn’t allocate memory or copy contents being pointed to!
Deep
Copy what each member variable is pointing to so that you get a separate but
identical copy
Has to be programmer specified
Shallow Copy vs. Deep Copy
The Big Three
If you implemented either a Destructor, a Copy Constructor, or a Copy Assignment Operator, you should ensure that all 3 are defined
If you needed one, you probably need all of them
This rule of thumb goes by a few names The Big Three
The Rule of Three
The Law of the Big Three
Line Class Example
#include "point.h"
class Line { private:
int n_points;
Point* points; public:
}
Line();
Line(int);
~Line();
Line(const Line&);
Line& operator= (const Line&);
Line Class - Constructors
Line::Line(){
n_points = 2;
points = new Point[n_points];
}
Line::Line(int n){ n_points = n;
points = new Point[n_points];
}
Line::Line(int x1, int y1, int x2, int y2){ n_points = 2;
points = new Point[n_points];
points[0].set_location(s_x,s_y);
points[1].set_location(e_x,e_y);
}
Line Class – Copy Constructors
Line::Line(const Line& old_line){ num_Points = old_line.num_Points;
Line::Line(const Line& old_line){ n_points = old_line.n_points; points = new Point[n_points]; for (int i=0; i