Jakeunderscore
Jakeunderscore

Reputation: 49

Is it bad practice to use a class for functions instead of using regular functions?

I'm writing a program that calculates the grade needed to score on a final exam to achieve the grade wanted by the user. So Im using a single class for functions that get input and store all the assignment grades, test grades etc and use that information to determine what is needed to be scored on the final to get a desired grade, accounting for different assignment and test weights of course.

I'm basically just using my class as a way to call functions to perform tasks that I could probably just get away with using normal functions. I'm only using one object. I guess what i'm unclear about is, when to use functions and when to use classes? Is it bad if I have just one object and just use the class to do all the heavy lifting so to speak in order to keep my main small or should I change it all to regular functions? I just like the idea of separate files and it keeps things neat and easier to read.

Here is an example of what part of my class member functions look like.

void Grade::setHwGrades() {
   int i = 1;
   std::cout << "What is the weight of Homework assignments? %";
   std::cin >> hwWeight;
   std::cout << "\nHow many Homework assignments did you have? ";
   std::cin >> numHw;

   // inserts each grade entered into vector homeworkGrades
   while (i <= numHw) {
      std::cout << "Homework " << i << " grade: ";
      std::cin >> grade;
      hwGrades.push_back(grade);
      ++i;
   }

   // calculates average grade for all homework assignments
   for (int i = 0; i < numHw; ++i) {
      hwAverage += hwGrades[i];
   }
   hwAverage /= numHw;
   std::cout << "__________________________" << std::endl;
   std::cout << std::setprecision(2) << std::fixed;
   std::cout << "Homework average: " << hwAverage << std::endl << std::endl;
}

Each component that makes up the total grade, like tests, quizzes, projects, etc., all have their own vectors and the same style of getting the grade inputs. Is this good practice or would it be better to use normal functions or something else entirely? Thanks.

Upvotes: 0

Views: 560

Answers (2)

4386427
4386427

Reputation: 44274

Seems fine to me. The class Grade probably have hwGrades as a member so it fine to save it inside a class for later use.

If your program should be extended later on to handle multiple students, you would simply reuse the Grade class by creating one instance per student.

I would make the average calculation a separate member function of Grade. That could also be the case for the printing.

numHw should be a local variable because you can use hwGrades.size() later on.

I don't know about hwWeight because I can't see how it is used.

BTW: Are you sure hwAverage is zero when the function is called? Maybe you should set it to zero before calculating the average.

Perhaps you should also check for numHw being zero (or less) before the division.

Upvotes: 0

vsoftco
vsoftco

Reputation: 56547

In general, it is best to use a class whenever you have some data (member variables) and closely related functions that process that data (member functions). For example, a class Square can have one member variable length and some member functions like area(), perimeter(), set_coordinates(x,y).

It is recommended that you use a non-member function whenever your function is not closely tied to your object. In your case, try to think what member functions a Grade should have (if any). If you cannot come with a satisfactory answer, it means that probably your design is not the best, and you should try breaking up your problem in more than one class.

The design of a class should be almost like a function: it should do only one thing, and do it right.

Upvotes: 1

Related Questions