Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 27th, 2005, 7:54 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
Question about passing objects into member functions

Let's assume I have the following code with the data members having relevant data.

class book {
  int price;
  char title[21];

  public:
  int comparePrice(book);
};

int main() {

  book a1, b1; //assume a constructor set the variables to something

  a1.comparePrice(b1);
 
  return 0;
}

int book::comparePrice(book z) //returns 0 or 1
{
   int rv;  
   if (price < z.price)
       rv = 0;
   else
      rv = 1;

  return rv;
}

My question is since comparePrice is a member function I know it can access the object's price directly but doesn't the member function also become associated with a particular object of the class? What I mean is, how come my comparePrice() function call can access the private member of the passed book object if the function is a member of the a1 object?

I hope the question is clear.

This was an example from my class but I didn't have time to ask that question .
aznluvsmc is offline   Reply With Quote
Old Sep 27th, 2005, 8:00 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
a1.comparePrice uses the a1 price; you just told it to. It compares it against the b1 price because you explicitly told it to (z.price, where z = b1).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 27th, 2005, 8:03 PM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Don't think of public/private on an object to object basis as much as a class to class basis.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Sep 27th, 2005, 8:10 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Ahhhhh, I DID misunderstand the question.... The method gets passed an invisible pointer to the actual object; that's how it KNOWS which collection of members to use. Or, to put it another way, that's how it KNOWS which object it belongs to (and thus has access to).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 27th, 2005, 8:32 PM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
I believe what he is asking is, summarized:

"If a member is private in class X, then why can instance Y access said private member on instance Z?"
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Sep 27th, 2005, 9:02 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Yeah, I finally got it. You said the answer. I don't believe I could phrase it better. Private members can be accessed from inside the bodies of methods of the class.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 27th, 2005, 11:29 PM   #7
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
So essentially what you're saying is that the member functions of a class can access the private data member of another object of that class if the object is passed into the function. They are associated only at the class level and not necessarily at the object level. Is that right?
aznluvsmc is offline   Reply With Quote
Old Sep 28th, 2005, 12:41 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The method is a member of the class. Each instantiation is an object. The method is sicced on the problem with a note in 'is hand about who he's working for, and a key to their house.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC