GP32 Could Somebody Explain These C++ Concepts To Me?


Here's how the author explained it to me in an email:
Randy Davis via Email said:
int nValue;

declares an integer. The following:

int* pValue;

declares a pointer to an integer. As the declaration states "pValue" is of type "int*", i.e., pointer to an integer.

The value "*pValue" is the integer pointed at by "pValue". As in the following:

int nValue;
int* pValue = &nValue;

nValue = 1; // these two lines are semantically equivalent
*pValue = 1;
 
Last edited by a moderator:
Azure posted on Aug 3 2004 at 10:24 PM said:
Here's how the author explained it to me in an email:
Randy Davis via Email said:
int nValue;

declares an integer. The following:

int* pValue;

declares a pointer to an integer. As the declaration states "pValue" is of type "int*", i.e., pointer to an integer.

The value "*pValue" is the integer pointed at by "pValue". As in the following:

int nValue;
int* pValue = &nValue;

nValue = 1; // these two lines are semantically equivalent
*pValue = 1;
Does it make more sense now?

Also, it's pretty cool that he(or someone he appointed) took the time to reply. I am sure he gets a lot of email from people on his book.
 
Last edited by a moderator:
Dalto posted on Aug 3 2004 at 03:34 PM said:
Azure posted on Aug 3 2004 at 10:24 PM said:
Here's how the author explained it to me in an email:
Randy Davis via Email said:
int nValue;

declares an integer. The following:

int* pValue;

declares a pointer to an integer. As the declaration states "pValue" is of type "int*", i.e., pointer to an integer.

The value "*pValue" is the integer pointed at by "pValue". As in the following:

int nValue;
int* pValue = &nValue;

nValue = 1; // these two lines are semantically equivalent
*pValue = 1;
Does it make more sense now?

Also, it's pretty cool that he(or someone he appointed) took the time to reply. I am sure he gets a lot of email from people on his book.
Yeah, I think I understand it now. *pValue is an int because you make it "=" to an int, or something like that.

Anyways, more concepts ( :D ):

Note: Due to the fact that there is a limit on how many pictures can be posted in one post (I tried to post 18 :lol:), I was forced to cut and paste related pics into one big picture. You should still be differentiate the different pictures that make up the one big picture).

1.
construct1and2.jpg

(First pic is a pic of the entire code, the second pic is a pic of the constructor in that code)
Okay, what I don't understand is, why the heck do we need contructors? In the second pic, the constructor is Student() {blah}, right? Why can't you just get rid of the constructor and initialize those two variables to 0 in the declaration? With every example the book gave of constructors (Even the more complex ones), I always thought, why not just initialize them to the value that the constructor gives them in the declaration, allowing you to forget the whole constructor thing altogether?

2.
1234codeanderror.jpg

(First pic is the code, second pic is the next page showing the 1234 error)
Okay, I don't understand this one. Why does the 1234 thing happen? And then there's supposedly a fix to this code:
1234forcecodeandforce2.jpg

(Second pic is the continuation of the code from the first pic)
Okay, so, is this really a fix? Because it appears that now the user is forced to have the user ssid equal "1234," because of the previous error. Would it still work if the ssid was another number? Here's the output from the code:
1234forceoutput.jpg


3.
and1infoand1and12.jpg

(First pic is the intro before the code segment. I thought it might help if I included it. The second pic shows what I'm having trouble with, the third pic shows the rest of the code, including the part that I'm having trouble with)
Okay, with this one, what I don't understand is the "&." What's up with the function argument, "Student& refs?" I've never seen this kind of syntax. What does Student& mean? I've heard of &Student, but not Student&. Here's another pic with the "&," but this one uses it multiple times:
and2introand2and22.jpg

(Again, the first pic is the intro before the code segement, the second and third pics show what I'm having trouble with)
The "&" is here again. It's everywhere in this one. So, what's the deal with this notation?

4.
minus1and12.jpg

(First pic is the first part of the code, the second pic is the second part)
Okay, I think I get this but I just want to make sure. The reason why the null is placed at element 39 is because name[40] doesn't exist, right? If this is wrong, could you tell me why it's placed at 39?

5.
arrow.jpg

Okay, this one about linked lists kind of confuses me. So, in that code snippet, pL first points to the beginning of the list, right? But I don't understand how it points to the next entry in that one line of code. I don't understand how pL = pL->pNext means point to the next entry. Why isn't it pL = pNext?
(I read about the -> already, and I understood how it worked for instances and their datamembers, like s->gpa, would refer to the gpa in the instance of the class Student, s.)

6.
classrulesand2.jpg

(In the first pic, look at the paragraphg that beings with "For example," specifically the last sentence in that paragraph. In the second pic, look at the top parapragh, specifically the sentence that begins with "You could make.")
Okay, I don't understand what's up with this or where it originated from, but what's up with the idea that classes can't rely on other things (I think that's what the rule is)? Could someone first please tell me the exact rule? So, would you get a compile error if you try to compile a class that relies on something else? If not, why can't you just ignore it?

Thanks for actually taking the time to read my long, and probably irritating (I apologize for that) posts. I'm trying my best to figure these out on my own, but sometimes I just need your help sometimes :D

BTW, when could I call myself a C++ Programmer or when could I say that I know C++? When I finish a beginner book? When I finish an advanced book? One reason that I'm really motivated to learn C++ is because my brother (Who is fat, lazy, and stupid, and older than me. I hate him so much :angry: (Not just because he's fat, lazy, and stupid, but because of the things he does in regard to him being those 3)) says that I won't be able to lean C++. I don't know why he would say such a thing, considering I'm A LOT smarter than him, he still gets grades below B's, while I get straight A's, he also has "stupid" classes, I have the more advanced classes, and I even have the same level math as him and I got a better grade in it...I'm guessing he said that because he thinks it's really hard.

BTW(2), what would you consider is harder to learn, C++ (and other programming languages) or Photoshop? Which one is more of an accomplishment?
 

Attachments

  • construct1and2.jpg
    construct1and2.jpg
    58.4 KB · Views: 174
  • 1234codeanderror.jpg
    1234codeanderror.jpg
    71.7 KB · Views: 194
  • 1234forceoutput.jpg
    1234forceoutput.jpg
    14 KB · Views: 191
  • 1234forcecodeandforce2.jpg
    1234forcecodeandforce2.jpg
    59.8 KB · Views: 189
  • and1infoand1and12.jpg
    and1infoand1and12.jpg
    96.9 KB · Views: 180
  • and2introand2and22.jpg
    and2introand2and22.jpg
    98.4 KB · Views: 189
  • minus1and12.jpg
    minus1and12.jpg
    60.7 KB · Views: 173
  • arrow.jpg
    arrow.jpg
    37.6 KB · Views: 187
  • classrulesand2.jpg
    classrulesand2.jpg
    90.8 KB · Views: 188
  • construct1and2.jpg
    construct1and2.jpg
    58.4 KB · Views: 176
  • 1234codeanderror.jpg
    1234codeanderror.jpg
    71.7 KB · Views: 191
  • 1234forcecodeandforce2.jpg
    1234forcecodeandforce2.jpg
    59.8 KB · Views: 167
  • 1234forceoutput.jpg
    1234forceoutput.jpg
    14 KB · Views: 178
  • and1infoand1and12.jpg
    and1infoand1and12.jpg
    96.9 KB · Views: 172
  • minus1and12.jpg
    minus1and12.jpg
    60.7 KB · Views: 175
  • and2introand2and22.jpg
    and2introand2and22.jpg
    98.4 KB · Views: 175
  • arrow.jpg
    arrow.jpg
    37.6 KB · Views: 173
  • classrulesand2.jpg
    classrulesand2.jpg
    90.8 KB · Views: 187
  • construct1and2.jpg
    construct1and2.jpg
    58.4 KB · Views: 176
  • 1234codeanderror.jpg
    1234codeanderror.jpg
    71.7 KB · Views: 179
  • 1234forcecodeandforce2.jpg
    1234forcecodeandforce2.jpg
    59.8 KB · Views: 168
  • 1234forceoutput.jpg
    1234forceoutput.jpg
    14 KB · Views: 193
  • and1infoand1and12.jpg
    and1infoand1and12.jpg
    96.9 KB · Views: 182
  • minus1and12.jpg
    minus1and12.jpg
    60.7 KB · Views: 169
  • and2introand2and22.jpg
    and2introand2and22.jpg
    98.4 KB · Views: 173
  • arrow.jpg
    arrow.jpg
    37.6 KB · Views: 173
  • classrulesand2.jpg
    classrulesand2.jpg
    90.8 KB · Views: 172
Last edited by a moderator:
1. In this case you probably could, but you could not do that with number 2.

2. The 1234 thing...hmm I'm not sure I understand so I will all the points.

A. In the first example the StudentID is printed out twice because two sperate studentID's are created. One in the protected section and one in the constructor. The on in the protected section prints out 0 because nothing is passed to it's constructor. The second prints out 1234 because 1234 is passed into it's constructor via SSID. The reason the 1234 is immediately destroyed is because it's scope is limited to the constructor itself.

If you are not famliar with scope let me know and I can explain.

B. Yes, it is fixed. Before it creaded 2 studentID's, not it creates only 1. No, it isn't hardcoded. You could put any value in there. Even a variable.

3. OK, the first thing is that it is important to understand that the C++ compiler gernerally ignores whitespace. This means that:

Student& refS
is the same as:
Student &refS
is the same as:
Student&refS

Now, as for what the & means. In this case, it means pass by reference. When you pass a parameter to a function by reference it means that any changes you make to the paramater will also be made on the variable you passed in.

For example look at this piece of code.
Code:
/*
 * Pass by value.  The variable passed is cpied and will not be changed
 */
void Test1( int testVar )
{
	testVar=5;
}

/*
 * Pass by reference.  The variable passed is not copied so it will be changed
 */
void Test2( int &testVar )
{
	testVar=5;
}

/*
 * Pass by reference using pointers.  This does the same thing as test2 but
 * requires some additional thought on the part of the programmer to pass
 * an address in.
 */
void Test3( int *testVar )
{
	*testVar=5;
}

int main( void )
{
	int x;
	x=2;
	Test1(x);	// This is a pass by value so x is copied and therefor does not change
	cout << x;
	
	x=2;
	Test2(x);	// This is a pass by reference so x is not copied and does change;
	cout << x;
	
	x=2;
	Test3(&x);	// This is a pass by reference using pointers so x is not copied
      	// and does change. We need to pass the address of x using &x
      	// this is expecting a pointer.
	cout << x;
	
	return 0;
}

The output of this is 255. In the last two x is changed because of a pass by reference.

4. Your correct. name[40] does not exist., name[39] is the last element of the array. The author does this in case pName is longer than the size allocated for name. The null ensures that the string is terminated.

5. Fpr some reason I can't see the image here. However, I can guess what is happening. The author has defined an object that is a node in a linked list. The way a linked list works is that each node in the list knows where the next node is. So, pNext is just a pointer to the next node. The reason you do pL->pNext; is that you are referencing the pointer inside the node.

Putting it all togther pL=pl->pNext; changes the pointer pL to point to the next node in the list.

6. This is philosophical. The compiler does not care if you use a contructor for intialization or if you initialize the members later in the calling program.

However, this is one of the fundamental concepts of OO programming called encapsulation. I would strongly recommend you take the author's advice here.

BTW> You can call yourself a C++ programmer after you have written a C++ program of medium size.

BTW2> WOW...that's an interesting comparison. I would answer this is two parts.
1. It is not challenging to learn how to use photoshop. It is challenging to be talented digital photographer/graphic artist. Photoshop is just the tool. I know how to use photoshop just as well as a friend of mine. However, my friend can produce things with photoshop that I could never dream of.

2. That being said. Being a programming and being a graphic artist/image editor are VERY different things. I am not sure that one is really more challenging than the other. One requires strong logic skills and the other requires strong creative talents. They both require an understanding of computers.

Thats all I have for now. Let us know what we can clarify.
 
Still need help on 1 & 5.

1. Why can't you do what I said on 2? So, what's the point of constructors? I can't find a case where you can't just intialize them directly, without having to deal with putting them in a constructor.

5: What's the difference between pl = pNEXT and pl->pNEXT? I still don't understand the pl->pNEXT notation. I thought that -> was just used for pointers that deal with instances of classes and their data members. BTW, I fixed the pic.
 
Azure posted on Aug 4 2004 at 05:22 AM said:
Still need help on 1 & 5.

1. Why can't you do what I said on 2? So, what's the point of constructors? I can't find a case where you can't just intialize them directly, without having to deal with putting them in a constructor.

5: What's the difference between pl = pNEXT and pl->pNEXT? I still don't understand the pl->pNEXT notation. I thought that -> was just used for pointers that deal with instances of classes and their data members. BTW, I fixed the pic.
1. The author is only setting all the values to 0. At later points you can send in whatever you want as an argument and set up your values to what you want them to be.

5. I suck at linked lists. Ask generalnmx :).
 
Last edited by a moderator:
CrazyDesi posted on Aug 3 2004 at 11:11 PM said:
Azure posted on Aug 4 2004 at 05:22 AM said:
Still need help on 1 & 5.

1. Why can't you do what I said on 2? So, what's the point of constructors? I can't find a case where you can't just intialize them directly, without having to deal with putting them in a constructor.

5: What's the difference between pl = pNEXT and pl->pNEXT? I still don't understand the pl->pNEXT notation. I thought that -> was just used for pointers that deal with instances of classes and their data members. BTW, I fixed the pic.
1. The author is only setting all the values to 0. At later points you can send in whatever you want as an argument and set up your values to what you want them to be.

5. I suck at linked lists. Ask generalnmx :).
Why set whatever you want your values to be through a constructor when you can set them to what you wan without a constructor? I still don't see the point of constructors. It seems like in every case you can just initialize them to your desired value without a constructor.
 
Last edited by a moderator:
Azure posted on Aug 4 2004 at 06:22 AM said:
CrazyDesi posted on Aug 3 2004 at 11:11 PM said:
Azure posted on Aug 4 2004 at 05:22 AM said:
Still need help on 1 & 5.

1. Why can't you do what I said on 2? So, what's the point of constructors? I can't find a case where you can't just intialize them directly, without having to deal with putting them in a constructor.

5: What's the difference between pl = pNEXT and pl->pNEXT? I still don't understand the pl->pNEXT notation. I thought that -> was just used for pointers that deal with instances of classes and their data members. BTW, I fixed the pic.
1. The author is only setting all the values to 0. At later points you can send in whatever you want as an argument and set up your values to what you want them to be.

5. I suck at linked lists. Ask generalnmx :).
Why set whatever you want your values to be through a constructor when you can set them to what you wan without a constructor? I still don't see the point of constructors. It seems like in every case you can just initialize them to your desired value without a constructor.
imagine this senario:

You have a class that is a 2d position class. The only vars are float nX and float nY. When using this class you will not want every object to have the same values for these variables, so you would have a constructor that takes in the desired X and Y co-ordinates and sets nX and nY appropriately.

A better example is if you pass in values to a constructor that then has to manipulate the values before setting it's own variables. For example, if you had a class that stored all the vertices for a 3D object and you passed this object into the constructor of another class that was a bounding box for collision detection. The constructor would have quite a bit of code to calculate what values the bounding box co-ordinates should have from the passed in 3D object.

I realise that this may be a little too advanced at the moment, but I guess that my point is that although you do not need a constructor in the example you have shown, there are many situations where you will need one and it is just good practice to start learning about it as early as possible.
 
Last edited by a moderator:
Wait, so you would use constructors when you plan on having multiple instances of a class and you want their data members to be initialized to different values?
 
Azure posted on Aug 4 2004 at 09:02 AM said:
Wait, so you would use constructors when you plan on having multiple instances of a class and you want their data members to be initialized to different values?
Yes, that's one case. The other case would be that you want to initialize it to a value, but you do not know what that value will be in advance.

As for why you can't say pL=pNext

The most simple answer is that pNext only exists as a member of pL.

If you try to to pL=pNext the compiler will complain that pNext is undefined. pNext only exists as a component or member of pL.

I think the reason that this is confusing is because when you do this:

pL=pL->next;

You have pL on both sides of the equation. What this does in practice is makes pL point to the next node in the list.

Do you understand what a linked list is? Perhaps this is the problem.
 
Last edited by a moderator:
Oh, so pL is a class (Linkable class)?
BTW, how many lines of codes does a medium sized C++ program contain?
 
