I'm currently working on adding memory capability to a counter program. My program has to use object oriented design aspects such as companionship and inheritance, so I have created two classes: Counter and User Interface. Two counter objects (aCounter and mCounter) are passed to the user interface object so that there are separate instantiations of the counter to keep track of the regular count and the memory count. The problem I am having is trying to update the memory count by adding the current count value and the memory value using objects. The easiest solution I believe would be operator overloading to add the two objects return count method return values, however since the user interface object is passed the counter objects, I can't seem to get the operator+ overload to compile without an error: "expression must be a modifiable lvalue" for mCounter.
Any suggestions or help would be great. I'm also trying to stay away from pointers, just to practice not using them, otherwise I'm assuming I could just read and write to the memory value directly from the setmemory function.
UserInterface.h
#pragma once
#include <iostream>
#include <string>
#include "Counter.h"
#include "Memory.h"
class UserInterface
{
private:
Counter aCounter;
Counter mCounter;
int menuChoice;
public:
UserInterface(Counter &c, Counter &m);
void displayChoices();
void getUserInput();
void executeSelection();
void displayOutput();
void setMemory();
};
UserInterface.cpp
#include "UserInterface.h"
#include "Counter.h"
#include <string>
using namespace std;
UserInterface::UserInterface(Counter &c, Counter &m)
{
aCounter = c;
mCounter = m;
menuChoice = 0;
x = 0;
}
void UserInterface::displayChoices()
{
string choices[] = { "1. increment counter", "2. decrement counter", "3. reset counter", "4. add to memory", "5. reset memory", "6. quit" };
for (int i = 0; i < 6; i++)
{
cout << "\n" << choices[i] << endl;
}
}
void UserInterface::getUserInput()
{
cin >> menuChoice;
}
void UserInterface::setMemory()
{
//What I want to essentially do
mCounter.returnCount() += aCounter.returnCount();
}
void UserInterface::displayOutput()
{
cout << "Current Count: " << aCounter.returnCount() << endl;
cout << "Current Count: " << mCounter.returnCount() << endl;
}
void UserInterface::executeSelection()
{
while (menuChoice != 6)
{
displayChoices();
getUserInput();
switch (menuChoice)
{
case 1: aCounter.incrementCount();
displayOutput();
break;
case 2: aCounter.decrementCount();
displayOutput();
break;
case 3: aCounter.resetCount();
displayOutput();
break;
case 4: setMemory();
displayOutput();
break;
case 5: //do something to reset mem
break;
case 6: cout << "Program Ending" << endl;
break;
}
}
}
Counter.h
#pragma once
class Counter
{
private: int count;
public:
Counter();
void incrementCount();
void decrementCount();
void resetCount();
int returnCount();
Counter operator+(const Counter& c)
{
Counter counter;
counter.count = this->count + c.count;
}
};
Counter.cpp
#include "Counter.h"
#include "Memory.h"
Counter::Counter()
{
count = 0;
}
void Counter::incrementCount()
{
count++;
}
void Counter::decrementCount()
{
count--;
}
void Counter::resetCount()
{
count = 0;
}
int Counter::returnCount()
{
return count;
}
Main.cpp
#include "Counter.h"
#include "UserInterface.h"
#include <iostream>
using namespace std;
int main()
{
Counter c;
Counter m;
UserInterface anInterface(c, m);
anInterface.executeSelection();
return 0;
}
You're trying to overload operator +
whereas you're executing +=
where you had commented.
Just change the operator overloading sign from +
into +=
and rather than writing:
void UserInterface::setMemory()
{
//What I want to essentially do
mCounter.returnCount() += aCounter.returnCount();
}
Since the operator overloading you've coded asks for an instance of a class, not an integer which is returned by the function returnCount()
and that must be a modifiable lvalue
(roughly the values which could be assigned with an =
sign) value.
Do:
void UserInterface::setMemory()
{
mCounter += aCounter; // overloading class instance rather than int
}