【问题标题】:Trouble with abstract classes in c++c++中抽象类的问题
【发布时间】:2013-05-08 13:25:55
【问题描述】:

主要:

#include <iostream>
#include <string>
#include "serviceChargeChecking.h"

int main()
{
    serviceChargeChecking newAccount("Crim", 111222, 50.00, 100, 1.00); 


    system("PAUSE");
    return 0;
}

serviceChargeChecking.h:

#ifndef H_serviceChargeChecking
#define H_serviceChargeChecking

#include "checkingaccount.h"
#include <string>


class serviceChargeChecking: public checkingAccount
{
public:
    void setMonthlyFee(double);
    void writeCheck(int);
    void getMonthlyStatement() const;
    serviceChargeChecking(std::string =" ",int = 0, double = 0.00, int= 0, double =     0.00);
private:
    double serviceCharge;

};
#endif

serviceChargeChecking.cpp:

#include "serviceChargeChecking.h"
#include <iostream>

using std::string;


void serviceChargeChecking::setMonthlyFee(double fee)
{
    serviceCharge=fee;
}
void serviceChargeChecking::getMonthlyStatement() const
{
    checkingAccount::getMonthlyStatement();
    std::cout<< "Service Charge: " << serviceCharge << std::endl;
}
void serviceChargeChecking::writeCheck(int ammount)
{
    if(checkingAccount::getChecks()>0)
    {
        checkingAccount::setChecks(checkingAccount::getChecks()-ammount);
    }
    else
    {
        std::cout<<"No checks available." << std::endl;
    }
}
serviceChargeChecking::serviceChargeChecking(string name, int acct, double bal, int numCheck, double sCharge)
{
    bankAccount::setAcctOwnersName(name);
    bankAccount::setAcctNum(acct);
    bankAccount::setBalance(bal);
    checkingAccount::setChecks(numCheck);
    serviceCharge=sCharge;
}

checkingAccount.h:

#ifndef H_checkingAccount
#define H_checkingAccount
#include "bankAccount.h"
#include <iostream>

class checkingAccount: public bankAccount
{
public:
    virtual void writeCheck()=0;
    void deposit(double);
    void withdraw(double);
    void getMonthlyStatement() const;
    int getChecks();
    void setChecks(int);

private:
    int numChecks;
};
#endif

checkingAccount.cpp:

#include "checkingAccount.h"
#include <iostream>

int checkingAccount::getChecks()
{
    return numChecks;
}
void checkingAccount::setChecks(int c)
{
    numChecks=c;
}
void checkingAccount::deposit(double d)
{
    bankAccount::setBalance(bankAccount::getBalance()+d);
}
void checkingAccount::withdraw(double w)
{
    bankAccount::setBalance(bankAccount::getBalance()-w);
}
void checkingAccount::getMonthlyStatement() const
{ 
    bankAccount::getMonthlyStatement();
}

bankAccount.h:

#ifndef H_bankAccount
#define H_bankAccount
#include <string>

class bankAccount
{
public:
    std::string getAcctOwnersName() const;
    int getAcctNum() const;
    double getBalance() const;
    void getMonthlyStatement() const;

    void setAcctOwnersName(std::string);
    void setAcctNum(int);
    void setBalance(double);

    virtual void withdraw(double)=0;
    virtual void deposit(double)=0;
private:
    std::string acctOwnersName;
    int acctNum;
    double acctBalance;
};
#endif

bankAccount.cpp:

#include "bankAccount.h"
#include <iostream>
using std::string;


string bankAccount::getAcctOwnersName() const
{
    return acctOwnersName;
}
int bankAccount::getAcctNum() const
{
    return acctNum;
}
double bankAccount::getBalance() const
{
    return acctBalance;
}
void bankAccount::setAcctOwnersName(string name)
{
    acctOwnersName=name;
}
void bankAccount::setAcctNum(int num)
{
    acctNum=num;
}
void bankAccount::setBalance(double b)
{
    acctBalance=b;
}
void bankAccount::getMonthlyStatement() const
{
    std::cout << "Name on Account: " << getAcctOwnersName() << std::endl;
    std::cout << "Account Id: " << getAcctNum() << std::endl;
    std::cout << "Balance: " << getBalance() << std::endl;
}

我知道这是很多代码,但谁能帮我理解为什么我不能从类 serviceChargeChecking 中创建一个对象,错误告诉我我不能从抽象类中创建一个对象,但它没有对我来说似乎很抽象。

【问题讨论】:

  • 错误信息有助于缩小搜索范围。
  • 有太多代码需要阅读。你能改写你的问题吗?
  • 错误出现在 newAccount 下的主类中,说明:不允许抽象类类型“serviceChargeChecking”的对象
  • 删除一半,然后再删除一半,等等,直到你有一个最小的例子。
  • 另外,欢迎来到 Stack Overflow。请花时间阅读faq;你会得到一个badge。 (c:

标签: c++ class virtual abstract


【解决方案1】:

serviceChargeChecking 实现了void writeCheck(int),但是来自checkingAccount 的纯虚函数具有void writeCheck() 类型,所以它在serviceChargeChecking 中仍然是纯虚函数,这使得类抽象。

【讨论】:

    【解决方案2】:

    你在抽象类checkingAccount中有这个:

    virtual void writeCheck()=0;
    

    但是在派生类serviceChargeChecking中实现这个:

    void writeCheck(int);
    

    签名必须相同。

    【讨论】:

      【解决方案3】:

      writeCheck() 方法在serviceChargeCheckingcheckingAccount 中有不同的签名。

      如果您使用 C++11,请使用override 以避免此类错误。

      【讨论】:

        【解决方案4】:

        因为你的 CheckingAcount 有 writeCheck() 而 serviceChargeChecking 有 writeCheck(int);

        【讨论】:

        • 是的,那种东西很容易错过。尤其是在自己的代码中。
        【解决方案5】:

        这可能是因为你没有覆盖检查帐户的 writeCheck 方法,抽象原型是

        在检查账户类中

        virtual void writeCheck()=0;
        

        在 serviceChargeChecking 类中

         void writeCheck(int);
        

        注意参数,您没有覆盖检查帐户的 writeCheck,您可能(隐式地)继承了它,serviceChargeChecking 使用 int 参数创建了一个新的 writeCheck。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-06
          • 1970-01-01
          • 1970-01-01
          • 2015-10-26
          • 2020-02-10
          相关资源
          最近更新 更多