User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,669 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,605 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 33199 | Replies: 45
Reply
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,059
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

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

  #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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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

  #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  
Join Date: Apr 2004
Posts: 3,471
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
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

  #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.
Reply With Quote  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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

  #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  
Join Date: Apr 2004
Posts: 3,471
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
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

  #6  
Mar 9th, 2005
#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int result = multiply(2,3);
   printf("result = %d\n", result);
   return 0;
}

/* my output
result = 6
*/
#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int i, j;
   for (i = 1; i < 10; ++i)
   {
      for (j = 1; j < 10; ++j)
      {
         printf("%2d ", multiply(i, j));
      }
      putchar('\n');
   }
   return 0;
}

/* my output
 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 
*/
Reply With Quote  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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

  #7  
Mar 10th, 2005
Originally Posted by Dave Sinkula
#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int result = multiply(2,3);
   printf("result = %d\n", result);
   return 0;
}

/* my output
result = 6
*/



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
#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int i, j;
   for (i = 1; i < 10; ++i)
   {
      for (j = 1; j < 10; ++j)
      {
         printf("%2d ", multiply(i, j));
      }
      putchar('\n');
   }
   return 0;
}

/* my output
 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 
*/


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  
Join Date: Apr 2004
Posts: 3,471
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
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

  #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.
Reply With Quote  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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

  #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  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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

  #10  
Mar 10th, 2005
then what is call-by-reference???
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC