【问题标题】:Is it possible to call a method from main if it is private? If not, how would it be possible?如果它是私有的,是否可以从 main 调用方法?如果没有,怎么可能?
【发布时间】:2012-11-19 13:51:09
【问题描述】:

我试图让这个程序开始运行,但目前我只是得到错误。我不知道如何让它工作。如果我将类 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;
    }

有什么建议吗?

【问题讨论】:

  • Friend functions in C++ 的可能重复项
  • 我意识到白天的反对票比晚上多得多。
  • 考虑一下时区,以及 SO 的大部分用户可能居住的地方 ;-)

标签: c++ class methods private-members


【解决方案1】:

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

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

您现在可以从主界面调用它们了。

【讨论】:

  • 然而,这不是根本问题。根本问题是他试图在没有类实例的情况下使用类的方法。
【解决方案2】:

由于您无法更改 SavingsAccount 类,并且它禁止访问其成员(private 是默认值),因此您不应使用该类的任何成员。

“问题出在 main 函数中”:不,出在你的类的设计中。没有公开的类是没有用的。

您的问题没有干净的解决方案。

在更改类的边界上的解决方案是定义一个“接口”,并使(未更改的)类继承该接口:

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


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

main 将使用 Account iso SavingsAccount

SavingsAccount savingsAccount;
Account& account = savingsAccount;

// should be accessible via the `Account` interface.
account.AccountClosureLoss(); 

【讨论】:

    【解决方案3】:

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

    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;
    };
    

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

    【讨论】:

      【解决方案4】:

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

      int main()
      {
          SavingsAccount account;
          account.Information();
      
          ...
      
          return 0;
      }
      

      另外,是的,你要调用的类的方法必须是公开的。

      【讨论】:

      • 他还需要正确声明 SavingAccount 的哪些部分是公开的,哪些是私有的。
      • 不需要投票给我。我发现代码有一个明显的问题。我已经对其进行了修改以表明是的,他要调用的方法必须是公开的。
      • 不是我!我和你一样困惑。
      • 太棒了,他只是编辑了代码来解决我发现的问题,没有说什么。
      猜你喜欢
      • 1970-01-01
      • 2013-12-01
      • 2010-10-07
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多