从对象指针的向量中获取值

Getting values from a vector of object pointers

本文关键字:获取 向量 对象 指针      更新时间:2023-10-16

我需要将存储在vector中的对象的名称,act# 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.nn";
    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" nn";
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);
}

再读一遍关于构造函数的课本:所有的构造函数都存在严重的问题。因此,您没有初始化对象成员变量,并且您最终有效地打印了大量零和空字符串…

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

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

其次,对于派生类,基类的初始化必须在初始化器列表中完成,而不是在函数体中。下面是检查函数

的正确定义:
Checking::Checking(Person* per, int actNum, double bal, double interest)
: Account(actNum, bal, per)
, monthlyFee(-bal)
{}

第三,要小心使用该函数的实参初始化成员变量。有时则相反,使用(未初始化的)成员变量赋值。

顺便说一句,Account是多态层次结构的基类:因此,Account析构函数必须声明为virtual。