More than a customer

Hi, sorry for my English it's not my first language,
i'm working on a bank project for school,the project says :

"Use inheritance to modify your code to accept more than one account and
more than one costumer. A costumer can also have multiple accounts.
Hint: Think about a class called Account as the base class and a class called
Costumer as the derived class"

how to connect or link between a customer name and his account ?
any help would be appreciated.
Thank you.
Last edited on
Use inheritance to modify your code

That implies that you have already made a simpler program. What does it have/do?

A costumer can also have multiple accounts.
That should probably be something other than inheritance.

The most common form of inheritance would mean that a Costumer is an Account and can be treated as an account. That seems a bit odd, assuming the names of the classes are meaningful.


The linking depends on how the objects are managed.

For example, one could have two logical "tables" and "owner-id" column in each account record that links to unique id of customer.

On the other hand, one might have a list of customers, and each customer have its private list of accounts.
That implies that you have already made a simpler program. What does it have/do?


bank.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include <string>
using std::string;
class Bank
{
public:
    void NewAccount();
    void Withdraw();
    void Deposit();
    void Display();
private:
    string Name;
    double Balance;
};


#endif // BANK_H_INCLUDED 


Bank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;
#include<iomanip>
#include <string>
using std::string;
#include "bank.h"

void Bank::NewAccount()
{
    cout<<"Please Enter Your Name To Create An Account"<<endl;
    getline(cin,Name);
    Balance=0;
}

void Bank::Withdraw()
{
    double draw;
    cout<<"Please Enter The Amount To Withdraw : ";
    cin>>draw;
   if(draw>Balance)
        cout<<"Your account is overdrawn, please make a deposit";
    else{
        Balance-=draw;
        cout<<"Balance After Withdraw Will Be :"<<Balance<<" $";
        }
}

void Bank::Deposit()
{
double dep;
cout<<"Enter the amount to deposit : ";
cin>>dep;
Balance+=dep;
cout<<"Balance After Deposit Will Be :"<<Balance<<" $";

}

void Bank::Display()
{
    cout<<setw(40)<<"DETAILS"<<endl;
    cout<<setw(20)<<"Account Name : "<<Name<<endl;
    cout<<setw(20)<<"Balance : "<<Balance<<" $"<<endl;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
#include "bank.h"

int main()
{
    Bank obj;
    obj.NewAccount();
    int choice=1;
    while(choice !=0)
    {
        cout<<"\n\n Please Choose An Operation \n 1- Create New Account\n 2- Deposit\n 3- Withdraw\n 4- Show Account Details\n 0- Exit"<<endl;
        cin>>choice;
        switch(choice)
        {
            case 1 : obj.NewAccount();
            break;
            case 2 : obj.Deposit();
            break;
            case 3 : obj.Withdraw();
            break;
            case 4 : obj.Display();
            break;
            case 0 : cout<<"Thank You For Using Our Services, Have A Nice Day";
            break;
            default : cout<<"illegal Option";
        }
    }

    return 0;
}


A costumer can also have multiple accounts.
That should probably be something other than inheritance.

I can use an array, is it correct ?
Last edited on
As keskiverto said, this seems a really odd (read bad) way of designing the classes. Why? Customer and Account have nothing in common apart from a link (a Customer Has Accounts). In fact, the only common data is perhaps 'address' - both have addresses, and maybe a name although they are two different types of names. They are not good candidates for inheritance, and if you try to force it, you'll run into problem after problem, because it's illogical.

Inheritance is not generally used for a mere 'x has a y' relationships. It's more appropriate when it's a 'x is a type of y' relationship. I know this doesn't help with your assignment, but I'm surprised a teacher would suggest it.

What you would normally do in this case is create two completely separate classes - Customer and Account. Then, in Customer, you would maintain an array or vector of Accounts the customer has (its collection) eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "account.h"
class Customer {
    string name;
    string street;
    string suburb;
    int customerType; // eg. 1 - personal, 2 - corporate etc.
    Accounts accounts std::vector<Account>;
   //etc.
   public:
      getName(int customerId);
      getAccount(int accountNum);
  // etc.
}


The easiest way to remember it is to go back to the basic standard example:

Dog IS A TYPE of Animal = inheritance. Dog gets all Animals methods/properties, (eg. typeOfAnimal = mammal) and adds its own eg. Bark();.

Zoo HAS Animals = not inheritance, just a collection of Animals.

Any basic C++ book willl explain when it's best to use inheritance, and when it's best to just have class members that refer to 'HAS A' relationships, via an array or a vector.

Last edited on
Topic archived. No new replies allowed.