Question: CopyType.h #ifndef COPY_TYPE_H #define COPY_TYPE_H #include #include #include using namespace std; class CopyType { public : CopyType(); /// Write the copy constructor and /// destructor


CopyType.h
#ifndef COPY_TYPE_H
#define COPY_TYPE_H
#include
#include
#include
using namespace std;
class CopyType
{
public:
CopyType();
/// Write the copy constructor and
/// destructor prototypes here.
void clearPin();
void printPin();
private:
int *ptr;
int pinLen;
};
#endif
CopyType.cpp
Pageof 2
ZOOM
#ifndef COPY_TYPE_CPP
#define COPY_TYPE_CPP
#include "CopyType.h"
/// Constructor
CopyType::CopyType()
{
srand(static_cast
/// Generates a random PIN length between
/// 10 and 20 (inclusive).
pinLen = rand() % 11 + 10;
/// Allocates memory to store a PIN of that
/// length in an array.
ptr = new int[pinLen];
/// Populates that array with random digits
/// between 0 and 9.
for (int i = 0; i
ptr[i] = rand() % 10;
}
/// TODO: Implement the copy constructor here.
///
///
///
///
///
///
/// To sufficiently provide for deep copies and to prevent
/// data loss, an assignment operator overload should also
/// be written. Overloading the assignment operator is
/// not part of this lab. More about that next chapter.
/// TODO: Implement the destructor here.
///
///
///
/// clearPin will set every digit to zero.
void CopyType::clearPin()
{
for (int i = 0; i
ptr[i] = 0;
}
/// printPin will display the PIN on screen.
void CopyType::printPin()
{
for (int i = 0; i
cout
cout
}
#endif // ! COPY_TYPE_CPP
copyTypeClient.cpp
#include
#include "CopyType.h"
using namespace std;
int main()
{
/// builds pin1 using the constructor
CopyType pin1;
cout
pin1.printPin();
/// builds pin2 using the copy constructor
CopyType pin2 = pin1;
cout
pin2.printPin();
cout
pin2.clearPin();
cout
pin1.printPin();
cout
pin2.printPin();
return 0;
}
Download the files CopyType.h, copyType.cpp, and copyTypeClient.cpp from Canvas. Use these files to create a project in Visual Studio. Read, compile, and run the code. The PIN length and PIN digits are randomized, so your output will look slightly different. 3Select CAUserslilatelSourcelReposlExample DebuglExample.exe rinting pin1: 07205 2 130 8678984 rinting pin2 07205 2 1 30 8670984 Calling pin2.clearPin() Printing pin1: 000 0 8888 88 8888 e rinting pin2 00 0 00 0 0 00 e 0 00 An important thing to notice is that pin2 is a copy of pinl. pin2 was created with the following line: copyType pin2 - pin1; Because we didn't write our own copy constructor (yet), a shallow copy was made. The member pointer ptr in pin2 points to the memory allocated in pin
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
