I have attached a quick class model of what I am trying to achieve (Also, sorry for the mass of code but I figured I'd post everything I have thus far in case anyone saw other improvements I could make).

I can give a customer an 'Account' but I want to give the customer a 'CurrentAccount' which is inherited from Account; and then store it in an array of Accounts.

How do I go about doing this?
Thank you for any help!

Customer.cpp

#include "Customer.h"
#include <iostream>
#include <string>
#include "Date.h"

using namespace std;


Customer::Customer()
{
    noOfAccounts = 0;
}

int Customer::getID()
{
    return ID;
}

string Customer::getName()
{
    return name;
}

string Customer::getAddress()
{
    return address;
}

string Customer::getTelNo()
{
    return telNo;
}

string Customer::getSex()
{
    return sex;
}

int Customer::getAge()
{
    return age;
}

Account Customer::getAccount()
{
    return *accRef;
}

void Customer::setID(int ID)
{
    this->ID = ID;
}

void Customer::setName(string name)
{
    this->name = name;
}

void Customer::setAddress(string address)
{
    this->address = address;
}

void Customer::setTelNo(string telNo)
{
    this->telNo = telNo;
}

void Customer::setSex(string sex)
{
    this->sex = sex;
}

void Customer::setAge(int age)
{
    this->age = age;
}

void Customer::setAccount(Account *accRef)
{
    this->accRef = accRef;
}

void Customer::addNewCustomer()
{
    cout << "Enter your name > ";
    getline(cin,name);
    cout << "Enter your Address: ";      
    getline(cin,address);
    cout << "Enter your telephone number: ";  
    getline(cin,telNo);
    cout << "Enter your sex: ";    
    getline(cin,sex);
    cout << "Enter your Age: ";   
    cin >> age;
}

void Customer::addNewAccount()
{
    acnts[noOfAccounts].createAccount();
    noOfAccounts++;
}


void Customer::showPersonDetails()
{
    cout << ID << ": " << name << " - " << address;
}

void Customer::showPersonAccounts()
{
    cout << "Accounts<" << noOfAccounts << ">:" << endl;

    if (noOfAccounts == 0)
    {
        cout << "<None>" << endl;
    }

    else
    {
        for (int i = 0; i < noOfAccounts; i++)
        {
            acnts[i].showAccount();
        }
    }
    cout << endl;
}

Account.cpp

#include "Account.h"

  Account::Account()   //create an account with an initial amountand a specified interest rate
  {
  } 

  double Account::balance(void) const                   //return the account's balance
  {
    return money;
  } 

  void Account::deposit(double amount)             //add money to the account
  {
    money += amount;
  } 

  void Account::createAccount()
  {
        cout << "Enter Initial Balance > ";
        cin >> money;
  }

  void Account::showAccount()
  {
      cout << "Amount: \x9C" << money <<  endl;
  }

CurrentAccount.cpp

#include "currentAccount.h"

  CurrentAccount::CurrentAccount()   
  {
  }

  void CurrentAccount::setInterest(double percent)
  {
      this->interestRate = percent;
  }

 void CurrentAccount::addInterest(void)                   
  {
        money *= (1 + interestRate/100.0);
  } 

   void CurrentAccount::createAccount()
  {
        cout << "Enter Initial Balance > ";
        cin >> money;
        cout << "enter Interest rate > ";
        cin >> interestRate;
  }

  void CurrentAccount::showAccount()
  {
      cout << "Amount: \x9C" << money << " at " << interestRate << "% interest" << endl;
  }

main.cpp

#include "Account.h"   
#include "Cheque.h"
#include <limits>
#include "Customer.h"
#include <ctime>
#include  <conio.h>

int noOfCustomers = 0;
Customer cust[10];


void choosingAccount(int ID)
{
    char choice;
    bool active = true;

    while (active)
    {
        cout << "\t\n" << "1. Current Account" << endl;
        cout << "\t\n" << "2. Junior Current Account" << endl;
        cout << "\t\n" << "3. Student Saving Account" << endl;
        cout << "\t\n" << "0. Back" << endl;

        switch(choice)
        {
            case '1':
                cust[ID-1].addNewAccount();
                break;
            case '2':
                cust[ID-1].addNewAccount();
                break;
            case '3':
                cust[ID-1].addNewAccount();
                break;
            case '40':
                active = !active;
                break;
            default:
                cout << "\nInvalid selection\n\n";
                break;
        }
    }
}



void specificCustomerMenu(int ID)
{
    char choice;
    bool active = true;

    while (active)
    {
        if ((ID > 0) && (ID <= noOfCustomers))
        {
            cout << "Main Menu >> Customers >> " << ID << endl;
            cout << "--------------------------------" << endl; 
            cout << cust[ID-1].getName() << endl;
            cout << cust[ID-1].getAge() << endl;
            cout << cust[ID-1].getTelNo() << endl;
            cout << cust[ID-1].getAddress() << endl;
            cout << "--------------------------------" << endl; 
            cust[ID-1].showPersonAccounts();
            cout << "--------------------------------" << endl; 
            cout << "Mortgage: " << endl;
            cout << "Loan: " << endl;
            cout << "Card: " << endl;
            cout << "--------------------------------" << endl; 
            cout << "\t\n" << "1. Deposit Money" ;
            cout << "\t\n" << "2. Withdraw Money" ;
            cout << "\t\n" << "3. Produce Cheque";
            cout << "\t\n" << "4. Deposit Cheque";
            cout << "\t\n" << "5. Open Acount";
            cout << "\t\n" << "6. Close Account" ;
            cout << "\t\n" << "7. Add Credit Card";
            cout << "\t\n" << "8. Add Loan";
            cout << "\t\n" << "9. Add Mortgage";
            cout << "\t\n" << "0. Back";
            cout << "\n\n\t" << "> ";

            cin >> choice;
            cin.ignore(256, '\n');

            switch(choice)
            {
                case '0':
                    active = !active;
                    break;
                case '5':
                    choosingAccount(ID);
                    break;
                default:
                    cout << "\nInvalid selection\n\n";
                    break;
            }
        }
        else
        {
            cout << "\nInvalid ID\n\n";
            active = !active;
        }
    }
    cout << endl;
}

void customerMenu()
{
    char choice;
    bool active = true;
    int cID;

    while (active)
    {
        cout << "Main Menu >> Customers" << endl;
        cout << "-----------------------" << endl;
        cout << "\t\n" << "1. View Customer" ;
        cout << "\t\n" << "2. List Customers" ;
        cout << "\t\n" << "3. Add Customer";
        cout << "\t\n" << "4. Delete Customer";
        cout << "\t\n" << "0. Back";
        cout << "\n\n\t" << "> ";

        cin >> choice;
        cin.ignore(256, '\n');

        cout << "\n";

        switch(choice)
        {
            case '1':
                cout << "Enter Customer ID > ";
                cin >> cID;
                cout << endl;
                specificCustomerMenu(cID);
                break;
            case '2':
                if (noOfCustomers == 0)
                {
                    cout << "-----No Customers----" << endl << endl;
                }
                else
                {
                    for (int i = 0; i < noOfCustomers; i++)
                    {
                        cust[i].showPersonDetails();
                        cout << endl;
                    }
                }
                cout << endl;
                break;
            case '3':
                cust[noOfCustomers].addNewCustomer();
                cust[noOfCustomers].setID(noOfCustomers+1);
                noOfCustomers++;

                cout << endl << "-----Customer Added Successfully----" << endl << endl;
                break;
            case '4':
                //TO DO: Delete Customer.
                break;
            case '0':
                active = !active;
                break;
            default:
                cout << "\nInvalid selection\n\n";
                break;
        }
    }
}


int main()
{
    char choice;
    bool active = true;

    cout << "BANK SYSTEM V1.4" << endl;
    cout << "-----------------------" << endl;
    while (active)
    {
        cout << "\nMain Menu" << endl;
        cout << "---------------" << endl;
        cout << "\t\n" << "1. Customers" ;
        cout << "\t\n" << "2. Account" ;
        cout << "\t\n" << "3. Cheques";
        cout << "\t\n" << "4. Management";
        cout << "\t\n" << "0. Exit";
        cout << "\n\n\t" << "> ";

        cin >> choice;
        cin.ignore(256, '\n');

        cout << "\n";

        switch(choice) 
        {
            case '1':

                {
                    customerMenu();
                    break;
                }


            case '0':
                active = !active;
                break;

            default:
            cout << "\nInvalid selection\n\n";
            break;
        }
    }
    return 0;
}
Attachments model.png 9.23 KB

Are you asking how to implement inheritence?

If so, such tasks take place primarly in your class definitions (.h) files, so, might be an idea to post them over the class implementation (.cpp). IMO.

I don't get it though because inheritence usually has a "IS A", "CAN BE" Are you sure you don't mean association? ("HAS A") so therefore..

CUSTOMER
<has a>
CurrentAccount

?

Also, look at this:

void Customer::addNewCustomer()
{
    cout << "Enter your name > ";
    getline(cin,name);
    cout << "Enter your Address: ";      
    getline(cin,address);
    cout << "Enter your telephone number: ";  
    getline(cin,telNo);
    cout << "Enter your sex: ";    
    getline(cin,sex);
    cout << "Enter your Age: ";   
    cin >> age;
}

Avoid putting inputs / outputs in the class. Think about if you wanted to output this code in a different way than just console... It wouldn't work.

Edited 4 Years Ago by phorce

Yes that's exactly what I mean, but CurrentAccount inherrits from account, and so if a customer opens a current account, or savings account, they can all be stored and shown in an array of accounts from the customer class.

I believe I have my inheritance initialised correctly, it's just the using it I don't understand (I could be wrong).

i.e. how would a customer open a current account as oposed to a parent account? As they both have the same openAccount() method name :S

Sorry, here are my .h files.

Customer.h

#ifndef Customer_H
    #define Customer_H

    #include<iostream>
    #include "Date.h"
    #include "Account.h"

    using namespace std;

    class Customer
    {
        public:
            int ID;
            int age;
            int noOfAccounts;
            Account acnts[10];
            string name, address, telNo, sex;
            Account *accRef;

        public:
            //Constructors
            Customer();     

            //Get Methods
            int getID();
            int getAge();
            string getName();
            string getAddress();
            string getTelNo();
            string getSex();
            Account getAccount();

            //Set Methods
            void setID(int ID);
            void setAge(int age);
            void setName(string name);
            void setAddress(string address);
            void setTelNo(string telNo);
            void setSex(string sex);
            void setAccount(Account *accRef);

            //Input/Output Methods
            void addNewCustomer();
            void addNewAccount();
            void showPersonDetails();
            void showPersonAccounts();
    };

    #endif  

Account.h

#ifndef ACCOUNT_H
#define ACCOUNT_H

#include "Cheque.h"
#include<iostream>

using namespace std;

class Account
{
    protected:
        int ID;
        double money;

    public:
         Account();     
         void setID(int ID);
         double balance(void) const;
         void deposit(double amount);
         bool deposit(const Cheque& c);
         bool withdraw(double amount);
         bool withdraw2(double& amount);
         Cheque withdrawCheque(double amount);
         void addInterest(void);
         void setInterest(double percent);
         void printOn(ostream& s);

         void createAccount();
         void showAccount();

        friend ostream& operator <<(ostream& s, Account& a);   // This operator is a friend of the class, but is NOT a member of the class
};

ostream& operator <<(ostream& s, Account& a);                  // This is the prototype for the overload

#endif

CurrentAccount.h

#ifndef CURRENTACCOUNT_H
#define CURRENTACCOUNT_H

#include "Account.h"

class CurrentAccount: public Account
{
    protected:
        double interestRate;
    public:
        CurrentAccount();   
        void addInterest(void);
        void setInterest(double percent);

        void createAccount();
        void showAccount();
};

#endif  

Edited 4 Years Ago by ralph1992: Code tags not working

i.e. how would a customer open a curre

i.e. how would a customer open a curre

Ok, so you know about Objects, right? Well if you had this:

Account a; // initalises an object of "Account"

CurrentAccount a; // Initalises an object of "CurrentAccount"

The thing is, through using association, if you know that a Customer HAS to make an Account with your system, and then they have the ability to make a current account, then what is the point in re-adding the data? The data is already there. (I believe):

Let me give you an example:

Account a(1, "Phorce", 150.00);
// my account number is 1, my name is Phorce and I'm very rich, I have £150+

Now I want a Current Account, it has a different number and you're going to give me £100 extra for making this as well as a intrest rate of 12%

Account a(1, "Phorce", 150.00); // Defined before
CurrentAccount c(10, 12, a); 

I have done a basic, basic example of this (I really do hope I haven't confused you) It's been a while since I have done association but have a look at this:

#include <iostream>

using namespace std;

class foo {

    public:
        foo() { }
        foo(int theAccountNo) {
             account_no = theAccountNo;
         }

        int getAccountNo()
        {
            return account_no;
        }
    protected:
        int account_no;


};

class bar : public foo {

    public:
        bar(int CURRENT_ACCOUNT_DETAILS) { current = CURRENT_ACCOUNT_DETAILS; }

        bar(int CURRENT_ACCOUNT_DETAILS, foo f) {

            current = CURRENT_ACCOUNT_DETAILS;  
            account_no = f.getAccountNo();
        }

        int returnCurrentAccountDetails()
        {
            return current;
        }

        int getAccountNo()
        {
            return account_no;
        }

    protected:
        int current;
        foo f;

};
int main(int argc, char *argv[]) {

    // I have created an "Account"
    foo f(10);
    // I have created a "CurrentAccount" which has all the properties of "Account"
    bar b(40, f);

    cout << "The account Number of the Account: " << b.getAccountNo() << endl;
    cout << "The Account Number of the Current Account: " << b.returnCurrentAccountDetails() << endl;
}

The account Number of the Account: 10
The Account Number of the Current Account: 40

Edited 4 Years Ago by phorce

I want to give the customer a 'CurrentAccount' which is inherited from Account; and then store it in an array of Accounts.
How do I go about doing this?

You can't. Account acnts[10]; holds objects of type Account by value.
When you try to store an object of type CurrentAccount into that array it gets sliced.
http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c

Use an array of pointers to Account instead: Account* ptr_accts[10];

Note: Ideally std::shared_ptr<Account> or std::reference_wrapper<Account> depending on the storage duration; but this may be beyond what you have learned till now.

If i create a pointer to Accounts:
Account* acnts[10]

all my calls too:
acnts[noOfAccounts].createAccount();

have an "expresison must have class type" error :S

And I also still don't udnerstand how I'd call my currentAccount createAccount Method :/

I can create a new decleration for them:
CurrentAccount cacnts[10];

And call them like so:
cacnts[noOfAccounts].createAccount();

but this stores them in 2 seperate arrays which I'm not allowed to do for the assignment :/
And I can't seem to grasp pointers :(

Hey,

I don't know what you're doing here..

acnts[noOfAccounts].createAccount();

What are you storing noOfAccounts with?

Essentially, you're initalising an array of objects here:

Account* acnts[10]

So to access this, you would do this:

acnts[0].CreateAccount();

Here, look at this example:

#include <iostream>
#include <vector>

using namespace std;

class Account {

    public:
        Account()
        {

        }

        virtual void CreateAccount()
        {
            cout << "I have created an Account " << endl;
        }

};

class CurrentAccount : public Account {

    public:
        CurrentAccount()
        {

        }

        virtual void CreateAccount()
        {
            cout << "I have created a Current Account! " << endl;
        }
};


int main(int argc, char *argv[]) {
   Account *acnts = new Account[10];
   CurrentAccount *cAcnts = new CurrentAccount[10];

   acnts[0].CreateAccount();
   cAcnts[0].CreateAccount();
 }

but this stores them in 2 seperate arrays which I'm not allowed to do for the assignment :/

And I can't seem to grasp pointers :(

I don't understand this.. Stores what as two seperate arrays? Are they wanting you to create a 2D array? Essentially, a pointer is memory location over value.

P.S. I think this is what you mean by it:

int numberOfAccounts = 0;

cout << "Please enter the number of accounts you want to create";
cin >> numberOfAccounts;
for(int i=0; (i < numberOfAccounts); i++)
{
   acnts[i].CreateAccount();

}

BUT as I pointed out first, it is really bad to have inputs/outouts DIRECTLY inside the class functionality.

Edited 4 Years Ago by phorce

Thank you for all your help so far and sorry if I'm really confusing you.

I just implemented what you showed me and it works fine. Customer can now have an 'Account' or a 'Current Account'.

However, I want Customer to be able to only have child accounts (ie. a CurrentAccount or SavingsAccount if I implement it) which can then be stored in one array.

So if i list all of the accounts a customer has:

void Customer::showPersonAccounts()
{
    cout << "Accounts<" << noOfAccounts << ">:" << endl;

    if (noOfAccounts == 0)
    {
        cout << "<None>" << endl;
    }

    else
    {
        for (int i = 0; i < noOfAccounts; i++)
        {
            acnts[i].showAccount();
        }
    }
    cout << endl;
}

i can loop through 1 array displaying the respected child accounts .showAccount() method.

Is this not possible?

Hey,

I think I get what you mean.. Look at it this way:

Customer <HAS>
   Account
   <Can be>
       Savings <or> Current

Let's say you have a customer:

Customer c(ID, NAME, ..., ...);

Customer c(1, "Phorce", ..., ...);

Now you would (in some way) have to link the ID to the customer, etc..

IF, however, you're creating the Objects all together, ok, so this way:

// create the customer, to have a savings, and current account
Customer c[1];
Account a[1];

for(int i=0; (i < 1); i++)
{
    c[i].makeAccount();
    a[i].makeAccount();
}

Then you can assume that c[0] HAS the account as a[0] thus infers that this can happen:

void Customer::showPersonsAccount(int customerNo)
{
   // remembering that CustomerNo is infact the number #id->place memory (set through "i")

   cout << c[i].returnName();
   cout << a[i].getAccountNumber();

}

You should find that the name, and the account number belongs to the same person that you created :)

@ralph1992 Please do ignore my last post, I've had a think about this.

Ok, what you're looking for is Association, nothing else, and nothing more. The fact your tutor wants this:

which can then be stored in one array.

Tells me that your instructor doesn't want you to have one ARRAY more as such as one ARRAY of OBJECTS. The method of Association is taught a lot in Colleges (I got taught this when I was at College..) ANYWAY..

I did an example, above somewhere that totally got ignored ;) so I'm guessing it confused the hell out of you. Not to worry, I have came up with another solution to show you exactly what I mean.

At the minute, you have an array of objects called Customers, an array of objects called "Accounts" and an array of objects called "Savings accounts" etc.. Our goal is to make all these objects, or, two objects become one array of objects, how we do this is to use Association, actually create an object of what you desire inside a class. (mhm?)

Let's see:

class Customer {

   public:

        Customer();
        Customer(int age, ..., ...);
        // Create another Constructor to allow for an object to be passed to it.
        Customer(int age, ..., ..., Account theA)
        {
            this->a = theA;
        }

        // create a member function to return "something"
        int getSomething()
        {
            return a.getSomething(); // we return the member function in "Account"
        }
    protected:

        Account a; // I have created an object of the class "Account"
};

class Account {

    public:

        Account();
        Account(int A)
        {
           this->something = A;
        }

        int getSomething()
        {
           return this->something;
        }
   protected:
      int something;
};

// Now I can do this:

int main()
{
    Customers c[10];

    Account a(20); // passing value "20"

    c[0] = Customer(1, ..., ..., a);

    cout << c[0].getSomething(); // returns 20.
}

Notice how I use Account a() but in the end, I don't need it. All I'm left with is an Array of objects, which, is what the outcome should be. Ok, another example, more specific to your needs:

#include <iostream>

using namespace std;

class Account {
    public:
        Account() { }
        Account(int accountNumber, int theBalance) 
        { 
            this->account_num = accountNumber;  
            this->balance = theBalance;
        }

        int returnAccountNo()
        {
            return account_num;
        }


    protected:

        int account_num;
        int balance;
};

class SavingsAccount : public Account {

    public:

        SavingsAccount() { }
        SavingsAccount(int savingsRate);
};

class Customer {
    public:
        Customer() { }
        // Constructor for just allowing a customer to be made
        Customer(int ID, string theFirstName, string theLastName);

        Customer(int ID, string theFirstName, string theLastName, Account theAccount)
        {
            a = theAccount;
        }

        int account_No()
        {
            return a.returnAccountNo();
        }

    protected:
        Account a;
        int num;
};


int main(int argc, char *argv[]) {

    Customer c[10];

    int customer_id = 0;
    string firstName;
    string lastName;
    int acc_type = 0;
    for(int i=0; (i < 1); i++)
    {
        customer_id = i;
        cout << "Please enter their first name: " << endl;
        cin >> firstName;

        cout << "Please enter their last name: " << endl;
        cin >> lastName;

        cout << "(1) Current Account, (2) Savings account";
        cin >> acc_type;

        if(acc_type == 1)
        {
            int accountNumber = 0;
            cout << "Please enter their Account number: ";
            cin >> accountNumber;

            float balance;

            cout << "Please enter their balance (up to now): ";
            cin >> balance;

            Account a(accountNumber, balance);

            c[i] = Customer(customer_id, firstName, lastName, a);

        }else if(acc_type == 2){
            cout << "Savings";
        }else{
            cout << "Not an option";
        }
    }

    cout << c[0].account_No();


}

Notice at the end, c[0].account_No(); is accessing the account number which is stored in class "Account" without the need of physically declaring an object to return this value

I really do hope this has helped you, and, cleared up any confusion. Let me know!

Thank you soo much for the help phorce! That got it and I understand it a lot more now!

I couldn't ask for this thread to be deleted? Or my posts to be edited and my code removed (Didn't realise I couldn't do this myself)

This is because I've put my entire written code for my assignment up...obviously a bad idea to keep it here :/

Sorry

Hey,

Please can you mark this post as solved and give rep to those who helped you? (You can give rep by clicking the "up" arrow next to someones name).

As for your question related to deletion, it is very, very unlikly that your post/thread will be deleted, this is because DaniWeb is a resource forum in which members looking for help in this area can look back to this.

Could you tell me why it is bad that your full source code is up?

As my cource code can be copied or used by others doing the same assignment; this is an individual university assignment marked by external examiners. If this post is found matching my source code handed in (They wouldn't know this was me) therefore I'd be marked as a fail (0) for copying code online.

Sorry :/

Hey,

Off-topic: What University and what year are you in?

I'll give you some hope:

When I was in my first and second year at University, I posted code here all the time and no code was copied (I'm not saying that this would happen in your case) BUT what I am saying is that, if EVERYONE has the same assignment, then, it is likely that parts of your code will look the same anyway. Your tutor, or, external examiners are not looking for the fact "Hey he can do conditional statements" more as the fact that you understand the logic behind the assignment (How does inheritence/Association etc..) work.

The likelyhood of someone finding this, as well, after you mark it as "solved" will be very unlikely, or, probablamtic since a lot of questions get's asked daily which ,eans they would most likely ask the same /or/ simular question anyway, in, which case, someone will help them anyway and probably give a more or less the same answer as me.

I am not a Moderator though, so, my advice would be to click the "Flag bad post" and explain your situation (In enough details) and hopefully a Moderator will look at it and make a decision from there :)! IMO I would be happy with you changing your original post to just your (.h) class definitions since that's what I went off when helping you out :) Good luck with your assignment though.

This question has already been answered. Start a new discussion instead.