My Questions C++

  • (5 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5

67 Replies - 6141 Views - Last Post: 30 July 2015 - 10:03 PM Rate Topic: -----

#46 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: My Questions C++

Posted 17 July 2015 - 04:13 PM

Oh sweet, I didn't know I had to close each of the individual tabs and then click on the project icon and compile that. It is working now, thank you for guiding me in the right direction. I appreciate it.

If you include a bunch of header files you don't necessarily need, wouldn't that slow down your code overall?
Was This Post Helpful? 0
  • +
  • -

#47 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: My Questions C++

Posted 17 July 2015 - 08:30 PM

Assuming non-malicious, well written header files, no it won't. All it will do is slowdown your compile time, but the run time of your program will stay the same. This is because ((most) modern) linkers are smart enough to only put the code and data that actually gets used into the generated executable.
Was This Post Helpful? 1
  • +
  • -

#48 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: My Questions C++

Posted 20 July 2015 - 10:29 AM

Ahh, thank you very much. That is some new knowledge to me, much appreciated.
Was This Post Helpful? 0
  • +
  • -

#49 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: My Questions C++

Posted 21 July 2015 - 10:06 AM

Another question: from what I've been reading, it seems that in my class I cannot have a const getter function, unless my value that the function is returning is a const as well. I thought it was good practice to make getter functions const because it emphasizes the fact that nothing will be done with the variable. Is this true, or is this a mistake on my part to think like that?

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!
Was This Post Helpful? 0
  • +
  • -

#50 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: My Questions C++

Posted 21 July 2015 - 10:13 AM

Quote

I thought it was good practice to make getter functions const because it emphasizes the fact that nothing will be done with the variable.

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.
Was This Post Helpful? 0
  • +
  • -

#51 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: My Questions C++

Posted 21 July 2015 - 10:36 AM

Well I'm talking about the actual function in the class that returns the value. This is why I am getting at where I 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.


Was This Post Helpful? 0
  • +
  • -

#52 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: My Questions C++

Posted 21 July 2015 - 10:40 AM

View PostChrispyChris, on 21 July 2015 - 07:06 PM, said:

Another question: from what I've been reading, it seems that in my class I cannot have a const getter function, unless my value that the function is returning is a const as well.


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

I thought it was good practice to make getter functions const because it emphasizes the fact that nothing will be done with the variable. Is this true, or is this a mistake on my part to think like that?


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

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.


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;`.

View Postmodi123_1, on 21 July 2015 - 07:13 PM, said:

CONST is used to detail if a variable is modifiable or not... not a function or property.


That's wrong. There are const member functions in C++.
Was This Post Helpful? 1
  • +
  • -

#53 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: My Questions C++

Posted 21 July 2015 - 11:08 AM

Thank you! I think I finally get it. The reason the code wouldn't work on the page that was displayed is because even if you AREN'T returning a reference, you still can't return a non const value if the value passed in is const, correct? It would be better to make all getters const because you can return both const and non const, UNLESS you plan on returning a non const reference, in which case you could either overload the function or leave out the const function altogether if you didn't need it.

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 :)!
Was This Post Helpful? 0
  • +
  • -

#54 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: My Questions C++

Posted 21 July 2015 - 11:58 AM

View PostChrispyChris, on 21 July 2015 - 08:08 PM, said:

The reason the code wouldn't work on the page that was displayed is because even if you AREN'T returning a reference, you still can't return a non const value if the value passed in is const, correct?


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.
Was This Post Helpful? 0
  • +
  • -

#55 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: My Questions C++

Posted 21 July 2015 - 12:10 PM

I think I messed up what I was trying to say. Let's say I have a non const getter function and I make a const object, the object could NOT call on the function because the function isn't const. Is that right? I'm not talking about references or pointers in this exact instance.

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!
Was This Post Helpful? 0
  • +
  • -

#56 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: My Questions C++

Posted 21 July 2015 - 12:13 PM

View PostChrispyChris, on 21 July 2015 - 09:10 PM, said:

I think I messed up what I was trying to say. Let's say I have a non const getter function and I make a const object, the object could NOT call on the function because the function isn't const. Is that right?


Yes, that's right.
Was This Post Helpful? 1
  • +
  • -

#57 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

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

Was This Post Helpful? 0
  • +
  • -

#58 Vaypur   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 53
  • Joined: 09-October 13

Re: My Questions C++

Posted 22 July 2015 - 01:14 PM

View PostChrispyChris, on 22 July 2015 - 10:03 AM, said:

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!


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

Was This Post Helpful? 0
  • +
  • -

#59 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: My Questions C++

Posted 22 July 2015 - 03:29 PM

Fixed. Thanks!

This post has been edited by ChrispyChris: 22 July 2015 - 03:49 PM

Was This Post Helpful? 0
  • +
  • -

#60 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: My Questions C++

Posted 22 July 2015 - 03:54 PM

This is a compiler question, but it relates to programming and my abundance of problems, so I'm sure someone here is smart enough to help me out.

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?
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5