I'm trying to learn using headers in C++. My sample code for the bank as follows
In following code, I just want to get a simple DepositTransaction and WithdrawTransaction class and have them inherit from base class called Transaction.
I started with the header file:
transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <string>
class Transaction {
protected:
std::string _customerId;
std::string _tellerId;
public:
Transaction(const std::string& customerId, const std::string& tellerId);
std::string get_customer_id() const;
std::string get_teller_id() const;
virtual std::string get_transaction_description() const = 0;
};
class DepositTransaction : public Transaction {
private:
double _amount;
public:
DepositTransaction(const std::string& customerId, const std::string& tellerId, double amount);
std::string get_transaction_description() const override;
};
class WithdrawTransaction : public Transaction {
private:
double _amount;
public:
WithdrawTransaction(const std::string& customerId, const std::string& tellerId, double amount);
std::string get_transaction_description() const override;
};
#endif
Then the source file
transaction.cpp
#include "transaction.h"
#include <iostream>
Transaction::Transaction(const std::string& customerId, const std::string& tellerId)
: _customerId(customerId), _tellerId(tellerId) {}
std::string Transaction::get_customer_id() const {
return _customerId;
}
std::string Transaction::get_teller_id() const {
return _tellerId;
}
DepositTransaction::DepositTransaction(const std::string& customerId, const std::string& tellerId, double amount)
: Transaction(customerId, tellerId), _amount(amount) {}
std::string DepositTransaction::get_transaction_description() const {
return "Deposit: " + std::to_string(_amount);
}
WithdrawTransaction::WithdrawTransaction(const std::string& customerId, const std::string& tellerId, double amount)
: Transaction(customerId, tellerId), _amount(amount) {}
std::string WithdrawTransaction::get_transaction_description() const {
return "Withdraw: " + std::to_string(_amount);
}
Finally, main.cpp
int main (){
DepositTransaction deposit("12345", "Teller001", 1000);
WithdrawTransaction withdraw("67890", "Teller002", 500);
std::cout << deposit.get_customer_id() << std::endl;
std::cout << withdraw.get_teller_id() << std::endl;
std::cout << deposit.get_transaction_description() << std::endl;
std::cout << withdraw.get_transaction_description() << std::endl;
}
But when running, I get the error
the prelaunchtask c/c++ g++ build active file terminated with exit code
Upon clicking "show errors", I get
no problems have been detected in the workspace
I tried to fix it according to this post here but no dice