Brock T.

asked • 03/01/22

Const Member Functions The code given below does not compile due to const issues.

Modify the existing class code and add const in the appropriate locations to make it compile with the existing main and printNumber


#include <iostream>
using namespace std;
//Do not modify anything on or above the line below this6
//YOUR_CODE_BELOW
class EvenNumber {
private:
   int value;
public:
   EvenNumber(int startValue) {
       //use our function to do the work...
       setValue(startValue);
  }
   int getValue() {
       return value;
  }
   void setValue(int newValue) {
       //if given odd number, increase by 1
       if(newValue % 2 == 0)
           value = newValue;
       else
           value = newValue + 1;
  }
   void increment() { value += 2; }
   void decrement() { value -= 2;
};
//YOUR_CODE_ABOVE
//Do not modify anything on or below the line above this
void printNumber(const EvenNumber& n) {
   cout << n.getValue() << endl;
}
int main
{
   EvenNumber e1(4);
   e1.increment();
   printNumber(e1);
   return 0;
}


1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.