DMarlon Studio

Lesson 1 · 1 of 20

Classes and const methods · C++

Write your own C++ classes: members, a constructor and const methods, with a bank account that takes deposits and withdrawals.

C++ · Classes and const methods in DMarlon Studio: the example's code and its output in the terminal

A class bundles some data with the functions that work on it. BankAccount has two members, owner and balance, and three methods: deposit, withdraw and print. With public: everything is reachable from outside (the next lesson shows how to hide it). Each variable of this type, such as ana or luis, is an object with its own data.

The constructor has the same name as the class and returns nothing: it runs every time an object is created. BankAccount ana("Ana", 120.0); calls it with the owner and the opening balance, which it stores in the members.

Methods are called with a dot: ana.deposit(30.5) adds to Ana's balance and leaves Luis's alone. withdraw first checks there is enough money: Luis cannot take 60 out of his 40 euros (it returns false), but he can take 25, leaving 15.

The `const` after print() promises that the method does not change the object. That matters because a const reference such as view only lets you call const methods: view.print() works, while view.deposit(5) would not compile. Mark every method that only reads as const.

In DMarlon Studio, C++ is the language behind native desktop apps built with FLTK, and classes like this one are their foundation. This program runs as it is with the Console target: press F9.

Code · main.cpp

#include <iostream>#include <string>// A class groups data (members) with the functions that work on it.class BankAccount {public:    std::string owner;    double balance;    // Constructor: runs every time an account is created.    BankAccount(std::string name, double initial) {        owner = name;        balance = initial;    }    void deposit(double amount) { balance += amount; }    bool withdraw(double amount) {        if (amount > balance) return false;   // not enough money        balance -= amount;        return true;    }    // const: this method promises not to change the object.    void print() const { std::cout << owner << ": " << balance << " EUR\n"; }};int main() {    BankAccount ana("Ana", 120.0);    BankAccount luis("Luis", 40.0);    ana.deposit(30.5);    std::cout << "Luis withdraws 60? " << (luis.withdraw(60) ? "yes" : "no") << "\n";    std::cout << "Luis withdraws 25? " << (luis.withdraw(25) ? "yes" : "no") << "\n";    ana.print();    luis.print();    const BankAccount& view = ana;   // read-only: only const methods are allowed    view.print();                    // view.deposit(5) would not compile}

Output · c++ -std=c++17 main.cpp -o main && ./main

Luis withdraws 60? no
Luis withdraws 25? yes
Ana: 150.5 EUR
Luis: 15 EUR
Ana: 150.5 EUR

Key points

  • A class groups members (data) and methods (functions).
  • The constructor is named after the class and sets up each new object.
  • object.method() works on that object's own data.
  • A const method does not change the object and can be called through a const reference.
  • A class ends with };: do not forget the semicolon.

Exercise

Add a transfer(BankAccount& to, double amount) method that uses withdraw and, if it succeeds, calls to.deposit(amount). Move 50 euros from Ana to Luis and print both accounts.