Write a main program which must includes a minimum of 10 test cases to exercise
ID: 3700019 • Letter: W
Question
Write a main program which must includes a minimum of 10 test cases to exercise the copy constructor and the overloaded operators
/7 class counter header file pcounter.h #ifndefPCOUNTERH - - #define -PCOUNTER_A Taclass to hold a pointer to an integer counter class Counter public // constructors Counter(int k); Counter(const Counter& rhs); 7/ copy constructor overloading operators Counter& operator-(const Counter& rhs); // overloading of operator +-,-, and *= Counter& operator+(const Counter& rhs); Counter& operator--(const Counter& rhs); Counter& operator*-(const Counter& rhs); // overloading of binary operator +,-, * Counter operator+(const Counter& rhs) const; Counter operator-(const Counter& rhs) const; Counter operator (const Counter& rhs) const; // overloading of comparison operators-and != bool operator(const Counter& rhs) const; bool operator!-(const Counter& rhs) const; increment and decrement functions void incrementO; void decrementO; // get function int getCountO const; //destructor CounterO; private int* _p; // holds the pointer to a dynamically allocated integer count #endif //-PCOUNTER. HExplanation / Answer
//pcounter.h
#include "pcounter.h"
using namespace std;
Counter::Counter(int k)
{
_p = new int;
*_p = k;
}
Counter::Counter(const Counter & rhs)
{
*_p = *rhs._p;
}
Counter & Counter::operator=(const Counter & rhs)
{
*_p = *rhs._p;
return *this;
}
Counter & Counter::operator+=(const Counter & rhs)
{
(*_p) += *rhs._p;
return *this;
}
Counter & Counter::operator-=(const Counter & rhs)
{
(*_p) -= *rhs._p;
return *this;
}
Counter & Counter::operator*=(const Counter & rhs)
{
(*_p) *= *rhs._p;
return *this;
}
Counter & Counter::operator+(const Counter & rhs)
{
(*_p) += *rhs._p;
return *this;
}
Counter & Counter::operator-(const Counter & rhs)
{
(*_p) -= *rhs._p;
return *this;
}
Counter & Counter::operator*(const Counter & rhs)
{
*_p *= *rhs._p;
return *this;
}
bool Counter::operator==(const Counter & rhs)
{
return *_p == rhs.getCount();
}
bool Counter::operator!=(const Counter & rhs)
{
return *_p != rhs.getCount();
}
void Counter::increment()
{
(*_p)++;
}
void Counter::decrement()
{
(*_p)--;
}
int Counter::getCount() const
{
return *_p;
}
Counter::~Counter()
{
if (_p)
delete _p;
}
//main.cpp
#include "pcounter.h"
#include <iostream>
using namespace std;
int main()
{
Counter counter1(20);
Counter counter2(10);
counter1 + counter2;
cout << counter1.getCount() << endl;
counter1.increment();
cout << counter1.getCount() << endl;
counter1.decrement();
cout << counter1.getCount() << endl;
bool result = counter1 != counter2;
cout << result << endl;
result = counter1 == counter2;
cout << result << endl;
counter1 += counter2;
cout << counter1.getCount() << endl;
counter1 -= counter2;
cout << counter1.getCount() << endl;
counter1 *= counter2;
cout << counter1.getCount() << endl;
counter1 * counter2;
cout << counter1.getCount() << endl;
counter1 - counter2;
cout << counter1.getCount() << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.