Passing an object into another

checkerboard's Avatar, Join Date: Feb 2007
Newbie Member
When we pass an object (say a) of Class A to another object (say b) of class B, what is the benefit of that. I understand that b now has access to the public functions of a.

Therefore, say I need to calculate a value of b using some values calculated by functions in a.

1) I can either, in Main, calculate the values from a and pass it as arguments into the function of b OR
2) I can access the function of a within the function of b to calculate the values i need.

I do not really see that why the second one should be preferred if indeed it is. The first can be achieved without passing one object into another.

Any particular reasons why passing objects into another object is superior. All it essentially save is the number of variables we pass into functions and some space on Main.

Pls advise.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
1. Because it is needed. Dont quote on me but what you are saying is a special scenario where just some values are needed but what if the calculation of A is dependent on the settings that B has.

2. Thats better way to organize the code. Say I call the A's function 3 times then I need to calculate the b's value 3 times unnecessarily. You can quote me on this :P