Azure posted on Aug 4 2004 at 09:36 PM said:
3000? :eek: Isn't that like the size of OS9XGP?
3000 isn't as big as it seems. The code adds up quick.

gpPaint is around 5000 and it was something I worked on part time for about 3 weeks.
 
Last edited by a moderator:
Okay, I still don't understand a few things.

andnospace.jpg

First, the whole "&" thing. You said that "Student& refS" is the same as "Student &refS," but, in this picture there's nothing after the Student&. So, how does this work then? What does it mean? It's also here:
andsuggest.jpg


I also saw this thing that confused me:
headpointer.jpg

Okay, I'm having a real hard time with linkable lists, and this just makes things worse. Read the last paragraph. Does that even make sense? He says head pointer is of type LinkableClass*, but then at the end he contradicts himself. I really don't understand linkable lists, there's too much there! So, WTF is a linkable list? I mean, is it a class, a class that's a pointer, anything that you want it to be? I see it in many different ways and they all confuse me. Could somebody give me an example of a linkable class and also show some code that navigates through the class's elements and code that adds an object to the head of the list (I don't understand this, if this adds an object to the head of the list, would the objects keep replacing themselves? It seems like this isn't how you would add items to a list, how would you add items to a list then?)?
 

Attachments

  • andnospace.jpg
    andnospace.jpg
    34.5 KB · Views: 169
  • andsuggest.jpg
    andsuggest.jpg
    46.4 KB · Views: 165
  • headpointer.jpg
    headpointer.jpg
    40.2 KB · Views: 157
  • andnospace.jpg
    andnospace.jpg
    34.5 KB · Views: 166
  • andsuggest.jpg
    andsuggest.jpg
    46.4 KB · Views: 168
  • headpointer.jpg
    headpointer.jpg
    40.2 KB · Views: 157
  • andsuggest.jpg
    andsuggest.jpg
    46.4 KB · Views: 175
  • andnospace.jpg
    andnospace.jpg
    34.5 KB · Views: 157
  • headpointer.jpg
    headpointer.jpg
    40.2 KB · Views: 165
1. It's a prototype. The argument names are optional in a prototype.

So if you have a function.

int AddOne( int x )
{
return x+1;
}

You can prototype it like this: int AddOne( int );

The purpose of a prototype is to allow the compiler to referenence functions that have not been defined yet.

2. OK, the last paragraph is an analogy and not a very demonstative one. He is actually trying to point where his analogy breaks down.

A linked list is a common data structure. Forgetting about C++ for a minute, let me try to explain what a linked list as an abstract concept.(You can have linked lists in any programming language)

A linked list is a collection of nodes. Each node has two things:
1. Data
2. A pointer to the next node in the list.

Let's say you have 4 nodes A,B,C and D they each point to one another. Like This:

A->B->C->D

A better of linked lists is here: http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
 
Azure posted on Aug 8 2004 at 02:32 AM said:
Could you also explain the difference between these two:
var1 = ++var1
var1 = var1++
Those are two things you should never do :)

But more seriously, the difference between ++var and var++ is that one increments before it is evaluated and one afterwords, respectively. Let me give an example.

Let's say take the function from above AddOne
Code:
int main( int argc, char** argv )
{
int a,b,c;

a=b=c=0;

a=AddOne(b++);
cout << a << " " << b << "\n";

a=AddOne(++c);
cout << a << " " << c << "\n";
return 0;
}
This will output:

1 1
2 1

When you ++c it adds on to c before it is passed into the function.

Make sense?
 
Last edited by a moderator:
Dalto posted on Aug 4 2004 at 02:32 PM said:
Azure posted on Aug 4 2004 at 09:25 PM said:
Oh, so pL is a class (Linkable class)?

Yes, pL is a class.
Wait, now that I think about it, shouldn't pL be an instance of a class? Isn't pL an object?
 
Last edited by a moderator:
Azure posted on Aug 8 2004 at 05:47 AM said:
Dalto posted on Aug 4 2004 at 02:32 PM said:
Azure posted on Aug 4 2004 at 09:25 PM said:
Oh, so pL is a class (Linkable class)?

Yes, pL is a class.
Wait, now that I think about it, shouldn't pL be an instance of a class? Isn't pL an object?
Yes, it is an object..
 
Last edited by a moderator:
Back
Top