计算班级内的平均值(getters和setters) [英] Calculating average inside the class (getters and setters)

查看:88
本文介绍了计算班级内的平均值(getters和setters)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个主程序,我在主程序中询问用户的两个数字。我还有一个叫做计算器和对象计算器的类。这两个数字应该放在对象和类中使用getter和setter然后平均值应该在Calculator类中计算(我知道,计算平均值的公式将是(number1 + number2)/ 2,但问题是不是那个)。问题是我不知道,我应该在课程计算器里写什么计算平均值将在那里完成。



//mainprogram.cpp



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

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.

推荐答案

您不需要 setAverage 因为只要其中一个数字发生变化就会变为无效。只需在 getAverage 中添加代码即可计算两个数字的平均值并将其返回。同样删除构造函数中的平均参数,这也是多余的。
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.


跟进Richard的评论...



您的界面令人困惑。是的,你已经设法给了班级两种做同样事情的方法 - 也就是说你可以这样写:



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:

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





或:





or:

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.


这篇关于计算班级内的平均值(getters和setters)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