I am new to both c and c++. A few weeks ago I started learning c++ using MIT's online courseware (class number 6.096). Everything was going great until I hit classes. Now I am really struggling (bad part about MIT's free course material, no TA's nor instructors to help).
Today I decided I was just going to program some classes myself and it was going great until I hit a brick wall. In my code below I defined a class called "Point". Then I tried to define a "Line" class that consists of two Points. The error section is outlined in comments below.
My guess is that the problem is very obvious to an experienced c++ programmer. My hope is I will understand the explanation.
In the class Point , the second constructor is considered as the default constructor because it does not necessary required any arguments because of the default parameters .
For the other errors , you tried to call the constructor in the class declaration not within a method member scope which is illegal except for static member that are const.
Thank you for your very thorough explanation. I am studying your code and explanation and going back to the lecture notes again.
In the class Point , the second constructor is considered as the default constructor because it does not necessary required any arguments because of the default parameters .
Yes, sadly I knew this. I threw this line in after hours of fighting the error, blindly hoping it might appease the compiler. This is when I know I am unlearning and have reached the point where I need to ask for help.
For the other errors , you tried to call the constructor in the class declaration not within a method member scope which is illegal except for static member that are const.
This part I need to go back and study more. Thank you again for pointing me in the right direction so I can get back on track.
OK, I re-read the section on "const member functions".
Is it safe to assume that every time a class method returns a variable value that it should be a const and when a method modifies variable values it should not be const?
*EDITS*
OK, I thought I was starting to understand this, but not there yet. In the "Additional Material" notes it says: "To declare a const member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the prototype and the definition."
In the lecture notes they are not always consistent with this. Here is a code snippit:
Just to add to AA's answer - that getStyle() very well could - and probably should - be defined as a const function, i.e.
const string &getStyle() const {return style;}.
The method doesn't modify the state of the object, and the reference it returns is const, so it can't be used to modify the state of the object either. So it would have been better to make the method const, in exactly the way you suggested.
Making methods const-correct is one of those things people often forget to do, even experienced professional developers. Usually, it's only when you (or someone else) first tries to use a const object of that type that you suddenly realise that you've forgotten to make some of your methods const - e.g.
1 2 3
const Car myPorche("1234567", 2001, "Sports car");
std::cout << "My car is a " << myPorche.getStyle(); // COMPILE ERROR - because we didn't
// make getStyle() a const method.
MikeyBoy, this has been clearly explained by everybody, but it took your explanation to really make it all clear to me. Maybe it just took repetition.
Let's make sure I got this concept...
Basically by making the method itself and the code "const" you are insuring that if somebody assigns the class to a constant rather than to a variable it won't throw a compiler error.
If the only thing the method is doing is returning a value (versus changing it), there is absolutely no downside to making it const.
I assume some professional programmers don't catch their mistake because they only assign the class to a variable when debugging. Therefore I should get in the habit now of always making "Get" methods const.
I hope this is right because a light bulb finally came on.