Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code must be c++ The code given below does not compile due to const issues.

ID: 650568 • Letter: T

Question

The code must be c++

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.
Hint: If you need to completely restore the state of the problem, change the URL in your browser so the number at the end is 0 and then hit enter to visit that URL. Should look like ".../problems/ConstMemberFunctions/0/".

#include <iostream>

using namespace std;

//Do not modify anything on or above the line below this
//START_PROVIDED
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(value % 2 == 0)
            value = newValue;
        else
            value = newValue + 1;
    }

    void increment() { value += 2; }

    void decrement() { value -= 2; }

};
//END_PROVIDED
//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;
}

Explanation / Answer

#include <iostream>

using namespace std;
class EvenNumber
{
private:
int value;
public:
EvenNumber(int startValue)
{

setValue(startValue);
}

int getValue()
{
return value;
}

void setValue(int newValue)
{
  
if(value % 2 == 0)
value = newValue;
else
value = newValue + 1;
}

void increment() { value += 2; }

void decrement() { value -= 2; }

}
void printNumber(const EvenNumber , &n)
{
cout<<n.getValue()<< endl;
}

int main()
{
EvenNumber.e1(4);
e1.increment();
printNumber(e1);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote