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:
- 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);
- Why I can't compile if I create the "Saving accounts2;"