Quote:
but then 28-31 shows that the inline methods set the so-called objects to what Location equals, as if they were seperate variables, and not objects.
I think you must be talking about these lines:
Code:
33: void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
34: void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
35: void SetUpperRight(Point Location) {itsUpperRight = Location;}
36: void SetLowerRight(Point Location) {itsLowerRight = Location;}
You can do the following, right?
Code:
int n = 10;
int y = 30;
n=y;
What value does n have stored in it?
You can do the same thing with Point objects and Point variables. When you write:
Point p;
that innocuous looking statement sets off a whole chain of events. First a variable named p is created. Then the default constructor for the Point class is called to create a Point object. Finally, the Point object is stored in the variable p. Try changing the default constructor in Point to this:
Code:
Point()
{
cout<<"Default constructor called."<<endl;
}
and then in main() write:
Point p;
and see what happens.
I think what is confusing you is the fact that there are no "object literals" in C++. With int's, there are int variables, like:
int n;
and "int literals" like 10, 23, 0. You assign an int literal to an int variable like so:
int n = 10;
int's like 10, 23, 0 are called "int literals" to distinguish them from int variables(which vary or change). In C++, there are also char variables which can be assigned "char literals" like 'a', string variables which can be assigned string literals like "hello world", and double variables which can be assigned double literals like 10.5. However, in C++ there are no object literals. You can't write:
Point p = {int x = 0, int y = 0};
You can only write:
Point p;
and then C++ does the rest: it creates the object using the default constructor and then stores the object in the variable. You can also write:
Point c(10, 20);
then the constructor that takes two ints as arguments in the Point class is called, an object is created, and the object is stored in c. Next, if you write this:
p = c;
A copy of the Point object stored in the variable c will be stored in the variable p.
Quote:
Lines 28-31 is not how I learned that methods can be made.
The absolute simplest way to define a method in a class is like this:
Code:
class Point
{
public:
int getX()
{
return x;
}
private:
int x;
int y;
};
There is no simpler way, and to start out that is the way it is usually taught. You can also write that with a different format:
Code:
class Point
{
public:
int getX() {return x;}
private:
int x;
int y;
};
The only difference is that a lot of whitespace was removed.
Quote:
I understand those are inline methods, but it appears it's being declared as a Point type, which, I don't know why, or how, or what it's supposed to do.. It just isn't explained.
In the Rectangle class you have this:
Code:
class Rectangle
{
....
Point GetUpperLeft() const { return itsUpperLeft; }
...
};
You can rearrange that line to look like this:
Code:
Point GetUpperLeft() const
{
return itsUpperLeft;
}
All functions must declare their return type, the function name, and any parameters the function has(this one also has a const modifier after it, but the explanation of that is a more advanced topic) . This function declares that it will return a value that is of type Point. Since the Point class was defined earlier, Point is now a type, just like int and double. Practically, what that means is that the function is going to return a Point object when you call it. Therefore, when you call the function you will typically write something like this:
Code:
Point p = myRectangle.GetUpperLeft();
The variable p will store the Point object returned by GetUpperLeft(). p is a variable, just like any other variable, so you can change its value at any time. For instance, later you could write:
Quote:
46-49 appears to be making objects of the Point class
45: private:
46: Point itsUpperLeft;
47: Point itsUpperRight;
48: Point itsLowerLeft;
49: Point itsLowerRight;
50: int itsTop;
51: int itsLeft;
52: int itsBottom;
53: int itsRight;
No, that isn't correct. Nothing is created. All those lines say is that if you create a Rectangle object, then it will have Point objects named itsUpperLeft, etc. inside of it. I assume you have studied arrays by this point. An array can only store things that are one type inside of it, like int's or doubles. All the elements that you put into an array have to be the same type: the type that was specified when the array was declared. An object of a class is like an array in that it can store data inside of it. But unlike an array, an object of a class can store different types of things in it. For instance, an object of a class can store integers like itsTop and itsLeft--but it can also store things of type Point, i.e. Point objects, inside of it.
Quote:
I understand what you're doing with those classes, 7stud, that makes sense. The p object was created inside of the Data class and is being used by it to access the p's members.
Then try rereading Note (5). Try the example and understand how it works. That is something that will repeatedly trip you up when programming with C++ no matter how experienced you are. Experienced programmers just understand the error message immediately when it happens and make the proper corrections to their code.
Quote:
I can't imagine this much work into a file to not explain something so thoroughly. -
I started learning C++ with that book(many years ago), and after 1 week I threw it in the trash because I was so frustrated by its poor explanations. People say current editions are better, but the online stuff is probably pretty old. I switched to Ivor Horton's Beginning C++, and I thought it was excellent. There are no holes in the explanations in that book(although I would suggest skipping the overly complex section in chapter 3 on bitwise operators--nothing in the rest of the book builds on that).
One book that's highly recommended is "Accelerated C++" which takes a much different approach to C++ than any other book. It's also fairly short, so it doesn't require a huge investement in time, like most C++ books do.