d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 500 Fg For C++ Homework Help
Prev12
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 7 2014 02:31am
Quote (matthan @ May 7 2014 01:31am)
If you are referring to me using this method to get my homework done, I am incapable of coding or typing in length at the moment.
Regardless, trade is done, close this please


I think he is referring to the actual code. I am not a professional C++ developer as he is, so there are most likely some design flaws. What is a bit annoying is the dude feels entitled enough to point this out, but not humble enough to spare a couple of minutes to elaborate.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 7 2014 09:34am
You're right about that, I'm sorry.

From a quick look:
Code
Question(string text, string choice1, string choice2, string choice3, string choice4, int answer)
{
setText(text);
setAnswer(answer);
setChoices(choice1,choice2,choice3,choice4);
}
In C++03 taking non-built-in types by value (or returning them as such) was terribly inefficient and almost always wrong. In C++11, you could use this idiom when you're certain an allocation is necessary, but then you should move from them immediately in the initializer list, not in the constructor body.

Code
Question(string text, string choice1, string choice2, string choice3, string choice4, int answer) :
_text(std::move(text)),
_choice1(std::move(choice1)),
_choice2(std::move(choice2)),
_choice3(std::move(choice3)),
_choice4(std::move(choice4)),
_answer(answer)
{}
You chose to represent choices as four variables, yet you implied that it's configurable. Why not take an array,vector, or initializer_list?

Code
~Question(){ }
That's hardly necessary.

Code
void display();
You break SRP right here. Not sure if you just wanted to get away with it because it's homework or because of a mistake, but in either case, I don't think newbies should be shown the wrong way.

Code
void display();
bool judge(int);
string getText();
int getAnswer();
void setText(string);
void setAnswer(int);
void setChoices(string,string,string,string);
Those should be inlined, and there's little point in defining them outside of the class definition (except for removing the implicit inline declaration, which is not what I'd want to happen)

Code
Game* g = new Game();
// ...
delete g;
You should never this in modern C++! Use std::unique_ptr. This is the worst thing about this code, because while other mistakes impact only performance or maintainability, using naked new & delete removes exception safety from your code (aside from requiring you to keep track of your allocations, another thing a lot of people do wrong because it's tedious and can and should be automated).

Code
cout << "Enter a question:";
cin >> text;
This will only read the first word, not the whole line/question.

Nothing else jumped at me, but a careful review may reveal more. I don't mention using const instead of constexpr as it's not exactly wrong, just a tad out of date.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 7 2014 10:37am
I didn't use an array because the assignment called for 4 variables. Ironically I had it using an array, which is why the choices are configurable, and then I switched it to the 4 variables to follow spec. I did have the constructor using initializers and I was copying the strings like you said and I did have the accessors and mutators inline. I don't know why I changed it. I wish I had a reason. Because it seems stupid at this point. The last point you've got me on. I don't use c++ rigorously so I don't understand the concept of naked new/delete.

The cin>>text one is a legit bug. Lol. My bad.
Member
Posts: 14,487
Joined: Mar 22 2006
Gold: 295.28
Jul 17 2014 03:03am
i remember having some one do my c++ home work making a calculator and when i brought it to class this hot girl was asking me how i did mine and i couldnt explain lol, i just gave her my paper and she copied it..

when i got my paper back from the teacher it had a note on it asking if i worked with that girl on it haha... i never answered her back

This post was edited by touchedzeroo on Jul 17 2014 03:03am
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll