Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Account class program in c++

///

#include <iostream>

using namespace std;

class Account {

private:

int balance;

public:

Account(int initialBalance) {

if (initialBalance >= 0) {

balance = initialBalance;

} else {

balance = 0;

cout << "Error: Initial balance is invalid. Set to 0." << endl;

void credit(int amount) {

if (amount > 0) {

balance += amount;

void debit(int amount) {

if (amount > 0) {

if (amount <= balance) {

balance -= amount;

} else {

cout << "Debit amount exceeds account's balance." << endl;


}

int getBalance() const {

return balance;

};

int main() {

Account account1(0);

Account account2(500);

cout << " Initial balances " << endl;

cout << "Account 1 balance: " << account1.getBalance() << endl;

cout << "Account 2 balance: " << account2.getBalance() << endl;

account1.credit(300);

account2.credit(200);

account1.debit(100);

account2.debit(1000);

cout << " Balances after transactions " << endl;

cout << "Account 1 balance: " << account1.getBalance() << endl;

cout << "Account 2 balance: " << account2.getBalance() << endl;

return 0;

You might also like