[英]Inheritance in SuperClass and SubClasses using c++ giving error
我正在使用C ++進行銀行項目
編譯時,它給我一個錯誤
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) ^
以下是我的文件
#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
#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);
}
#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
#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 Im 正確構造子類的幫助,而該類因上面突出顯示的錯誤而卡住了。 它還顯示了另一個錯誤
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){} ^
任何幫助是極大的贊賞。 非常感謝
您在頭文件中定義了Account::Account
構造函數。
然后,您在account.cpp
(包含頭文件)中再次定義了它。 那是您重復的定義。
只在頭文件中聲明構造函數; 或將定義保留在那里,並將其從account.cpp
刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.