| | |
Return a private member from a NOT friend class
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
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:
For people who would like to go through the code:
Bank Class Header and Definition
Account Class Header and Definition
C++ Syntax (Toggle Plain Text)
Account savings = new Account[5]; //How do i return the following: 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:
C++ Syntax (Toggle Plain Text)
const char * Bank::getCustomer(int pos) const{ if (pos < size){ return savings[pos].customer; } else { return ""; } } const char * Bank::getAccountNumber(int pos) const{ if (pos < size){ return savings[pos].accountNumber; } else { return ""; } } int Bank::getBalance(int pos) const{ if (pos < size){ return savings[pos].balance; } else { return 0; } }
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!
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!
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? 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
). 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.
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!
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 :
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.
C++ Syntax (Toggle Plain Text)
Account data = new Account[arraySize]; 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!
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.
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!
![]() |
Similar Threads
- Help with Friend Functions (C++)
- stuck on a member function for a class (C++)
- Passing Objects (C++)
- Question about private scope (C++)
- C++ que (C++)
- friend functions (C++)
- Help me for friend classes and using templates (C++)
- Please I Need U Again (C++)
- Simple Class Question (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: How can I use this code repeatedly?
- Next Thread: solving a quadratic equation
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






