67 Replies - 6141 Views - Last Post: 30 July 2015 - 10:03 PM
#46
Re: My Questions C++
Posted 17 July 2015 - 04:13 PM
If you include a bunch of header files you don't necessarily need, wouldn't that slow down your code overall?
#47
Re: My Questions C++
Posted 17 July 2015 - 08:30 PM
#48
Re: My Questions C++
Posted 20 July 2015 - 10:29 AM
#49
Re: My Questions C++
Posted 21 July 2015 - 10:06 AM
Also, could someone maybe explain to me what you do when it comes to creating getter functions? Do you make a const version and a non const version? I'm still working on learning classes, so I'm sure I'm a little confusing, but please bear with me and help push me in the correct direction.
Thanks guys!
#50
Re: My Questions C++
Posted 21 July 2015 - 10:13 AM
Quote
CONST is used to detail if a variable is modifiable or not... not a function or property. Just because a property uses a variable does not mean you need to tell anything downstream that uses it to be 'const' as well too.
The const accessorizes the variable and not what uses it.
#51
Re: My Questions C++
Posted 21 July 2015 - 10:36 AM
Taken from learncpp.com
Now, consider the following call: 1 std::cout << cSomething.GetValue(); Surprisingly, this will cause a compile error! This is because const class objects can only call const member functions, GetValue() has not been marked as a const member function. A const member function is a member function that guarantees it will not change any class variables or call any non-const member functions.
#52
Re: My Questions C++
Posted 21 July 2015 - 10:40 AM
ChrispyChris, on 21 July 2015 - 07:06 PM, said:
That's only true if you're returning a reference - that is a const member function must not return a non-const reference to a member variable. If you're returning by value, it does not matter at all.
The reason for that is that a non-const reference to a member can be used to change the object, so any member function that allows this, can't be const.
Quote
If a member function does not change the object or allow changes to the object (which is generally true of getters), it should be const because otherwise it will not be possible to call that function on const objects.
Quote
If you need to return a non-const reference in some cases, it makes sense to have a const and non-const version. However in most cases having a setter function to go with the getter would make more sense than returning a non-const reference.
One situation where you usually do define two versions is the `[]` operator, which necessarily has to return a non-const reference if you want to change elements (there's no sensible way to define a "`[]` setter"). So const objects would have a `[]` operator returning a const reference and non-const objects would have one returning a non-const reference, so the user can write `non_const_object[i] = foo;`.
modi123_1, on 21 July 2015 - 07:13 PM, said:
That's wrong. There are const member functions in C++.
#53
Re: My Questions C++
Posted 21 July 2015 - 11:08 AM
Is that all sounding about right now? Thank you so much and PLEASE let me know if what I just said is wrong anywhere. I feel like I understand it now though, so hopefully that's all correct
#54
Re: My Questions C++
Posted 21 July 2015 - 11:58 AM
ChrispyChris, on 21 July 2015 - 08:08 PM, said:
No, if you return by value, it does not matter if the returned value is const or not. Only if you return a pointer or reference. If you have code with a const function returning a non-const value that does not compile, please post the code, so I can look at what might be going wrong there.
#55
Re: My Questions C++
Posted 21 July 2015 - 12:10 PM
In the simplest way I can put it, from what I actually read straight off the site: This is because const class objects can only call const member functions
I understood everything you said in the first post, which helped a ton, but now I'm curious if this is correct. Thanks for helping!
#56
Re: My Questions C++
Posted 21 July 2015 - 12:13 PM
#57
Re: My Questions C++
Posted 22 July 2015 - 10:03 AM
//Checks if macros fit into calories
void UserStats::MacrosToCalories(float calorieTotal, float carbohydrateCheck, float fatCheck, float proteinCheck) const
{
//Calculate if macros are correct for amount of calories specified
float macroIndex;
macroIndex = (carbohydrateCheck * 4) + (fatCheck * 9) + (proteinCheck * 4);
if(macroIndex > calorieTotal)
{
std::cout << "Your macronutrients do not fit into your amount of calories specified. ";
}
std::cout << "Let me adjust your macros for you. Enter \"1\" if you want me to adjust your carbs, \"2\" if you "
<< "would like me to adjust your fats, or \"3\" if you would like me to adjust your protein.\n";
int answer;
std::cin >> answer;
if(answer == 1)
{
int carbCounter = 0;
do
{
macroIndex - 4;
carbCounter++;
}
while(macroIndex > calorieTotal);
std::cout << "You need to remove " << carbCounter << "carbs from your total amount.";
}
if(answer == 2)
{
int fatCounter = 0;
do
{
macroIndex - 9;
fatCounter++;
}
while(macroIndex > calorieTotal);
std::cout << "You need to remove " << fatCounter << "fats from your total amount.";
}
}
I am having a problem with this function, when I call it in my main, I get to the point where I enter 2 to adjust the fats. That is all I have it set up for at this point, because I'm trying to come up with a good way to do it. My input is 2000 calories total, 200 carbs, 45 fats, and 200 protein. Now, in this 2000 calorie diet, you cannot have 45 grams of fat, only 44 to be under the 2000 calorie limit. I enter 2 to adjust the fats but then the program hangs. Can you identify the problem from this function alone, or should I post all three files of my code?
I would appreciate the help, thanks!
This post has been edited by ChrispyChris: 22 July 2015 - 10:06 AM
#58
Re: My Questions C++
Posted 22 July 2015 - 01:14 PM
ChrispyChris, on 22 July 2015 - 10:03 AM, said:
I would appreciate the help, thanks!
That's because you are in an infinite loop.
You increment Fat count, but when you subtract from macroIndex, it doesn't increment. MacroIndex is already greater than calories and never subtracts, therefore the fat count is just counting up.
Consider revising this part:
macroIndex - 9;
Edit: I would also suggest learning about debugging. Placing a break point at this part of the program would have shown you what is happening behind the scenes. It would have been a quick fix as it would have shown that the variables are not doing what you think they are.
This post has been edited by Vaypur: 22 July 2015 - 01:21 PM
#59
Re: My Questions C++
Posted 22 July 2015 - 03:29 PM
This post has been edited by ChrispyChris: 22 July 2015 - 03:49 PM
#60
Re: My Questions C++
Posted 22 July 2015 - 03:54 PM
When I am coding and I start a parenthesis in Dev-C++, it automatically places the closing parenthesis, but when I'm done and want to move past it if I input one myself, it doesn't overwrite, it adds another closer. Is there a key I am supposed to use to move on coding, or is the arrow key the only one that works?

New Topic/Question
Reply




MultiQuote




|