如何在类的属性/变量中使用组合类作为向量类型
How to Use Composition Class as Vector Type in Attribute/Variable in a Class
最近我做了一个面向对象的测试,问题不那么难,但他们给我的信息让我问自己如何使用它
#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;
}
我创建的主函数中的所有代码都是为了查看输出的样子。
我认为以下代码是我们如何利用所有类的一部分(但我仍然不知道如何使用矢量(
Saving s1(5000,600.00);
Saving s2(5001,111.11);
Bank b1;
b1.addAccount(s1);
b1.addAccount(s2);
所以我真正想知道的是:
- 如何使用这些代码行,使类似可以使用上面的代码(或者最好只使用推回功能创建新实例,因为它是向量(
////
vector<Saving> accounts;
void addAccount(Saving account);
- 为什么如果我创建"Saving accounts2;">
有关如何使用矢量的指导原则,请查看:http://www.cplusplus.com/reference/vector/vector/介绍这门课。只是一个快速提示:在最常见的情况下,您通常使用push_back
添加到向量中。这将在矢量的末尾添加一个元素。然后对它进行迭代(对std::vector:unsigned vs signed index变量进行迭代(,并简单地打印出所需的信息。
关于:Saving accounts2;
使您的程序不可编译是因为您没有默认的构造函数。请参阅http://en.cppreference.com/w/cpp/language/default_constructor关于如何创建一个。
在Bank
类中实现void addAccount(Saving account)
函数,将帐户实际添加到帐户向量中:
void addAccount(Saving account) {
accounts.push_back(account);
}
在Account
类中实现double getBalance()
和int getAccno()
函数,分别返回账号和余额:
double getBalance() { return balance; }
int getAccno() { return accno; }
实现Bank
类的print()
功能,打印存储在accounts
矢量中的每个账户的账号和余额:
void print() {
for (vector<Saving>::iterator it = accounts.begin(); it != accounts.end(); it++) {
cout << "accno: " << it->getAccno() << " balance: " << it->getBalance() << endl;
}
}
相关文章:
- 检查向量是否具有所有可能的字符组合
- 从向量的向量中删除向量的组合
- 如何得到几个子向量的组合
- 如何组合两个整数向量
- 如果我们从每个 Y 向量中选择一个值,则 X 数字的每个组合都可能
- 在不使用递归的情况下求解所有 2D 字符串向量组合?(C++)
- 字符串向量的有效组合
- 向量的高效组合最小值和平均值计算
- 如何在 Boost.Sprit.Qi 中从向量制作组合的 ascii::string 规则<string>?
- 将多个向量(函数结果)组合到一个使用模板中
- 双重释放或损坏(输出):使用向量的组合算法0x0000000001a880a0***
- 如何沿行组合两个向量?
- 创建多个向量的所有可能组合
- 如何在 c++ 中将集合和数组合并到向量中
- 在C++编程语言中,如何用n个向量长度的所有可能的字符串组合来填充向量
- 在 C++11 中计算字母和单词的双字母组合的 std::线程向量的问题
- 找到两个向量数组的组合,该数组的总和是 c++ 中所有其他组合的最大值
- 如何在C++中组合向量的元素
- c++组合向量(如果它们共享一个数字)
- 这是用组合向量类重载[]的正确方法吗?