Welcome! Log In Create A New Profile

Advanced

When to use classes?

Posted by ShaunGVW 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
When to use classes?
September 19, 2009 12:01PM
I have been reading a bit on when, and when not, to use classes. As an example, can someone advise me on the following.
Let's say I want to write a program that will tell me how long to go before a certain event, call it the 2010 soccer world cup. Would this need classes? If so, what classes would I set up?

Thanks.
avatar Re: When to use classes?
September 19, 2009 12:10PM
The only time I don't use classes is when it's just for a helper function, eg. mathematical functions or generic algorithms. Some of these, if made more complex, could be converted into classes but it's not always necessary.

In your example, I'd use classes for the interface and probably a QDateTime class or something derived therefrom.
Anonymous User
Re: When to use classes?
September 20, 2009 09:37PM
your 2010 program would need to hold data pertaining to your 2010 needs, say number of days, weeks, months, years. It would need specific functions to calculate these. These are your fields and methods. So you can have a 2010 class to deal with 2010 data. If you need a function to convert days to weeks, for instance, that can be made into a universal helper function - either make it public so that you include 2010 in other projects, make it static and just refer to it without instantiating a 2010 object or stick it in a helper class like MyHelperClass (this could lead to a jumble up too.) Classes should be loosely coupled with as tight cohesion as possible.
If you're going to write many 2010-related programs/versions, then you would want a template class to derive from. Then you start looking at abstract classes and interfaces (I'm not sure if C++ draws a distinction between the two) and use inheritance.
You can use design patterns when your 2010-class starts getting complex and you want to simplify maintenance. I must just say, from reading pretentious comments in the social forums, that design patterns are merely an aid to solving a type of problem and should not be over-used. It allows one to stand on the shoulders of giants when designing a complex solution and shouldn't make the solution unnecassarily complex.
avatar Re: When to use classes?
September 21, 2009 08:17AM
Quote
Rick
Then you start looking at abstract classes and interfaces (I'm not sure if C++ draws a distinction between the two) and use inheritance.

Unlike Java, there is not distinction between the two. In fact, c++ allows you to hybridise the two concepts. You can have a complete class with data and methods except for a single pure virtual method and it would become abstract (this may sound odd but can be very useful). You can make the equivalent of a Java interface by creating a class definition with nothing but pure virtual functions.
Re: When to use classes?
September 21, 2009 05:17PM
Thanks. That has given me a clearer picture of approaching problems.
Sorry, only registered users may post in this forum.

Click here to login