Menu
Coddy logo textTech

Get Balance

Part of the Object Oriented Programming section of Coddy's C journey — lesson 26 of 61.

challenge icon

Challenge

Easy

Your Bank Account is almost complete! You've built the ability to create accounts, deposit funds, and withdraw money—but there's one crucial piece missing. Right now, external code has no way to see the current balance. Let's fix that by adding a getter function.

A getter is the proper way to expose hidden data in an encapsulated module. Instead of making the balance field public (which would break encapsulation), you provide a read-only function that returns the value. This keeps the data protected while still allowing controlled access.

You'll continue working with your three files:

  • account.h: Add a new function prototype: get_balance. This function takes a const Account * (since it only reads data, never modifies it) and returns a double representing the current balance. Keep all your existing declarations.
  • account.c: Implement the get_balance function. It simply returns the hidden balance field from the account. Notice how this function can access the internal data because it's defined in the same file as the struct—external code still can't peek inside directly.
  • main.c: Now you can finally see what's happening inside your accounts! Create an account, perform some transactions, and use your new getter to display the balance at each step.

You will receive four inputs: the account ID (an integer), a deposit amount, a withdrawal amount, and a second deposit amount.

In your main file, create an account and print its initial balance (should be 0.00). Deposit the first amount and print the new balance. Withdraw the specified amount and print the balance again. Finally, make the second deposit and print the final balance. Then destroy the account.

Print the output in this format:

Account 1001 created
Balance: 0.00
Balance: 500.00
Balance: 200.00
Balance: 350.00
Account destroyed

Where 1001 is the account ID, and the balances reflect: initial (0.00), after depositing 500.00, after withdrawing 300.00, and after depositing another 150.00. Format all balance values with two decimal places using %.2f.

With this getter in place, your Bank Account module now provides complete, controlled access to account data—users can see the balance but can only modify it through your validated deposit and withdraw functions.

Try it yourself

#include <stdio.h>
#include "account.h"

int main() {
    int account_id;
    scanf("%d", &account_id);
    
    double deposit1;
    scanf("%lf", &deposit1);
    
    double withdraw_amount;
    scanf("%lf", &withdraw_amount);
    
    double deposit2;
    scanf("%lf", &deposit2);
    
    Account* account = create_account(account_id);
    printf("Account %d created\n", account_id);
    
    // TODO: Print the initial balance using get_balance
    
    deposit(account, deposit1);
    // TODO: Print the balance after the first deposit
    
    withdraw(account, withdraw_amount);
    // TODO: Print the balance after the withdrawal
    
    deposit(account, deposit2);
    // TODO: Print the balance after the second deposit
    
    destroy_account(account);
    printf("Account destroyed\n");
    
    return 0;
}

All lessons in Object Oriented Programming