2016-08-05 16 views
0

最近我有一个面向对象的测试,问题并不那么艰难,但他们给我的信息让我问自己如何使用它。下面是代码和有,我创建测试和看到一些代码(我给的,我创建的代码注释)如何在类中的属性/变量中使用组合类作为矢量类型

#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); 

所以我真正想知道的部分是:

  1. 如何利用这些代码,这样类似 上面的代码的东西都可以使用(或更好的只是使用反推功能 创建新实例,因为它的载体)

////

vector<Saving> accounts; 
void addAccount(Saving account); 
  • 为什么如果创建的我不能编译 “保存accounts2;”
  • 回答

    0

    为了给出一些如何使用矢量的指导,请看看:http://www.cplusplus.com/reference/vector/vector/介绍这个类。只是一个简短的提示:在最常见的情况下,你通常添加到一个向量与push_back。这将在向量的末尾添加一个元素。然后你迭代它(Iteration over std::vector: unsigned vs signed index variable),并打印出所需的信息。

    关于:Saving accounts2;使您的程序无法编译是由于您没有默认构造函数。请参阅http://en.cppreference.com/w/cpp/language/default_constructor关于如何创建一个。

    +0

    我知道有关的push_back功能,但它只是没有想到要使用@Vicente怎么写的,因为我刚开始编程,所以我只知道如何使用普通类型添加元素而不使用类。 所以我测试一些代码,但我似乎无法弄清楚如何打印帐户 下面是我测试过 '无效addAccount(储蓄账户)' '{ 账户。的push_back(帐户); }' '空隙打印()' '{ COUT << “ACCNO:” <<帐户[0] .accno << “平衡:” <<帐户[0] << .balance ENDL; }' – hafhaf100

    0

    实现在Bankvoid addAccount(Saving account)功能其实帐户添加到帐户的载体:

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

    实现在Accountdouble 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; 
        } 
    } 
    
    +0

    所以这就是你如何在类中添加一个元素与矢量。因为我刚开始学习班,我通常只需要创建新的对象为类如: '保存S1(1000,100.00);' '保存S2(1001,200.00);' 所以,我写我的代码只是重载的构造函数,访问器和mutator。 所以我觉得我应该这样写: 'void Saving :: addAccount(Saving account)''{{{0} {0} }' – hafhaf100

    +0

    在'bank'类中,你有一个'addAccount'方法,它目前还没有实现(没有任何代码包含在'{'和'}'之间,因此,按照我的说法我的回答是,以'account'作为参数传递它,并将其存储到'bank'类的'accounts'向量中。 –

    +0

    感谢您的快速回复。我已经完成并编译现在的问题我似乎不知道如何编写函数'print'的实现。以下是我尝试的 'cout <<“accno:”<< accounts [0] .accno <<“余额:“<< accounts [0] .balance << endl;' 我知道变量已被保护,但我被卡住了 – hafhaf100

    相关问题