从对象指针向量中获取值

时间:2015-10-24 05:22:30

标签: c++ vector

我需要将名称,行为#balance和存储在矢量中的对象的地址写入文件。

我相信我有将程序推入向量的程序,但由于它们是对象指针的向量,因此我无法弄清楚如何调用对象并打印出所有3个对象。

Main.cpp的

vector<Account*> accounts;

accounts.push_back(new Savings(new Person("Bilbo Baggins", "43 Bag End"), 1, 500, 0.075));
accounts.push_back(new Checking(new Person("Wizard Gandalf", "Crystal Palace"), 2, 1000.00, 2.00));
accounts.push_back(new Savings(new Person("Elf Elrond", "Rivendell"), 3, 1200, 0.050));

ofstream outFile;
outFile.open("accounts.txt");

if (outFile.fail())
{
    cout << "\nYour file did not open, the program will now close!\n";
    system("PAUSE");
    return 0;
}
else
{
    cout << "\nBINGO!!! It worked.\n\n";
    system("PAUSE");
    cout << "\n";
}

//  New : Using a loop, send messages to each of the three Account objects to write themselves out to the file.

cout << "\nNow we are going to write the information to \"Accounts.txt\" \n\n";
system("PAUSE");

for (int i = 0; i < accounts.size(); i++) {
    accounts[i]->writeAccount(outFile);
}

Account.h

#pragma once
#include <string>
#include <iostream>
#include "Person.h"

using namespace std;

// Account class - abstract/parent class
class Account
{

private:
    int actNumber;
    double actBallance;
    Person PersonName;
public:
    Account();
    Account(int, double, Person*);
    int getActNumber();
    virtual double getActBallance();
    string getName();
    string getAdd();
    void deposit(double);
    void withdrawl(double);
    virtual void writeAccount(ofstream&);
    virtual void readAccount(ifstream&);

    void testAccount(int i);

};

// Checking class: inherits from the Account class
class Checking : public Account
{

private:
    double monthlyFee;

public:
    Checking();
    Checking(Person*, int, double, double);
    void setMonthlyFee(double);
    double getActBallance();
    void writeAccount(ofstream&);
    void readAccount(ifstream&);

};



// Savings class: inherits from the Account class
class Savings : public Account
{


private:
    int interestRate;

public:
    Savings();
    Savings(Person*, int, double, double); // person, act#, Ballance, Interest Rate
    void setInterestRate(double);
    double getActBallance();
    void writeAccount(ofstream&);
    void readAccount(ifstream&);

};

Account.cpp

#include "Account.h"
#include <string>
using namespace std;



Account::Account()
{
    actNumber = 0;
    actBallance = 0.0;

}


Account::Account(int act, double bal, Person* name)
{
    actNumber = act;
    actBallance = bal;
}


int Account::getActNumber() 
{
    return actNumber;
}
double Account::getActBallance() 
{
    return actBallance;
}
string Account::getName()
{
    return PersonName.getName();
}
string Account::getAdd()
{
    return PersonName.getAddress();
}
void Account::deposit(double money) 
{
    actBallance += money;
}
void Account::withdrawl(double money) 
{
    actBallance -= money;
}

void Account::writeAccount(ofstream& output)
{
    output << actNumber << "\n" << actBallance << "\n" << PersonName.getName() << "\n" << PersonName.getAddress() << endl;
}

void Account::readAccount(ifstream& output)
{
    output >> actNumber;
    output >> actBallance;
}


// Checking Account
Checking::Checking() {
    monthlyFee = 0;
}

Checking::Checking(Person* per, int actNum, double bal, double interest) {

    bal -= monthlyFee;

    Account:Account(actNum, bal, per);
}

void Checking::setMonthlyFee(double fee) {
    monthlyFee = fee;
}

double Checking::getActBallance() {
    double ballance = Account::getActBallance();
    return ballance = monthlyFee;

}

void Checking::readAccount(ifstream& output) {

    int actNumber = Account::getActNumber();
    int actBallance = Account::getActBallance() - monthlyFee;

    output >> actNumber;
    output >> actBallance;

}

void Checking::writeAccount(ofstream& output) {

    int actNumber = Account::getActNumber();
    int actBallance = Account::getActBallance();

    output << actNumber << "\n" << actBallance << endl;
}



// Savings Account
Savings::Savings() {
    interestRate = 0;

}
// Savings(Person, int, double, double) // person, act#, Ballance, Interest Rate
Savings::Savings(Person* per, int actNum, double bal, double interest) {

    bal += (bal * interest);

    Account:Account(actNum, bal, per);

}

void Savings::setInterestRate(double rate) {
    interestRate = rate;
}

double Savings::getActBallance() {
    double ballance = Account::getActBallance();
    return ballance + (ballance * interestRate);
}

void Savings::readAccount(ifstream& output) {

    double actBallance = Account::getActBallance();
    int actNumber = Account::getActNumber();
    actBallance += (actBallance * interestRate);

    output >> actNumber;
    output >> actBallance;

}

void Savings::writeAccount(ofstream& output) {
    int actNumber = Account::getActNumber();
    int actBallance = Account::getActBallance();

    output << actNumber << "\n" << actBallance << endl;
}

我意识到我离我很远......但是我已经在这里度过了我的生活,我无法弄清楚我的生活,而是拿出对象指针的向量并输出对象值。

Person.h

#pragma once
#include <string>
#include <fstream>
using namespace std;

class Person
{
private:
    string name;
    string address;
public:
    Person();
    Person(string a, string b);
    string getName();
    string getAddress();
    void writePerson(ofstream&);
    void readPerson(ifstream&);

};

Person.cpp

#include "Person.h"
#include <string>
using namespace std;

Person::Person()
{
    name = "NAME";
    address = "123 STREET";
}


Person::Person(string a, string b)
{
    name = a;
    address = b;
}

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

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

void Person::writePerson(ofstream& output)
{
    output << name << " " << address << endl;
}

void Person::readPerson(ifstream& output)
{
    output >> name;
    output >> address;

    Person(name, address);

}

1 个答案:

答案 0 :(得分:0)

再次阅读有关构造函数的课程书:所有构造函数都存在严重问题。因此,您不会初始化对象成员变量,并且有效地最终会打印大量的零和空字符串......

首先,对于您的基类,您必须初始化人名。你应该写:

Account::Account(int act, double bal, Person* name)
: actNumber(act)
, actBallance(bal)
, PersonName(name)
{}

其次,对于派生类,基类的初始化必须在初始化列表中完成,而不是在ctor的主体中完成。这是例如Check's ctor的正确定义:

Checking::Checking(Person* per, int actNum, double bal, double interest)
: Account(actNum, bal, per)
, monthlyFee(-bal)
{}

第三,小心使用ctor的参数初始化成员变量。您有时会执行相反的操作并使用(未初始化的)成员变量分配ctor参数。

BTW,Account是多态层次结构的基类:因此,帐户析构函数必须被声明为虚拟。