使用c ++给出错误的SuperClass和SubClasses中的继承

时间:2016-09-10 01:11:46

标签: c++ inheritance casting subclass superclass

我正在使用C ++开发一个Banking项目

编译时,它给我一个错误

C:\Qt\Qt5.3.0\Tools\QtCreator\bin\FinanceApp\account.cpp:5: error: redefinition of 'Account::Account(QString, QString, double, QString)' Account::Account(QString cn, QString an, double ir, QString ty) ^

以下是我的文件

父类

account.h

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <QString>
#include <QList>
#include <transaction.h>


class Account
{
public:
    Account(QString cn, QString an, double ir, QString ty) : custName(cn), accNum(an), interestRate(ir), type(ty){}
    virtual ~Account();
    QString getCustName();
    QString getAccNum();
    QList<Transaction> getTransaction();
    virtual QString toString();
    QString getType();
    void setType(QString ty);
    double getInterestRate() const;
    virtual void transaction(double amt) = 0;
    virtual void calcInterest() = 0;
    void getAccountInfo();

protected:
    double balance;
    QList<Transaction> transactions;
private:
    QString custName;
    QString accNum;
    double interestRate;
    QString type;
};

#endif // ACCOUNT_H

account.cpp

#include "account.h"
#include <iostream>
#include <conio.h>

Account::Account(QString cn, QString an, double ir, QString ty)
{
}

QString Account::toString(){

    QString accountString;
    accountString += "ACCOUNT DETAILS";
    accountString += "---------------";
    accountString += "\n\tAccount No    : " + accNum;
    accountString += "\n\tAccount holder: " + custName;
    accountString += "\n\tBalance       : " + QString::number(balance,'f',2);
    accountString += "\n\n\tTransactions";
    accountString += "\n\n\t------------";

    foreach(Transaction single_transact, this->transactions)
    {
        accountString += "\n\t\t" + single_transact.toString();
    }

    return accountString;
}

QString Account::getCustName(){
    return this->custName;
}

QString Account::getAccNum(){
    return this->accNum;
}

QList<Transaction> Account::getTransaction(){
    return this->transactions;
}

QString Account::getType(){
    return this->type;
}

void Account::setType(QString ty){
    this->type = ty;
}

double Account::getInterestRate()const{
    return this->interestRate;
}

void Account::getAccountInfo(){

    std::string cn, an, ty;


        std::cout<< "\n\nEnter Customer Name :- ";
        std::cin>> cn;
        std::cout<< "Enter Account Number :- ";
        std::cin>> an;
        std::cout<< "Enter Account Type :- ";
        std::cin>> ty;

        this->custName = QString::fromStdString(cn);
        this->accNum = QString::fromStdString(an);
        this->type = QString::fromStdString(ty);

}

子类

savingsaccount.h

#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
#include <QString>
#include "account.h"

class SavingsAccount : public Account
{
public:
    SavingsAccount();
    SavingsAccount(QString cn, QString an, double ir, QString ty);
    double minIntEarnedForPoints;
    void transaction(double amt);
    void calcInterest();
    int getPoints();
    QString toString();
    void make_withdrawal();
    void make_deposit();
    void print_statement();
    void print_balance();
    void print_account_type();
    void print_points_earned();
private:
    int points;
};

#endif // SAVINGSACCOUNT_H

savingsaccount.cpp

#include "savingsaccount.h"
#include <QString>
#include <QList>
#include <iostream>
#include <conio.h>

SavingsAccount::SavingsAccount(QString cn, QString an, double ir, QString ty):{
    this->custName = cn;
    this->accNum = an;
    this->interestRate = ir;
    this->type = ty;
}

void SavingsAccount::transaction(double amt){

    QString transaction_type;

    this->setType("Savings");

    if(amt < 0){
        transaction_type = "Withdrawal";
    }else{
        transaction_type = "Deposit";
    }

    if(this->balance >= amt){

        this->balance += amt;

        Transaction _t = Transaction(transaction_type, amt);

        this->transactions::append(_t);

        std::cout << "Transaction successful.\n";

        std::cout << this->toString() + "\n" + transaction_type + "\t:\t" + amt;

    }else{
        std::cout << "Insufficient funds.";
    }

}

void SavingsAccount::calcInterest(){

    QString transaction_type = "Interest";

    double new_interest = this->balance * (this->getInterestRate()/100) * (1/12);

    this->balance += new_interest;

    int points_earned = 0;

    if(new_interest > this->minIntEarnedForPoints){
        points_earned = (int)new_interest;
    }
    this->points += points_earned;
    Account::transactions.append(new Transaction(transaction_type, new_interest));

    std::cout << "Transaction successful.\n";

    std::cout << this->toString() + "\n" + "Interest" + "\t:\t" + new_interest;

    std::cout << "\nPoints \t:\t"+QString::number(points_earned);

}

void SavingsAccount::print_points_earned(){

    std::cout << "Points Earned: " << this->points;

}

void SavingsAccount::getPoints(){

    return this->points;

}

QString SavingsAccount::toString(){

    QString savings_account = "SAVINGS ACCOUNT\n";
    savings_account += "------------------------------";
    savings_account += Account::toString();
    savings_account += "\nMinimum Earned Points: " + QString::number(this->getPoints(),'f',2);
    savings_account += "\n\n";
    return savings_account;

}

void SavingsAccount::make_withdrawal(){
    double amt;
    std::cout <<"\nEnter amount to Withdraw :- ";
    std::cin>>amt;
    double amount = 0-amt;
    transaction(amount);
}

void SavingsAccount::make_deposit(){
    double amt;
    std::cout <<"\nEnter amount to Deposit :- ";
    std::cin>>amt;
    transaction(amt);
}

void SavingsAccount::print_statement(){

    std::cout << "Statement";
    std::cout << "---------";

    foreach(Transaction single_transact, this->transactions)
    {
        std::cout << single_transact.toString();
    }
}

void SavingsAccount::print_balance(){

    std::cout << "ChequeAccount Balance: " << this->balance;

}

void SavingsAccount::print_account_type(){

    std::cout << this->toString();
}

我需要有关如何使用SuperClass 正确构建SubClass的帮助 我坚持上面突出显示的错误。它还显示另一个错误

C:\Qt\Qt5.3.0\Tools\QtCreator\bin\FinanceApp\account.h:11: error: 'Account::Account(QString, QString, double, QString)' previously defined here Account(QString cn, QString an, double ir, QString ty) : custName(cn), accNum(an), interestRate(ir), type(ty){} ^

非常感谢任何帮助。非常感谢你

1 个答案:

答案 0 :(得分:1)

您在头文件中定义了Account::Account构造函数。

然后您在account.cpp中再次定义了它,其中包含头文件。那是你的重复定义。

要么只在头文件中声明构造函数;或保留定义,并将其从account.cpp中删除。