Part B - Types

Move and Copy Semantics

Workshop 2


In this workshop, you are to compare move and copy operations on an object that contains a dynamically allocated array of string objects.


Learning Outcomes

Upon successful completion of this workshop, you will have demonstrated the abilities to

  • implement copy semantics for a class with a resource
  • implement move semantics for a class with a resource
  • identify the processing-intensive operations in copy and move assignments
  • retrieve data from a text file using an ifstream object
  • reflect on the material learned in completingh this workshop

Specifications

Text Class

Design and code a class named Text that manages a dynamically allocated array of strings.  Upon instantiation, a Text object receives nothing or a reference to an unmodifiable object of the string class.  The string holds the name of the text file that contains the records to be stored in an object of the Text class.  If no file name is received or if the file does not exist, your Text object assumes a safe empty state.  If the file exists and opens successfully, the one-argument constructor allocates memory for the number of records contained in the file and copies them to dynamically allocated memory. 

To review the syntax for reading from a text file using an ifstream object, see the chapter in the OOP244 notes entitled Input and Output Operators.  See also cplusplus.com

Your design also includes the following member functions:

  • a copy constructor
  • a copy assignment operator
  • a move constructor
  • a move assignment operator
  • a destructor
  • a member function named size_t size() const that returns the number of records of text data

Define your class and its implementation in namespace sict.  Store your class definition in a header file named Text.h and your member function definitions in an implementation file named Text.cpp.

Main Program

The main program that uses your Text class is listed below

 // Workshop 2 - Copy and Move Semantics
 // w2.cpp
 // Chris Szalwinski
 // 2018.05.16

 #include <fstream>
 #include <iostream>
 #include <iomanip>
 #include <utility>
 #include <ctime>
 #include <chrono>
 #include "Text.h"
 using namespace std::chrono;

 // report system time
 //
 void reportTime(std::ofstream& file, const char* msg,
  steady_clock::duration span) {
     auto ms = duration_cast<milliseconds>(span);
     file << msg << " - took - " << std::setw(5) <<
         ms.count() << " milliseconds" << std::endl;
 }

 int main (int argc, char* argv[]) {
     std::cout << "Command Line : ";
     for (int i = 0; i < argc; i++) {
         std::cout << argv[i] << ' ';
     }
     std::cout << std::endl;

     // Command Line syntax
     if (argc == 1) {
         std::cerr << "\n*** Insufficient number of arguments ***\n";
         std::cerr << "Usage: " << argv[0] << " fileName \n";
         return 1;
     }
     else if (argc != 2) {
         std::cerr << "\n*** Too many arguments ***\n";
         std::cerr << "Usage: " << argv[0] << " fileName \n";
         return 2;
     }

     steady_clock::time_point cs, ce;
     std::ofstream file("timer.txt");
     {
         std::cout << std::fixed << std::setprecision(3);
         cs = steady_clock::now();
         sict::Text a;
         ce = steady_clock::now();
         reportTime(file, "Dflt Constructor", ce - cs);
         std::cout << "Constructor - a.size = " << a.size() << std::endl;

         cs = steady_clock::now();
         sict::Text b(argv[1]);
         ce = steady_clock::now();
         reportTime(file, "Constructor     ", ce - cs);
         std::cout << "Constructor - b.size = " << b.size() << std::endl;

         cs = steady_clock::now();
         a = b;
         ce = steady_clock::now();
         reportTime(file, "Copy Assignment ", ce - cs);
         std::cout << "Copy Assignment - a.size = " << a.size() << std::endl;

         cs = steady_clock::now();
         a = std::move(b);
         ce = steady_clock::now();
         reportTime(file, "Move Assignment ", ce - cs);
         std::cout << "Move Assignment - a.size = " << a.size() << std::endl;

         cs = steady_clock::now();
         sict::Text c = a;
         ce = steady_clock::now();
         reportTime(file, "Copy Constructor", ce - cs);
         std::cout << "Copy Constructor - c.size = " << c.size() << std::endl;

         cs = steady_clock::now();
         sict::Text d = std::move(a);
         ce = steady_clock::now();
         reportTime(file, "Move Constructor", ce - cs);
         std::cout << "Move Constructor - d.size = " << d.size() << std::endl;

         cs = steady_clock::now();
     }
     ce = steady_clock::now();
     reportTime(file, "Destructor      ", ce - cs);
 }

A text file named w2.cpp that contains these instructions is available here.

Test Run

A text file named gutenberg_shakespeare is available here.


SUBMISSION

Follow your professor's submission instructions. 

Unless otherwise stated by your instructor, your submission should include the following components:

  1. source code for your w2 module
  2. source code for your Text module
  3. a text file named reflect.txt that includes:
    • a list of the times that each step took (you may copy the contents of file timer.txt
    • an explanation of the difference in the times needed to execute each member operator
    • the corrected answers to the latest quiz that you received
    • a description of what you learned in completing this workshop





  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License