c++ - c++中抽象类的问题

标签 c++ class virtual abstract

主要内容:

#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 创建对象错误告诉我我不能从抽象类创建对象,但它不会对我来说似乎很抽象。

最佳答案

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

关于c++ - c++中抽象类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16441958/

相关文章:

c++ - c++ 中的 std::string 与字符串

c++ - 按值从 vector 中获取对象

c++ - 自定义 "Very Long Int"分区问题

C++ 模板,在不初始化的情况下声明对象

c++ - 重写虚拟方法时,Cat 与 Animal 不协变

C++继承纯虚函数

c++ - 进行少量插入时应该使用哪个 STL 容器?

c++ - 返回指向 vector 数据的指针的最佳方法是什么? const 还是非常量?

flash - 访问 AS3 中的 Document 类

windows - 虚拟 wifi 的最大连接数/客户端数的注册表设置?