Question: I need a solution for part 2 only, thank you. Workshop 8: Smart Pointers In this workshop, you combine two lists and use a smart

I need a solution for part 2 only, thank you.

Workshop 8: Smart Pointers

In this workshop, you combine two lists and use a smart pointer to ensure that memory is deallocated in the possible presence of an exception.

Learning Outcomes

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

  • Build a program component of quadratic complexity
  • use a smart pointer to move an object

Submission Policy

The workshop is divided into two coding parts and one non-coding part:

  • Part 1: worth 0% of the workshop's total mark, is optional and designed to assist you in completing the second part.
  • Part 2: worth 100% of the workshop's total mark, is due on Sunday at 23:59:59 of the week of your scheduled lab. Submissions of Part 2 that do not contain the reflection are not considered valid submissions and are ignored.
  • reflection: non-coding part, to be submitted together with Part 2. The reflection does not have marks associated to it, but can incur a penalty of max 40% of the whole workshop's mark if your professor deems it insufficient (you make your marks from the code, but you can lose some on the reflection).

The workshop should contain only work done by you this term or provided by your professor. Work done in another term (by you or somebody else), or work done by somebody else and not clearly identified/cited is considered plagiarism, in violation of the Academic Integrity Policy.

Every file that you submit must contain (as a comment) at the top your name, your Seneca email, Seneca Student ID and the date when you completed the work.

If the file contains only your work, or work provided to you by your professor, add the following message as a comment at the top of the file:

If the file contains work that is not yours (you found it online or somebody provided it to you), write exactly which parts of the assignment are given to you as help, who gave it to you, or which source you received it from. By doing this you will only lose the mark for the parts you got help for, and the person helping you will be clear of any wrong doing.

Compiling and Testing Your Program

All your code should be compiled using this command on matrix:

/usr/local/gcc/10.2.0/bin/g++ -Wall -std=c++17 -g -o ws file1.cpp file2.cpp ...

  • -Wall: compiler will report all warnings
  • -std=c++17: the code will be compiled using the C++17 standard
  • -g: the executable file will contain debugging symbols, allowing valgrind to build better reports
  • -o ws: the compiled application will be named ws

After compiling and testing your code, run your program as following to check for possible memory leaks (assuming your executable name is ws):

valgrind ws

To check the output, use a program that can compare text files. Search online for such a program for your platform, or use diff available on matrix.

Part 1 (0%)

The first portion of this workshop consists of modules:

  • w8 (supplied)
  • EmpProfile (incomplete)
  • GeneratingList (incomplete)
  • WritingRecord (supplied header, incomplete implementation)

Your tasks for this part of the workshop are:

  • Add a rangeValidator() function to the EmpProfile module
  • Add an operator += overload to the GeneratingList module using raw pointer syntax
  • Add an implementation of Luhn Algorithm to the GeneratingList module to validate the SIN
  • Complete the writeRaw() function in the WritingRecord module using raw pointer syntax

EmpProfile Module

The EmpProfile module holds employee, salary and correct wages information.

  • The Employee structure holds employee SIN and employee name.
  • The Salary structure holds employee SIN and current salary.
  • The EmployeeWage structure holds employee name and employee salary.

Your Task: Add a rangeValidator() function to the EmploeeWage type. Your function reports an exception if the object's salary is more than 99,999 or less than 0. Your function receives nothing and returns nothing.

GeneratingList Module

The GeneratingList module defines a class that retrieves a list of records stored in a text file, holds the elements in an STL vector, provides access to them by index and displays them to an output stream.

Your Task:

  • Add an operator += overload to the GeneratingList template. Your function receives the address of an object to be stored in the GeneratingList container and moves the object into that container. Your overload returns nothing.
  • Add an implementation of Luhn's Algorithm to check the validity of the SIN. Your function receives a reference of a const std::string type representing the SIN to check and return true if the SIN is valid, false otherwise.

WritingRecord Module

Your Task: Implement the writeRaw() function. This function creates a list of active employees; an employee is considered active if both lists received as parameters contain an employee with the same SIN. For each active employee found in both lists, dynamically build a new object of type EmployeeWage and add it to the collection activeEmp only if it passes both validations below:

  • validate the salary before adding it using the function you defined above
  • validate the SIN using Luhn's algorithm you implemented above; if the SIN doesn't pass validation, generate an exception of type std::string

w8 Module (supplied)

The tester module has been supplied. Do not modify the existing code!

Sample Output

When the program is started with the command (the input files are provided):

ws NamesSIN.dat CorrectSalary.dat WrongSalary.dat 

the output should look like the one from the sample_output.txt file.

