CSCE156

Spring 2004

Lab 5 – Big Three and Operator Overloading Lab

Instructional Script

  • Start promptly at the beginning of the class even if everyone is not ready.
  • Verify all students in attendance have passed the pretest.
  • Go through the answers to the pretest to check for questions that were particularly difficult for the students.
  • Remind students to follow their handouts and worksheets as they work through the lab.

Review/Discussion:

  • Introduce the objectives of the lab.
  • Ask the students to review quickly the links provided in the handout if they have not done that.
  • Explain briefly the use of a constructor and destructor.
  • Explain the importance of defining a destructor and who calls a destructor.
  • Explain what a copy constructor is and why we need that instead of the default copy constructor provided by the compiler. Briefly introduce the difference between swallow and deep copy while explaining that.
  • Explain what operator overloading is and how it is done. Explain why we need operator overloading.
  • Explain briefly the term dynamic memory allocation and how it is done in C++.

General Comments:

  • Make sure they know where to find the Queens class so they can refer to it during the lab. This should be very helpful to them in completing the lab.
  • Most of the code they will write for Activities 1-4 will be very similar to the Queens class.
  • The Queens class uses “helper” functions like CopyBoard, DeleteBoard, and NewBoard, which facilitate code reuse. Suggest that they might want to do something similar.
  • However, emphasize that the Queens class uses a square matrix, and they must implement one that is not square.

Activity 1: Implement a constructor (15 minutes)

  • The goal of this activity is to define a constructor for a dynamic matrix.
  • Make sure they realize that, unlike the Queens class, the matrix here does NOT have the same number of rows and columns.

Notes: (How easy was it for the students to do this activity? How long did they take?)

Activity 2: Implement the destructor (10 minutes)

  • The goal of this activity is to teach the students how to define a destructor for a class.
  • If they need help, first tell them that the Queens class destructor is very similar.

Notes: (How easy was it for the students to do this activity? How long did they take?)

Activity 3: Implement the copy constructor (15 minutes)

  • The goal of this activity is to make the students write a copy constructor for a program.
  • They should also see the difference between shallow and deep copy after doing this activity.

Notes: (How easy was it for the students to do this activity? How long did they take?)

Activity 4: Overload the assignment operator (15 minutes)

  • The goal of this activity is to get the students to overload the assignment operator.
  • They should also learn when the copy constructor and overloaded assignment operators are each used.

Notes: (How easy was it for the students to do this activity? How long did they take?)

Activity 5: Overload addition operator (20 minutes)

  • The goal of this activity is to get the students to overload the addition operator.
  • They would overload an addition operator so that it will perform matrix addition. That means by doing C = A + B, where A, B and C are matrices, it will add the matrix A and B, then store the result in matrix C.
  • Make sure to tell them that the multiplication is already done, and they should be able to figure out how to do plus, which is easier, based on multiplication.
  • Some may not remember how to add matrices.

Notes: (How easy was it for the students to do this activity? How long did they take?)

Activity 6: [Extra Credit] Calculate the column sums of a matrix

  • The goal of this activity is to implement one method that will transpose the matrix.
  • This should be pretty easy, but requires de-allocating and allocating memory, so it might be a bit tricky for them.

Notes: (How easy was it for the students to do this activity? How long did they take?)

9/19/20181