Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there any kind of rules or a guide to when should I create classes?

I am doing a school assignment in where I create a basic company workers management system.

I input the data of the workers and save them on a file, and use them all over the program.

I have some functions that I have to write like, inputWorkersDataAndSaveToFile(), readFromFileAndPrintWorkersData(), sortAndPrint, diplayWorkersWithSalariesAbove, and a few more.

So it got me wondering, should I create a class and group all these functions together since they are all related? Would it be necessary since I am going to use only one instance of the class ever?

Worth noting that I want create a vector that hold all the workers data and plan using that vector in multiple functions. One reason I started thinking about creating a class is that I am trying to avoid using global variables.

So my program would have two classes class Worker and class Manager (the latter is not a class to represent an employee but a class to manage the functions mentioned above).

Should I create a class in this scenario? Also, is there any rule of thumbs to when to create classes.

My question really falls back on how to design things. 

Thanks 


What I have tried:

I have searched about this topic but didn't find much.
Posted
Updated 2-May-20 23:02pm
v2

Quote:
Is there any kind of rules or a guide to when should I create classes?

Technically, there is absolutely no rule.
You can write code without a single class, using only standard objects, you can write code with 100% classes, and anything in between.
It is more a matter of the way you learn programming, techniques and likes/dislikes.
I am an old fashioned guy, I learned programming in an era where C++ didn't exist. I usually create minimum classes/objects for my projects.
 
Share this answer
 
v2
Comments
hbtalha 3-May-20 9:35am    
So how do you deal with global variables?? you don't mind them?
Patrice T 3-May-20 12:36pm    
I declare them on top, before any code.
Having a class for Worker and one for Manager may make sense, but it depends on what your application does. Note that they're also both employees, so maybe they should derive from a base Employee class that handles things that are common to both.

More generally, classes usually follow is-a and has-a relationships. A worker is an employee, so this suggests that Worker should derive from Employee. Both managers and workers have a salary, so that suggests that Employee contains a Salary (maybe just an int, but more generally it means that Employee has a Salary object as one of its data members).

With regard to your functions, it's best to split them up into separate capabilities: read from a file, write to a file, print, sort, select according to some criterion. When split this way, it's easy to chain them together to do things that would otherwise force you to copy and paste pieces of code from different functions to create another monolithic one.
 
Share this answer
 
v2
Comments
hbtalha 2-May-20 18:33pm    
Sorry, something important got lost in translation. The class manager is not to represent an employee. It is a class that I want to group the functions print, sort, read, etc together and add some variables too.

Now I see that the way I wrote the question causes confusion. Sorry
See C++ Classes and Objects[^]. some good examples of what a class should be based on.
 
Share this answer
 
Comments
hbtalha 3-May-20 9:45am    
The link has some good examples but for complete beginners, what I am not. It just gives the basic and most trivial class examples, Fruits, Cars, Myclass etc etc.
What I need is some kind of guide on when to create classes, like, is it worth create a class group some functions knowing that I will only have one instance of that in the entire program?
Richard MacCutchan 3-May-20 10:29am    
Well if you are not a complete beginner then I assume you know when to create classes, and when to just use helper functions.
hbtalha 3-May-20 10:37am    
I know most of the times when to create classes. But in this scenario I am not sure if I should.

Should I create a class that is going to have only one object?
Richard MacCutchan 3-May-20 10:38am    
Only you can answer that question, because only you know what your program is going to do.
hbtalha 3-May-20 10:58am    
I know it is a choice of mine to make.
What I am asking is about a design in general.
When does one should group some functions in a class?
Design of code is very important because it leeds to small and understandable code. One main aspect of classes is a unique set of tasks or properties. So Worker and Manager are classes. Take the time to learn the basic about classes and its usage.

i recommend this Learn C++ tutorial. Classes are chapter 8, so you will need some time before understanding it.

tips:
- separate display and file operation from calculation code
- use classes like vector and string because they have well designed code which you will need.
- try to work with pointers to objects, because it speeds the execution
 
Share this answer
 
Comments
hbtalha 3-May-20 9:47am    
I already know the basic about classes and its usage, however the website is very good and will prove to be useful in the future, thanks.

What I need is a guide to when to create them, like, is it worth create a class group some functions knowing that I will only have one instance of that in the entire program?
KarstenK 4-May-20 2:54am    
Ofcourse. Class design is also for separating code for clear, testable and reusable purposes. ;-)
hbtalha 4-May-20 10:16am    
I have created the class and used the Singleton Pattern.
hbtalha 3-May-20 9:55am    
Could you provide examples with code to elaborate on the tips you gave me?

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