Is it possible to call a method from main if it is private? If not, how would it be possible?

后端 未结 4 960
日久生厌
日久生厌 2021-01-29 15:33

I am trying to get this program to start running but currently I just get errors. I am not sure how to get this to work. If I change the class SavingsAccount to public it should

4条回答
  •  生来不讨喜
    2021-01-29 16:12

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

提交回复
热议问题