954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Return a private member from a NOT friend class

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

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 functionmust return savings[pos].customer. Otherwise it should return a blank string "".

For convinience, heres is the code:

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

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

I don't see a problem with that code. It is legal and ok for a class to return a string like that.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

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

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

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 ;)

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

I dont see a problem too..As long as u returned the rite type..

cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

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;)

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

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 :

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: Account.h
Account.cpp
Bank.h
Bank.cpp

If there is anything else you might need in order to figure out the working let me know.

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

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

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 
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.

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

try returning ' ' instead of " ", don't know why but something tells me it might work

chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 92
Solved Threads: 16
 

actually the problem is not in returning the data. The problem lies in Accessing private members of teh Account class by public member functions of the same class. For eg, getFirstName() in Account is unable to access customer, visual studio spits out a Access violation error stating that the memory could not be read. this is the problem i need help with.

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

Use the Call Stack window to work your way back up the call stack, looking for corrupted data being passed as a parameter to a function. If that fails, try setting a breakpoint at a point before the location where the access violation occurs. Check to see if data is good at that point. If so, try stepping your way toward the location where the access violation occurred. If you can identify a single action, such as a menu command that led to the access violation, you can try another technique: set a breakpoint between the action (in this example, the menu command) and the access violation. You can then look at the state of your program during the moments leading up to the access violation.

You can use a combination of these techniques to work forward and backward until you have isolated the location where the access violation occurred. For more information, see Using the Call Stack Window.

http://msdn.microsoft.com/en-us/library/6decc55h(VS.80).aspx


So meh.. wants us to look at parameters first I take it

chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 92
Solved Threads: 16
 

If its just a problem with retreiving the private member, why not just create a public function within that class to grab the private value and return it:

int getPrivateThingy()
{
    return privateThingy;
}
chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 92
Solved Threads: 16
 

Actually the call stack is not able to diagnose it either!! here's a screen shot of the error:

Error Screen

Please take a look to better understand the problem i am facing.

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

If its just a problem with retreiving the private member, why not just create a public function within that class to grab the private value and return it:

int getPrivateThingy()
{
    return privateThingy;
}


:D This is the problem where i am stuck! it is not returning the privateThingy!! LOL

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

Well thats not very nice of it then :<

What class is getData function in and what does it look like?

chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 92
Solved Threads: 16
 

Also, whats the value of strlen(customer) at that point? Make sure its not falling off the array (just as an extra thing to look at possibly).

chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 92
Solved Threads: 16
 
Also, whats the value of strlen(customer) at that point? Make sure its not falling off the array (just as an extra thing to look at possibly).

Finally you get it! LOL! thanks bud! well the thing is like this. When the function is called, the getFirstName() is looking at the following line:

savings[pos].getFirstName(st);


The problem is, I am unable to get why I am not getting the value equal to savings[8].getFirstName(st) as the pos is 8. Can you help on that?

As a final help, i'm attaching the main that I am using with my program: a34main.cpp

I am sorry to ask for help, but my professor says that its against the college policy to help Before the due date! DUMB!!!

Anyway, please try to see if I have screwed up the function, or have screwed something else!

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

Storm in a teacap. Totally absurd code and its "rationale":

Account data = new Account[arraySize];
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.


Are you sure that an even number of errors helps you to write correct codes? You have run-time errors. Why you are trying to play with program syntax in that case? Access violations do not bear a relation to a private or public member access rules. Have you ever seen the Debug menu choice?

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You