Welcome! Log In Create A New Profile

Advanced

Assignment 3 - Question 2 - Preference (continue)

Posted by Adri 
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
Re: Assignment 3 - Question 2 - Preference (continue)
July 18, 2007 08:30PM
Adri Wrote:
-------------------------------------------------------
> Hi all,
>
> I've got an enum question. If I understand it
> correctly, if you take
>
> enum Rating{undefined=0, None, Bad, Poor, Fair,
> Good, Very, Excellent} rate;
>
> and you say
>
> rate = Bad;
>
> Then rate actually gets the value 2. Is that
> right?
> If so, how do you convert a string "Bad" to a type
> 'Rating' Bad. It is not happy with
>
> rate = "Bad" and I'm not sure how to convert the
> string I get in to a value that it will like.
>
> Any advice?
>
> Thanks.


I haven't tried this question yet so I could be wrong, but did you try:

enum Rating {"Undefined"=0, "None"=1 ....}

Or maybe you need typecasting it, something like:

rate = static_cast<Rating>(Bad);
Re: Assignment 3 - Question 2 - Preference (continue)
July 19, 2007 09:56PM
I see there is a lot of discussion about using QVariant here. Not sure what the final sollution was with QVariant, but there is another way.

You can add typecast overloading to your object so that it can be cast to an int. Then QVariants are never created to compare the values as they both are the same.
Re: Assignment 3 - Question 2 - Preference (continue)
July 20, 2007 08:50AM
Hi

I have tried casting the string (like "Bad"winking smiley to type Rating, but I couldn't get it to work. I have completed the question with the workaround suggested by kram(Thanks by the way smiling smiley).

Whether using QVariant like I did was the best or most correct way is debatable, I'm just happy my test case works..yeehaah (Will work on understanding the bigger picture later).

Thanks
a3
Re: Assignment 3 - Question 2 - Preference (continue)
July 24, 2007 10:02PM
Hi,

I know that this topic has been covered quite extensively but I am still confused about a couple of things.

1) Firstly the qDebug() << verygood.toString(); statement. My Preference class is based on QVariant and as such Preference verygood("Very Good"winking smiley is stored as 6. How does toString convert this back to "Very Good". Am I supposed to override the toString() function?

2) Secondly I am missing something, what is the point of the fromString(QString) function? The constructors are used depending on wether to input is an integer or string, so why then do we need the fromString() function?

Any advice
Thanks
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 07:32AM
Nick

You should have a QStringList with then enum eg. 6 being an index to the string list.
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 08:39AM
Hi Andre,

I do, but do I then override the toString function to look up the string?
Or am I confused?
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 10:23AM
Nick

As far as I can remember , yes you should override it.
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 10:33AM
Or is toString() supposed to use my overloaded operator<< ??????

If so do you have any ideas why it would not be using it?
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 10:53AM
nick@cks.co.za Wrote:
-------------------------------------------------------
> Or is toString() supposed to use my overloaded
> operator<< ??????
> If so do you have any ideas why it would not be
> using it?

toString() should return a String representation of your object, in this case it should return the option (not the number e.g. "Very Good"winking smiley.

I wouldn't call the overloaded operator << from the toString method, you'll have to do some fancy code to convert the stream to string again. It would be better if your overloaded operator << calls the toString method.
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 11:49AM
Thanks all,

If I overload the toString function it makes up part of my public interface of the Preference Class, but the text book provides me with the public interface, surely I can not add to it? The same goes for the overloaded operator<, operator>, etc.

Or am i trusting the text book to much?
Anonymous User
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 03:20PM
I remember from 214 that a to-string method is required as part of the canonical form of a class. Supposed to be good oo programming practice to include it in the class.
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 03:31PM
In the textbook it says:

(pg 557)

Here is a public interface of the class definition to get you started

he then goes on to say (under the class definition)

Complete the class definition in preference.h


from this and the implementation of the test() function on the bottom of pg 557 - pg 558 I just assumed that I had to do whatever was necessary in order to get the Preference class to pass the test.

So just start writing code man, whatever it takes - don't worry about whether it's the same as the book.

later,

kram
Re: Assignment 3 - Question 2 - Preference (continue)
July 25, 2007 08:02PM
Well I am completely clueless - I have been trying to make heads or tails of the suggestions made by everyone and I am still stumped.
This subject has really got the better of me and I am still waiting for my assignment 02 results....

I just hope that the 3 assignment is marked leniently.....
Sorry, only registered users may post in this forum.

Click here to login