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 function must 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

Recommended Answers

All 21 Replies

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

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.

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

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

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

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

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:

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

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 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.

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

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.

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

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

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.

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

Well thats not very nice of it then :<

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

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

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!

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?

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

Account data = new Account[arraySize];

Sorry if I pissed you off, I dint mean it! anyway, as per the call stack and the watch variables, my savings is not functioning as a proper array. Only the first scanned value is stored in savings and that is the only data that is read. Therefore, savings[pos].... related functions are not functioning as there is NO array according to them. This is very much easier to tackle now that I know what is wrong! And i give complete credit to all those who posted here, you were the ones who pointed to me the problem might reside somewhere else. Thus, now i have to figure out how to make my a pointer to an array of Account objects work! LOL!!

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?[/QUOTE]

Thank You every buddy!!! As I stated above, the problem was in the savings array, and i have finally solved it!! what i did now was instead of creating a new:

Account * savings = new Account[arraySize];

I went:

savings = new Account[arraySize];

AND IT WORKED!! yepee!! and as ArkM said

Storm in a teacap

Thanks all!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.