Return a private member from a NOT friend class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 106
Reputation: sid78669 is an unknown quantity at this point 
Solved Threads: 4
sid78669's Avatar
sid78669 sid78669 is offline Offline
Junior Poster

Return a private member from a NOT friend class

 
0
  #1
Nov 30th, 2008
This one is really crazy cause i think my professor screwed it up. I have to write a class Bank. It contains objects savings of class Account. now say i have a savings array and a savings object has

  1. Account savings = new Account[5];
  2.  
  3. //How do i return the following:
  4. savings[0].customer;

The problem is that customer is a private member of class Account. The text says my function must return savings[pos].customer. Otherwise it should return a blank string "".

For convinience, heres is the code:
  1.  
  2. const char * Bank::getCustomer(int pos) const{
  3. if (pos < size){
  4. return savings[pos].customer;
  5. } else {
  6. return "";
  7. }
  8. }
  9.  
  10. const char * Bank::getAccountNumber(int pos) const{
  11. if (pos < size){
  12. return savings[pos].accountNumber;
  13. } else {
  14. return "";
  15. }
  16. }
  17.  
  18. int Bank::getBalance(int pos) const{
  19. if (pos < size){
  20. return savings[pos].balance;
  21. } else {
  22. return 0;
  23. }
  24. }

For people who would like to go through the code:
Bank Class Header and Definition

Account Class Header and Definition
Last edited by Ancient Dragon; Nov 30th, 2008 at 2:02 am. Reason: corrected code tags -- there is no such code tag as c++
Live free.... Die Hard!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Return a private member from a NOT friend class

 
0
  #2
Nov 30th, 2008
I don't see a problem with that code. It is legal and ok for a class to return a string like that.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 106
Reputation: sid78669 is an unknown quantity at this point 
Solved Threads: 4
sid78669's Avatar
sid78669 sid78669 is offline Offline
Junior Poster

Re: Return a private member from a NOT friend class

 
0
  #3
Nov 30th, 2008
I know that but unfortunately it is nto compiling. I get error everytime stating that the customer, accountNumber and balance are private members of Account Class. Any suggestions? You may want to take a look at those links with the complete codes to better understand the working of the program.
Live free.... Die Hard!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Return a private member from a NOT friend class

 
0
  #4
Nov 30th, 2008
It sounds as if you need to set up some accessor/modifier methods within your account class, rather than attempting to access them directly from your bank member functions. private data members of a class can only be directly accessed from other members of that same class
Last edited by Bench; Nov 30th, 2008 at 7:57 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Return a private member from a NOT friend class

 
0
  #5
Nov 30th, 2008
Alas, your attempts to get direct access to the class Account private members display only your C++ and OOP knowledge (and programming skill level ). The Account class designer declared public interface to ordinar class clients. Nobody should try to get more: that's class designer decision. Do you want to get customer data? Use getCustomer() member function. It returns a const pointer to C-string. Is it customer member value? Mind your own business, it's my private member - said class Account designer...

May be it helps you to understand one of the most fundamental OOP concepts - incapsulation
Last edited by ArkM; Nov 30th, 2008 at 1:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Return a private member from a NOT friend class

 
0
  #6
Nov 30th, 2008
I dont see a problem too..As long as u returned the rite type..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 106
Reputation: sid78669 is an unknown quantity at this point 
Solved Threads: 4
sid78669's Avatar
sid78669 sid78669 is offline Offline
Junior Poster

Re: Return a private member from a NOT friend class

 
0
  #7
Nov 30th, 2008
I agree with the last two posts. There are functions in the Account function that return the data but i was confused if i could use them, as I told before, it was to return savings[pos].balance, etc. now i will be returning savings[pos].getBalance(), etc. I just hope he doesn't deduct marks for that! LOL
Live free.... Die Hard!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 106
Reputation: sid78669 is an unknown quantity at this point 
Solved Threads: 4
sid78669's Avatar
sid78669 sid78669 is offline Offline
Junior Poster

Re: Return a private member from a NOT friend class

 
0
  #8
Dec 3rd, 2008
I am having this problem again but in a NEW way. when i try to access savings[pos].customer or run savings[pos].getFirstName(st), i was not able to read anything! thus i created a :
  1. Account data = new Account[arraySize];
  2. savings = &data;

This did not work as i got the following message:
Error 1 error C2440: 'initializing' : cannot convert from 'Account *' to 'Account' c:\users\siddharth\documents\visual studio 2008\projects\oop3\oop3\bank.cpp 82 oop3

I have no idea why it does not read savings[pos].
The code was also modified from the last time, the new codes are:
If there is anything else you might need in order to figure out the working let me know.
Live free.... Die Hard!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Return a private member from a NOT friend class

 
0
  #9
Dec 3rd, 2008
I dont know why you are assigning the address or memory location of data to savings whereas your variable data is not a pointer, I think the problem in your code is a mismatch of the data types you are trying to assign
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 106
Reputation: sid78669 is an unknown quantity at this point 
Solved Threads: 4
sid78669's Avatar
sid78669 sid78669 is offline Offline
Junior Poster

Re: Return a private member from a NOT friend class

 
0
  #10
Dec 3rd, 2008
Originally Posted by Traicey View Post
I dont know why you are assigning the address or memory location of data to savings whereas your variable data is not a pointer, I think the problem in your code is a mismatch of the data types you are trying to assign
I tried to do that as a final resort because even public functioncs of the Account class are giving errors while reading into the customer, private member of Account. I get a Access violation saying the memory could not be read. Any suggestions? It seems like my Array, savings is not working properly.
Live free.... Die Hard!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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