0

Recently I have an object oriented test and the questions are not so tough but the info that they gave me makes me ask myself how to use it. Below are the codes and there are some codes that I create to test and see (I give comment on the codes that I create)

#include <iostream>
#include <vector>
using namespace std;

class Account{
protected:
    int accno;
    double balance;
public:
    // I add the initialization part to make the program works
    // (In my test they don't provide because there is a question about that)
    Account(int accno, double balance) : accno(accno), balance(balance){};
    double getBalance();
    int getAccno();
    void deposit(double amount);
};

class Saving : public Account{
public:
    // Same reason as above for initialization
    Saving (int accno, double balance) : Account(accno,balance){};
    bool withdraw (double amount);
    void compute_interest();
    // I add this method/function to see how Saving object behave
    void print(){
        cout << "ID: " << accno << "     balance: " << balance << endl;
    }
};

class Bank{
    int ID;
    //Saving accounts2;// If I comment this line I can compile otherwise I can't even create a Bank object
    vector<Saving>accounts;// This is the line that I don't know how to make use of it
public:
    // The 2 methods/functions below & the attribute/variable "ID" I create to confirm my expectation
    void addID(int newID){
        ID = newID;
    }
    void printID(){
        cout << "ID: " << ID << endl;
    }

    void addAccount(Saving account);
    void print();
};

int main()
{
    Saving s1(1001,500.0);
    s1.print();
    Bank b1;
    b1.addID(1111);
    b1.printID();

    return 0;
}

All the codes inside main function I create to see how the output looks like.

I think the following codes are part of how we can make use of all the classes (but I still don't know how to use vector though)

Saving s1(5000,600.00);
Saving s2(5001,111.11);
Bank b1;
b1.addAccount(s1);
b1.addAccount(s2);

So what I really want to know is:

  1. How to make use these lines of code so that something similar to the above code can be used (or better just use pushback function to create new instance because it's vector)

////

vector<Saving> accounts;
void addAccount(Saving account); 
  1. Why I can't compile if I create the "Saving accounts2;"
hafhaf100
  • 1
  • 3

2 Answers2

0

For giving some guideline on how to use the vector, please take a look at: http://www.cplusplus.com/reference/vector/vector/ presenting this class. Just a quick hint: in the most common situation you usually add to a vector with push_back. This will add an element at the end of the vector. Then you iterate over it (Iteration over std::vector: unsigned vs signed index variable) and simply print out the required information.

Regarding: Saving accounts2; makes your program uncompilable is due to the fact that you don't have a default constructor. Please see http://en.cppreference.com/w/cpp/language/default_constructor on how to create one.

Community
  • 1
  • 1
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • I knew about the push_back function but it just doesn't occur to me to use how @Vicente wrote because I just started programming so I only know how to add elements using the normal type and not using a class. So I test a few codes but I just can't seem to figure out how to print the account Here's what I've tested `void addAccount(Saving account)` ` { accounts.push_back(account); }` `void print()` `{ cout << "accno: " << accounts[0].accno << " balance: " << accounts[0].balance << endl; }` – hafhaf100 Aug 05 '16 at 12:35
0

Implement the void addAccount(Saving account) function in the Bank class to actually add the account to the vector of accounts:

void addAccount(Saving account) {
    accounts.push_back(account);
}

Implement the double getBalance() and int getAccno() functions in the Account class to return the account number and the balance respectively:

double getBalance() { return balance; }
int getAccno() { return accno; }

Implement the print() function of the Bank class to print the account number and balance of every account stored in the accounts vector:

void print() {
   for (vector<Saving>::iterator it = accounts.begin(); it != accounts.end(); it++) {
      cout << "accno: " << it->getAccno() << " balance: " << it->getBalance() << endl;
  }
}
  • So that's how you add an element with vector in class. Because I just started learning about class and I usually just create new object for that class eg: `Saving s1 (1000, 100.00);` `Saving s2 (1001, 200.00);` So I write my code with just the overloaded constructor, accessor, and mutator. So I thought I should just write like this `void Saving::addAccount(Saving account)``{ accounts = account; }` – hafhaf100 Aug 05 '16 at 12:33
  • In the class `bank` you have a method `addAccount` which is currently not implemented (doesn't have any code enclosed between `{` and `}`. So, make it do something as I told you in my answer. Take the `account` you are passing it as an argument, and store it into the `accounts` vector that the `bank` class has. – Vicente Olivert Riera Aug 05 '16 at 12:37
  • Thanks for the quick reply. I've already done it & it compiles with no problem. The problem now I cant seem to figure out how to write the implementation for function `print`. Here's what I've tried `cout << "accno: " << accounts[0].accno << " balance: " << accounts[0].balance << endl;` I know the variables are protected but I'm stuck. – hafhaf100 Aug 05 '16 at 12:50
  • So when you call the `print()` method of the `bank` class, you want to print the `accno` and the `balance` of every account stored into the `accounts` vector? You will need to implement the `getAccno()` and `getBalance()` methods in the `Account` class in order to return the `accno` and the `balance` respectively. – Vicente Olivert Riera Aug 05 '16 at 12:59
  • I've updated my answer based on your latests comments. – Vicente Olivert Riera Aug 05 '16 at 13:42