Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
So, I have a main program and I ask two numbers from user in my main program. I also have a class called Calculator and object calculator. Those two numbers should be placed in the object and in the class using getters and setters and then the average should be calculater inside the class Calculator (I know, the formula to calculate average would be (number1+number2)/2 but the problem is not that). The problem is I don't know, what I should write inside the class Calculator that calculating average would be completed there.

//mainprogram.cpp

C++
Calculator calculator (0,0,0.0);

int number1;
int number2;

cout << "Please give the first number:";
cin >> number1;
calculator.setFirstNumber(number1);

cout << "Please give the second number:";
cin >> number2;
calculator.setSecondNumber(number2);


//Calculator.cpp

Calculator::Calculator (int fn, int sn, double av): firstnumber(fn), secondnumber(sn), average(av) {
}

void Calculator::setFirstNumber (int newFnumber) {
      firstnumber = newFnumber;
}

int Calculator::getFirstNumber const () {
      return firstnumber;
}

void Calculator::setSecondNumber (int newSnumber) {
      secondnumber = newSnumber;
}

int Calculator::getSecondNumber const () {
      return secondnumber;
}

void Calculator::setAverage (double newA) {
    //what should I write here? It should calculate and set average from the first and the second  number?
}

double Calculator::getAverage const {
   //what should I write here?
}

How should Calculator class be changed? Average should set and get that I can use it in another classes.
Posted
Updated 13-Nov-13 10:43am
v2
Comments
Aescleal 14-Nov-13 14:53pm    
Average should not be set. It's completely logically bent to even suggest that. It's a derived quantity from the rest of the object's state.

To follow up Richard's comments a bit...

Your interface is confusing. Yep, you've managed to give the class two ways of doing the same stuff - i.e. you can write something like:

C++
Calculator c( 27, 84 );
std::cout << c.average() << std::endl;


or:

C++
Calculator c;
c.setFirst( 27 );
c.setSecond( 84 );
std::cout << c.average() << std::endl;


Pick one and stick with it, your code will be cleaner and easier to read that way. Given my highly rational hatred of getters and setters stick with constructing an object and then using it, don't build it then twiddle it into shape before you can use it.
 
Share this answer
 
Comments
Richard MacCutchan 14-Nov-13 4:08am    
Are you saying that we should never use getters and setters? It seems C# is largely dependent on them.
Aescleal 14-Nov-13 14:47pm    
I can't comment on C# as I don't use the language, but a getters/setters style of programming takes the focus away from the behaviour of objects and places it more on attributes of the object. Instead of telling an object to walk to John O'Groats and letting it get on with it you ask it it's shoe size and calculate the number of steps.

Setters make it far harder to maintain a class's invariant (if you can twiddle small chunks of an object independently it's harder to check the object's state is valid) and encourages the ghastly practice of "constructor-followed-by-initialisation-function-cause-we-dont-understand-exceptions" that pervades certain arenas of software development.
You don't need a method for setAverage as that becomes invalid as soon as either of the numbers changes. Just add the code in getAverage to calculate the average of the two numbers and return it. Also remove the parameter for average in the constructor, again it is redundant.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Nov-13 17:00pm    
Agree, a 5.
—SA
HotDog2000s 14-Nov-13 14:22pm    
Thanks for the answer. My program is working correctly after that. I still have one question: What should I do, if I want at first number1 and number2 should be summed and then average should be calculated?

int Calculator::getTotal() const {
int total = 0;
total = number1+number2;
return total;
}

double Calculator::getAverage() const {
//what should I write here if I want calculate average with total/2 ? I should get "total" from the getTotal and then calculate average with using "total"?
}

I'm asking this because I noticed I need to code my program in that way, because of another classes.
Richard MacCutchan 15-Nov-13 3:15am    
To get the average just code : return getTotal() / 2;. Think first about the simple values, such as the sum, then build your other methods on top of those functions. In that way you reuse existing code rather than rewriting the same pieces over again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900