开发者

When to use OOP instead of Arrays

When should I use OOP instead of an array? Does the size of my project matter?

To provide a little background: I'm making a small program for my friend. Basically, he wants to add different scores to each other to be able to see his grade. (Yes, I know there's co开发者_如何学Gommercial software, but this is also a fun exercise.) Anyways, from time to time, he might want the program to recalculate all the scores, so I would have to read the previous entries in. I'm more familiar with arrays, so I was going to use those, but OOP might be an equally good tool. So, basically, when should I use OOP?


Umm... apple (array) vs orange (OOP = Object Oriented Programming)


Arrays can be used in OOP, so it seems that you are confusing concepts.

If you feel that using an assignment class will help you, then you can easily just use that to model your program, but if you just want to use C and use static arrays then that is fine also.

Basically, decide how you want to model your application, then, in most languages you can use arrays, if you feel that is best.

But, if you have an unknown number of scores, then put them into a list. When you want to add them together, if you want, convert the list into an array, but I don't see what that would buy you if you are just adding them up every so often.


Agreed with others, arrays and OOP are two different and overlapping concepts. It's like saying, "How should I get to work today? On time, or in a car?" You can do both/either/neither, they are two different things.

Assuming you have one person (your friend) in the program with a set of scores, and you add all the scores up to find a grade, then just use an "array" (list, sequence, vector, etc)

C++:

vector<float> myScores;
myScores.push_back(100.0);
myScores.push_back(50.5);
myScores.push_back(10.0);
float calcGrade(vector<float> scores) {
    float grade = 0;
    for (unsigned int i=0; i<scores.size(); i++) {
        grade += scores[i];
    }
    return grade / scores.size();
}
calcGrade(myScores)

Python:

scores = []
scores.append(100.0)
scores.append(50.5)
scores.append(10.0)
def calcGrade(scores):
    grade = 0
    for i in scores:
        grade += i
    return grade / len(scores)
calcGrade(scores)

However, if you have multiple people in your program with multiple scores, then you should consider using a better structure to hold data. You could do a more OOP approach

class Person {
    vector<float> scores;
    float calcGrade();
}

or just take it simple with multi-dimensional arrays. But at that point, you probably need some "OOP" in your program.


If jimyi is correct, you're asking which is better ?

class grades
{
   public:
      int grade1;
      int grade2;
};

or

int grades[2];

Neither... You better use a vector of int.


This guy is asking about association techniques. Whether to use aggregation or inheritance. I would recommend you to refer Association (object-oriented programming). Then you can decide according to your architecture design.


I prefer standard functions to handwritten loops:

#include <numeric>
float sum = std::accumulate(scores.begin(), scores.end(), 0.0f);
float average = sum / scores.size();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