-3

我试图让这个程序开始运行,但目前我只是得到错误。我不知道如何让它工作。如果我将类 SavingsAccount 更改为 public 应该没问题,但我必须保持原样。

问题出在主函数中。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class SavingsAccount
{
    int accountType;
    string ownerName;
    long ssn;
    double accountClosurePenaltyPercent, accountBalance;
    void Information();
    inline double AccountClosureLoss()
    {
        return (accountBalance * accountClosurePenaltyPercent);
    }
    void OutputInformation();

};

void SavingsAccount::Information()
{
    cout << "Enter Account Type (1 for Checking or 2 for Savings): ";
    cin >> accountType;
    cout << "Enter owner name: ";
    getline(cin, ownerName);
    cout << "Enter the Social Security Number: ";
    cin >> ssn;
    cout << "Enter the percent penalty for closing account(decimal form): ";
    cin >> accountClosurePenaltyPercent;
    cout << "Enter the account balance: ";
    cin >> accountBalance;
}

void SavingsAccount::OutputInformation()
{
    cout << "Account Type: " << endl;
    cout << "Name: " << ownerName << endl;
    cout << "SSN: " << ssn << endl;
    cout << "Account Closure Penaly %: " << accountClosurePenaltyPercent << endl;
    cout << "Account Balance: " << accountBalance;
}

int main(void)
{
    SavingsAccount.Information(); 
    SavingsAccount.AccountClosureLoss();
    SavingsAccount.OutputInformation();
    return 0;
}

到目前为止我尝试了什么。

int main(void)
    {
        SavingsAccount John;
        John.Information(); 
        John.AccountClosureLoss();
        John.OutputInformation();
        return 0;
    }

有什么建议么?

4

4 回答 4

3

好吧,默认情况下成员函数是私有的,因此您可以随时将它们添加到公共中,如下所示:

class SavingsAccount
{
    private:
    int accountType;
    string ownerName;
    long ssn;
public:
    double accountClosurePenaltyPercent, accountBalance;
    void Information();
    inline double AccountClosureLoss()
    {
        return (accountBalance * accountClosurePenaltyPercent);
    }
    void OutputInformation();
};

您现在可以从主系统调用它们。

于 2012-11-19T13:54:23.817 回答
1

Since you cannot change the SavingsAccount class, and since it prohibits access to it's members (private is the default), you are not supposed to use any menber of that class.

"The problem is in the main function": no, it is in the design of your class. A class with nothing public is not useful.

There is no clean solution to your problem.

A solution on the borderline of changing the class would be in defining an 'interface', and making the (unchanged) class inherit that interface:

class Account {
public:
   virtual ~Account(){}
   virtual void Information() = 0;
   virtual double AccountClosureLoss() = 0;
   virtual void OutputInformation() = 0;
};


class SavingsAccout : public Account {
... body remains unchanged
};

The main will use Account iso SavingsAccount:

SavingsAccount savingsAccount;
Account& account = savingsAccount;

// should be accessible via the `Account` interface.
account.AccountClosureLoss(); 
于 2012-11-19T13:57:43.450 回答
1

您正在尝试在方法中使用成员属性,但您正在尝试在没有实例的情况下使用您的方法。所有成员属性的值都存储在您的实例中,因此您首先需要一个实例。将其添加到您的主要功能中:

int main(void)
{
    SavingsAccount myAccount;
    myAccount.Information();
    myAccount.AccountClosureLoss();
    myAccount.OutputInformation();
    return 0;
}

此外,您的方法被定义为私有的,您应该始终使用public:private:如下:

class SavingsAccount
{
    public:
        void Information();
        inline double AccountClosureLoss()
        {
            return (accountBalance * accountClosurePenaltyPercent);
        }
        void OutputInformation();

    private:
        int accountType;
        string ownerName;
        long ssn;
        double accountClosurePenaltyPercent;
        double accountBalance;
};

你不能使用没有实例的方法。即使您可以(可能是静态的?)您也不能在其中使用任何成员属性,因此将其包含在类中是没有用的。

于 2012-11-19T14:06:30.293 回答
0

您必须首先声明 SavingsAccount 类的实例。例如:

int main()
{
    SavingsAccount account;
    account.Information();

    ...

    return 0;
}

此外,是的,您要调用的类的方法必须是公共的。

于 2012-11-19T13:53:55.440 回答