Test Your Code

To test the execution of your program, use the same data as shown in the output example above.

Upload your source code to your matrix account. Compile and run your code using the latest version of the g++ compiler (available at /usr/local/gcc/10.2.0/bin/g++) and make sure that everything works properly.

Then, run the following command from your account (replace profname.proflastname with your professor's Seneca userid):

~profname.proflastname/submit 345_w8_p1 

and follow the instructions.

This part represents a milestone in completing the workshop and is not marked!

Part 2 (100%)

The second part of this workshop upgrades your solution to use smart pointers.

Your tasks for this part of the workshop are:

  • Add an operator += overload to the GeneratingList module using smart pointer syntax
  • Complete the WriteSmart() function in the WritingRecord module using smart pointer syntax

These two functions should do the same thing as the raw pointer version, but using unique smart pointers instead.

w8 Module (supplied)

The tester module has been supplied. Do not modify the existing code!

Sample Output

When the program is started with the command (the input files are provided):

ws NamesSIN.dat CorrectSalary.dat WrongSalary.dat 

CorrectSalary.dat

506745728 35715.23 505968818 33525.96 174784181 95875.12 524544921 15478.54

EmpProfile.h

#ifndef SDDS_EMPPROFILE_H #define SDDS_EMPPROFILE_H #include  #include  #include  #include  namespace sdds { struct Employee { std::string id; std::string name; bool load(std::ifstream& f) { f >> id >> name; return f.good(); } void print(std::ostream& os) const { os << std::setw(10) << id << std::setw(7) << name << std::endl; } }; struct Salary { std::string id; double salary; bool load(std::ifstream& f) { f >> id >> salary; return f.good(); } void print(std::ostream& os) const { os << std::setw(10) << id << std::setw(10) << salary << std::endl; } }; struct EmployeeWage { std::string name{}; double m_salary{}; int m_counter{}; static int recCount; static bool Trace; EmployeeWage() { m_counter = ++recCount; if (Trace) { std::cout << "Default Constructor [" << m_counter << "]" << std::endl; } } EmployeeWage(const std::string& str, double sal) { this->name = str; this->m_salary = sal; m_counter = ++recCount; if (Trace) { std::cout << "Ovdrloaded Constructor"<name = copyEmpProf.name; this->m_salary = copyEmpProf.m_salary; m_counter = ++recCount; if (Trace) { std::cout << "Copy Constructor "<< std::setw(11) << "[" << m_counter << "] from [" << copyEmpProf.m_counter <<"]" << std::endl; } } ~EmployeeWage() { if (Trace) { std::cout << "Destructor "<< std::setw(17) << "[" << m_counter << "]" << std::endl; } } //TODO: add a function here to check correct salary range void print(std::ostream& os)const { os << std::setw(15) << name << std::setw(10) << m_salary<

GeneratingList.h

#ifndef SDDS_GENERATINGLIST_H #define SDDS_GENERATINGLIST_H #include  #include  #include  #include  #include  #include  #include  namespace sdds { template class GeneratingList { std::vector list; public: GeneratingList(){} GeneratingList(const char* f) { std::ifstream file(f); if (!file) throw std::string("*** Failed to open file ") + std::string(f) + std::string(" ***"); while (file) { T t; if (t.load(file)) list.push_back(T(t)); } } size_t size() const { return list.size(); } const T& operator[](size_t i) const { return list[i]; } //TODO: Implement the Luhn Algorithm to check the // valadity of SIN No's //TODO: Overload the += operator with a smart pointer // as a second operand. //TODO: Overload the += operator with a raw pointer // as a second operand. void print(std::ostream& os) const { os << std::fixed << std::setprecision(2); for (auto& e : list) e.print(os); } }; template std::ostream& operator<<(std::ostream& os, const GeneratingList& rd) { rd.print(os); return os; } } #endif // !SDDS_GENERATINGLIST_H

NamesSIN.dat

799273984 Jill 174784181 Mike 524544921 Jack 506745728 Kyle 505968818 Carol

WritingRecord.cpp

#include "GeneratingList.h" #include "EmpProfile.h" #include "WritingRecord.h" using namespace std; namespace sdds { GeneratingList writeRaw(const GeneratingList& emp, const GeneratingList& sal) { GeneratingList activeEmp; // TODO: Add your code here to build a list of employees // using raw pointers return activeEmp; } GeneratingList writeSmart(const GeneratingList& emp, const GeneratingList& sal) { GeneratingList activeEmp; // TODO: Add your code here to build a list of employees // using smart pointers return activeEmp; } }

WritingRecord.h

#ifndef SDDS_WRITINGRECORD_H #define SDDS_WRITINGRECORD_H #include "GeneratingList.h" #include "EmpProfile.h" namespace sdds { GeneratingList writeRaw(const GeneratingList& emp, const GeneratingList& sal); GeneratingList writeSmart(const GeneratingList& emp, const GeneratingList& sal); } #endif // SDDS_WRITINGRECORD_H 

WrongSalary.dat

506745728 35715.23 505968818 33525.96 174784181 95875.12 524544921 154782.54

w8_p2.cpp

#include  #include  #include  #include "GeneratingList.h" #include "EmpProfile.h" #include "WritingRecord.h" using namespace std; using namespace sdds; int EmployeeWage::recCount = 0; bool EmployeeWage::Trace = true; int main(int argc, char** argv) { cout << "Command Line: " << argv[0]; for (int i = 1; i < argc; i++) cout << " " << argv[i]; cout << endl << endl; if (argc != 4) { cerr << endl << "***Incorrect number of arguments***" << endl; return 1; } try { sdds::GeneratingList emp(argv[1]); sdds::GeneratingList correctSal(argv[2]); sdds::GeneratingList wrongSal(argv[3]); cout << setw(10) << "SIN No" << setw(17) << "Employee Name" << endl; cout << emp << endl; cout << "********************************************" << endl << "* Correct Salaries with SIN No's" << endl << "********************************************" << endl; cout << setw(10) << "SIN No" << setw(10) << "Salary" << endl; cout << correctSal << endl; cout << "********************************************" << endl << "* Wrong Salaries with SIN No's" << endl << "********************************************" << endl; cout << setw(10) << "SIN No" << setw(10) << "Salary" << endl; cout << wrongSal << endl; EmployeeWage::Trace = true; cout << "********************************************" << endl << "* Merging wrong salaries using Raw Pointers" << endl << "********************************************" << endl; try { GeneratingList empPro = writeRaw(emp, wrongSal); } catch (const string& msg) { cout << "ERROR: " << msg << endl; } cout << endl; cout << "********************************************" << endl << "* Merging wrong salaries using Smart Pointers" << endl << "********************************************" << endl; try { GeneratingList empPro = writeSmart(emp, wrongSal);; } catch (const string& msg) { cout << "ERROR: " << msg << std::endl; } EmployeeWage::Trace = false; cout << endl << endl; // no exceptions should be generated from the code below. cout << "********************************************" << endl << "* Merging correct salaries using Raw Pointers" << endl << "********************************************" << endl; { GeneratingList empPro = writeRaw(emp, correctSal); cout << setw(5) << "Employee Name" << setw(10) << "Salary" << endl; cout << empPro << endl; } cout << "********************************************" << endl << "* Merging good prices using Smart Pointers" << endl << "********************************************" << endl; { GeneratingList empPro = writeSmart(emp, correctSal); cout << setw(5) << "Employee Name" << setw(10) << "Salary" << endl; cout << empPro << endl; } } catch (...) { cout << "ERROR: Unknown error!"; } return 0; }

sample_output.txt

Command Line: ws NamesSIN.dat CorrectSalary.dat WrongSalary.dat SIN No Employee Name 799273984 Jill 174784181 Mike 524544921 Jack 506745728 Kyle 505968818 Carol ******************************************** * Correct Salaries with SIN No's ******************************************** SIN No Salary 506745728 35715.23 505968818 33525.96 174784181 95875.12 524544921 15478.54 ******************************************** * Wrong Salaries with SIN No's ******************************************** SIN No Salary 506745728 35715.23 505968818 33525.96 174784181 95875.12 524544921 154782.54 ******************************************** * Merging wrong salaries using Raw Pointers ******************************************** Ovdrloaded Constructor [1] Copy Constructor [2] from [1] Destructor [1] Ovdrloaded Constructor [3] Destructor [3] Destructor [2] ERROR: *** Employees salaray range is not valid ******************************************** * Merging wrong salaries using Smart Pointers ******************************************** Ovdrloaded Constructor [4] Copy Constructor [5] from [4] Destructor [4] Ovdrloaded Constructor [6] Destructor [6] Destructor [5] ERROR: *** Employees salaray range is not valid ******************************************** * Merging correct salaries using Raw Pointers ******************************************** Employee Name Salary Mike 95875.12 Jack 15478.54 Kyle 35715.23 Carol 33525.96 ******************************************** * Merging good prices using Smart Pointers ******************************************** Employee Name Salary Mike 95875.12 Jack 15478.54 Kyle 35715.23 Carol 33525.96 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!