简体   繁体   中英

How to compare two objects of the same class?

I am making a Bank Management system in c++. I have Visual Studio 2008. See the code below and I will explain my problem.

class ACCOUNT
{
int accno,deposit;
char name[20];
char type;
public:
int create_account()
{
    //The whole process of accepting details from user goes here/
}
int retacc() //Accessor Function
{
    return accno;
}
void write_account()
{
ACCOUNT acc1,acc2;
char ch='Y';
fstream rec;
rec.open("d://useless.dat",ios::out|ios::in|ios::app|ios::binary);
c:
acc1.create_account();
rec.seekg(0);
while(!rec.eof())
{
    rec.read((char*)&acc2,sizeof(acc2));
    if(acc1.retacc()==acc2.retacc()) //THIS IS WHERE I AM DOING THE COMPARING PART. IS THIS THE WRONG WAY TO DO IT?
    {
        cout<<"\nAccount No. already exist.";
        cout<<"Want to enter again? (Y/N) : ";
        cin>>ch;
        ch=toupper(ch);
        if(ch=='Y')
            goto c;
    }   
}
rec.write((char*)&acc1,sizeof(acc1));
rec.close();
cout<<"\nCongratulations! Your account is created.\n\n";
}

What I am trying to do here is when user enters account no., it is sent to be checked whether another record with same account no. is present in the file useless.dat or not. So I made two instances of the class. One for getting the data from user and one for reading the file. Then, Compare them and if there is some record which has same account no. then user have to enter another number and if not then the details given by the user is to be written in the file.

An approach you may want to consider is to create a static method for loading your data from a file (instead of having it be an instance method on the account object). Each account number corresponds to an Account object, so as you load the data make an Account object from each of the account numbers. Store these Account objects in a data structure such as a Vector.

Then, to determine if an account number already exists you would simply iterate of over the vector, checking to see if the account number corresponds to an existing number. If it does, you reject the input and repeat until a valid account number is input.

That said, @asawyer is absolutely right that in his comment on the security issue. Are you sure the user should be providing their own account number? Typically, this would be handled by the bank and the user would just be assigned an account number when they create a user account. Again, this might be overkill for you assignment though, and can be ignored based on the guidelines your professor has provided for you.

EDIT: A bit of pseudo-code to help you understand

class Account
{
    Account(acctNumber);
    retacc();
    write_account();

    static load_accounts() {
        for each acctNumber A in file
            storageVector->push(A)
        end
    }
    static account_exists(Account account) {
        for each acctNumber A in storageVector {
             if  account->retacc() == A->retacc() {
                 return true
             }
        }
        return false;
    }
}

The general idea is to load all of the account numbers in and store them to the Vector container, then just iterate over that container to see if the value you're looking for already exists.

This is just psuedo-code, but it should be enough to give you an idea of how to proceed from here. Good luck!

You may declare operators in C++.

class ACCOUNT {
    ... 
    bool operator==(const ACCOUNT& other) const {
        return accno == other.accno; // whatever you want
    }   
}

After that you may use this operator== as you do for standard types.

You may see more about it here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM