C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #1
Mar 9th, 2005
In my old C++ programming classes I would often trip up on theses basic concepts when things got more and more complicated:

1. Arrays
2. Functions
3. Inheritance
4. Call by Reference
5. Call by Value
6. Pointers

I kind of know how they work but don't really know what they are good for in real life applications. Could somebody explain his or her:

(A) Definition of the concept
(B) Understanding of what the concept does/how it works
(B) Quick example of the concepts use in real life applications (i.e. what is it good for?)



I'll put my understandings and explanation up later tonight, tomorrow the latest
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #2
Mar 9th, 2005
Each of these are rather broad topics. Can you cut it down to specific questions so we don't have to write page after page of tutorials?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #3
Mar 9th, 2005
Originally Posted by Narue
Each of these are rather broad topics. Can you cut it down to specific questions so we don't have to write page after page of tutorials?

Okay, lets deal with functions first, then inheritance

lets start with the format of a function:

return-value-type function name( parameter-list)
{
declaractions and statements
}

^^^what does all that mean, and what does it mean whem you have nothing in the parameter-list compared to when you do have something(s) in the parameter-list?

that one always gets me
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #4
Mar 9th, 2005
return-value-type Any value, or none at all, that you want to return from the function: an error code, a calculated result, etc.

parameter-list Any value(s) -- or possibly references in C++, or none at all, that you want to pass to the function: a pointer to an array to calculate the sum, for example.

declaractions and statements The "meat"; what it takes to do whatever it is the function does.

what does it mean whem you have nothing in the parameter-list compared to when you do have something(s) in the parameter-list?

It means whatever you want it to mean. This is a little like asking how a painter turns paints and canvas into a portrait.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #5
Mar 9th, 2005
Originally Posted by Dave Sinkula
parameter-list Any value(s) -- or possibly references in C++, or none at all, that you want to pass to the function

okay you touched on something there! what do you mean by "pass to the function"

explain with an example please
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #6
Mar 9th, 2005
  1. #include <stdio.h>
  2.  
  3. int multiply(int x, int y)
  4. {
  5. return x * y;
  6. }
  7.  
  8. int main()
  9. {
  10. int result = multiply(2,3);
  11. printf("result = %d\n", result);
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. result = 6
  17. */
  1. #include <stdio.h>
  2.  
  3. int multiply(int x, int y)
  4. {
  5. return x * y;
  6. }
  7.  
  8. int main()
  9. {
  10. int i, j;
  11. for (i = 1; i < 10; ++i)
  12. {
  13. for (j = 1; j < 10; ++j)
  14. {
  15. printf("%2d ", multiply(i, j));
  16. }
  17. putchar('\n');
  18. }
  19. return 0;
  20. }
  21.  
  22. /* my output
  23.  1 2 3 4 5 6 7 8 9
  24.  2 4 6 8 10 12 14 16 18
  25.  3 6 9 12 15 18 21 24 27
  26.  4 8 12 16 20 24 28 32 36
  27.  5 10 15 20 25 30 35 40 45
  28.  6 12 18 24 30 36 42 48 54
  29.  7 14 21 28 35 42 49 56 63
  30.  8 16 24 32 40 48 56 64 72
  31.  9 18 27 36 45 54 63 72 81
  32. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #7
Mar 10th, 2005
Originally Posted by Dave Sinkula
  1. #include <stdio.h>
  2.  
  3. int multiply(int x, int y)
  4. {
  5. return x * y;
  6. }
  7.  
  8. int main()
  9. {
  10. int result = multiply(2,3);
  11. printf("result = %d\n", result);
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. result = 6
  17. */


hmmm...thanx for the break down...ok so did you pass parameters x and y in function multiply() to function multiply() found in the function main()? am i right or wrong?

and what is this "%d", what does it do?

please explain whats going on in the printf() function


Originally Posted by Dave Sinkula
  1. #include <stdio.h>
  2.  
  3. int multiply(int x, int y)
  4. {
  5. return x * y;
  6. }
  7.  
  8. int main()
  9. {
  10. int i, j;
  11. for (i = 1; i < 10; ++i)
  12. {
  13. for (j = 1; j < 10; ++j)
  14. {
  15. printf("%2d ", multiply(i, j));
  16. }
  17. putchar('\n');
  18. }
  19. return 0;
  20. }
  21.  
  22. /* my output
  23.  1 2 3 4 5 6 7 8 9
  24.  2 4 6 8 10 12 14 16 18
  25.  3 6 9 12 15 18 21 24 27
  26.  4 8 12 16 20 24 28 32 36
  27.  5 10 15 20 25 30 35 40 45
  28.  6 12 18 24 30 36 42 48 54
  29.  7 14 21 28 35 42 49 56 63
  30.  8 16 24 32 40 48 56 64 72
  31.  9 18 27 36 45 54 63 72 81
  32. */

Hmmmmm....in the function multiply() how did you go from parameters x and y to i and j, that looks like a "call-by-reference" but i don't understand why you did that and how it works, please explain dave

and what does the function putchar() mean
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #8
Mar 10th, 2005
Originally Posted by Fasola
hmmm...thanx for the break down...ok so did you pass parameters x and y in function multiply() to function multiply() found in the function main()? am i right or wrong?
In the first example, the values 2 and 3 from the call to multiply in main() become the values of the parameters x and y in the function multiply().

Originally Posted by Fasola
and what is this "%d", what does it do?

please explain whats going on in the printf() function
It is just printing an integer, which was the result returned by the function.

Originally Posted by Fasola
Hmmmmm....in the function multiply() how did you go from parameters x and y to i and j
In the second example, the values of i and j in main() are passed to the function multiply() as the parameters x and y.

Originally Posted by Fasola
that looks like a "call-by-reference" but i don't understand why you did that and how it works, please explain dave
Nope. Pass by value. A copy of the value of i, for instance, is passed to the function as the parameter x.

Originally Posted by Fasola
and what does the function putchar() mean
It puts a char to the stdout.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #9
Mar 10th, 2005
Originally Posted by Dave Sinkula
In the first example, the values 2 and 3 from the call to multiply in main() become the values of the parameters x and y in the function multiply().

In the second example, the values of i and j in main() are passed to the function multiply() as the parameters x and y.

i noticed how you said "become the value"


let me make sure i got that right

does the funtion

multiply(int x, int y)

CALL THE VALUES 2 and 3 (i.e. call-by-value")

of the function

multiply(2,3);

to perform

return x * y;


i'm not sure, is that what you meant by "become"?
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #10
Mar 10th, 2005
then what is call-by-reference???
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC