Hi, I need help designing a class to work with some test program code that was p
ID: 3629481 • Letter: H
Question
Hi, I need help designing a class to work with some test program code that was provided for me. I am really just having trouble getting started, I know that I need a copy constructor, a default constructor, a 2 argument constructor, assignment operator and a destructor. I also need to have the output << operator overloaded. I have started working on the class myself I just don't think that I am on the right track. Any help would be appreciated. I have no idea what the ID CopyCheck is at the very top, I have never seen anything like that before the main statement. I provided this code before but did not really get the help that I was looking for, I need help with the actual code, not just telling me what constructors and functions I need to write.
I have included the test code that we are supposed to design the class header file "id.h" and the class program file "id.cpp" around.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <id.h>
// #include <id.cpp> // in lieu of makefile
const size_t arraySize = 10;
const size_t numDigits = 2;
ID CopyCheck (ID id) // pass in by value calls CC
{
ID x = id; // initialization calls CC (NOT assignment!)
return x; // return by value calls CC
}
int main()
{
ID p1, p2("Chris Lacher", 100);
std::cout << " IDs after declaration: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
p1.SetName("blah");
p1.SetAge(44);
p2.SetName("blahblah");
p2.SetAge(44);
std::cout << " IDs after Set: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
p1 = CopyCheck(p2);
std::cout << " IDs after p1 = CopyCheck(p2): ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
ID p3 ("Assignment Check", 50);
p1 = p2 = p3;
std::cout << " IDs after p1 = p2 = p3: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
std::cout << " p3 = " << p3 << ' ';
ID * idarray = new ID [arraySize];
std::cout << " ID Array after declaration: ";
for (size_t i = 0; i < arraySize; ++i)
{
std::cout << " id[" << std::setw(numDigits) << i << "] = " << idarray[i] << ' ';
}
for (size_t i = 0; i < arraySize; ++i)
{
idarray[i].SetName("A. B. Student");
idarray[i].SetAge(17 + i);
}
std::cout << " ID Array after Set: ";
for (size_t i = 0; i < arraySize; ++i)
{
std::cout << " id[" << std::setw(numDigits) << i << "] = " << idarray[i] << ' ';
}
}
Explanation / Answer
For the header file "ID.h" #include #include using namespace std; #ifndef ID_H #define ID_H class ID { private: string name; int age; public: friend ostream& operatorname = rhs.name; } //------------------------------------------------------- const ID& ID::operator=(const ID& rhs) { if (this != &rhs) { this->name = rhs.name; this->age = rhs.age; } return *this; } //------------------------------------------------------- std::ostream& operatorRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.