// Classes.cpp - understanding basics of classes and OOP in c++ #include #include #include //#include "Account.h" using namespace std; class Transaction { private: int amount; string type; public: Transaction(int amt, std::string t); string Report(); }; class Account //Declaring the class { private: int balance; vector log; //We are not using namespace std here as the compiler is going to paste the code into the main cpp file. // As a rule of thumb: do not include the namespaces into a header files public: Account(); //This is a constructor vector Report(); //Functions that is going to return a vector of strings bool Deposit(int amt); //Deposit function bool Withdraw(int amt); //Withdraw function }; Account::Account() :balance(0) //We are using scope resolution operator :: to show to the compiler and linker that we are implementing functions for a class Account, not any other class. { } //We are using initialization syntax: we are giving 0 values to variable balance //We do not need to initialize the vector of logs as it is initialized as empty one vector Account::Report() { vector report; report.push_back("Current Balance is " + to_string(balance)); report.push_back("Transactions: "); for (auto t : log) { report.push_back(t.Report()); } report.push_back("_______________"); return report; } bool Account::Deposit(int amt) { if (amt >= 0) { balance += amt; log.push_back(Transaction(amt, "Deposit")); return true; } else { return false; } } bool Account::Withdraw(int amt) { if (amt >= 0) { if (balance >= amt) { balance -= amt; log.push_back(Transaction(amt, "Withdraw")); return true; } else { return false; } } else { return false; } } Transaction::Transaction(int amt, string t) :amount(amt), type(t) {} string Transaction::Report() { string report; report = " "; report += type; report += " "; report += to_string(amount); return report; } int main() { Account a1; a1.Deposit(90); cout << "After depositing £90" << endl; for (auto s : a1.Report()) { cout << s << endl; } a1.Withdraw(50); cout << "After withdrawing £50" << endl; for (auto s : a1.Report()) { cout << s << endl; } return 0; }