INTRO TO COMPUTER SCIENCE II
IN-CLASS ACTIVITIES
CS162
In-class Worksheet #1
Fill in the blanks in your breakout rooms for ~7 minutes We’ll go over the answers after
Submit text responses on Canvas
In-class Worksheet #2
Fill out the sections of the worksheet for the next 20 minutes Include prototype, what each function is supposed to do, when it is
called, and what the default behavior is for: Constructor
Copy Constructor
Destructor
Assignment Operator Overload
Submit picture, pdf, anything with the information on Canvas
Function
Constructor
Copy Constructor
Destructor
Assignment Operator Overload
In-class Worksheet #2
Prototype
ClassName(); ClassName(w/params);
Job
When is it called?
Default behavior if not defined
Reminders
Function
Constructor
Copy Constructor
Destructor
Assignment Operator Overload
In-class Worksheet #2
Prototype
ClassName(); ClassName(w/params);
Job
Builds the object
When is it called?
Default is called when object is declared with no parameters and no “=” sign.
Nondefault is called if parameters are given.
Default behavior if not defined
Will declare all variables with garbage values, will not set up pointers
Reminders
If any constructor is defined then the compiler will not automatically provide one, even if a default constructor is not defined.
Function
Constructor
In-class Worksheet #2
Prototype
ClassName(); ClassName(w/params);
Job
Builds the object
When is it called?
Default is called when object is declared with no parameters and no “=” sign.
Nondefault is called if parameters are given.
Default behavior if not defined
Will declare all variables with garbage values, will not set up pointers
Reminders
If any constructor is defined then the compiler will not automatically provide one, even if a default constructor is not defined.
Copy Constructor
Destructor
Assignment Operator Overload
ClassName(const ClassName &);
Copies the contents of the passed in object to the destination object.
1. Pass by value
2. Return value
3. When initializing an object with this constructor
Shallow copy, will only copy over the values stored in each variable
Works with objects that can be assumed to be uninitialized.
Function
Constructor
Copy Constructor
Destructor
Assignment Operator Overload
In-class Worksheet #2
Prototype
ClassName(); ClassName(w/params);
ClassName(const ClassName &);
~ClassName();
Job
Builds the object
Destroys the object
When is it called?
Default is called when object is declared with no parameters and no “=” sign.
Nondefault is called if parameters are given.
1. Pass by value
2. Return value
3. When initializing an object with this constructor
Any time an object goes out of scope
1. When a function ends
2. When the program ends
3. A block containing a local variable ends 4. A delete operator is called
Default behavior if not defined
Will declare all variables with garbage values, will not set up pointers
Will delete anything on the stack
Reminders
If any constructor is defined then the compiler will not automatically provide one, even if a default constructor is not defined.
Copies the contents of the passed in object to the destination object.
Shallow copy, will only copy over the values stored in each variable
Works with objects that can be assumed to be uninitialized.
Left operand may already be initialized, must delete preexisting dynamic memory when copying
Function
Constructor
Copy Constructor
In-class Worksheet #2
Prototype
ClassName(); ClassName(w/params);
ClassName(const ClassName &);
Job
Builds the object
When is it called?
Default is called when object is declared with no parameters and no “=” sign.
Nondefault is called if parameters are given.
Default behavior if not defined
Will declare all variables with garbage values, will not set up pointers
Reminders
If any constructor is defined then the compiler will not automatically provide one, even if a default constructor is not defined.
Copies the contents of the passed in object to the destination object.
1. Pass by value
2. Return value
3. When initializing an object with this constructor
Shallow copy, will only copy over the values stored in each variable
Works with objects that can be assumed to be uninitialized.
Destructor
Assignment Operator Overload
~ClassName();
ClassName & operator=(const ClassName &);
Destroys the object
Copies the contents of the right operand to the left operand.
Any time an object goes out of scope
1. When a function ends
2. When the program ends
3. A block containing a local variable ends 4. A delete operator is called
When setting an object of the same class type to another object of the same class type
Will delete anything on the stack
Shallow copy, will only copy over the values stored in each variable
Left operand may already be initialized, must delete preexisting dynamic memory when copying
Define a destructor whenever there is dynamic memory in order to delete all of the memory being pointed to on the heap