1

So id like to make a simply bank account in c++ with simple methods to add or withdraw money and check the current balance but it doesn't compile and i don't know why. Variables and methods are in german but it should be clear i hope.

konto.h:

#include <stdio.h>
#include <string>

class Konto
{
public:
    Konto();
    ~Konto();
    void einzahlen(float geld);
    void abheben(float geld);
    float kontostand();
    float balance;

};

konto.cpp

#include "konto.h"

Konto::Konto()
{
    balance = 0;
}

Konto::~Konto()
{

}

void Konto::einzahlen(float geld)
{
    balance += geld;
}

void Konto::abheben(float geld)
{
    balance -= geld;
}

float Konto::kontostand()
{
    return balance;
}

main.cpp

#include "konto.h"

void getKontostand(Konto k)
{
    printf("Aktueller Kontostand: %f⁄n", k.kontostand());
}
int main()
{
    Konto k(0);
    k.einzahlen(1000);
    k.abheben(400);
    k.abheben(400);
    getKontostand(k);
}
VYZN
  • 23
  • 3
  • 2
    In the future, please post the relevant error messages you are getting. Just saying 'doesn't compile' has very little information to go on. – MicroVirus May 26 '15 at 23:33

1 Answers1

4

Your constructor is declared parameterless

Konto::Konto()

but you pass one int argument in main()

Konto k(0);

You may consider using a default argument:

Konto::Konto(int bal = 0): balance(bal){}

so you now have the option of specifying the initial balance,

Konto k(42); // we can specify the initial balance

or use the default

Konto k(); // initial balance is by default 0
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Ok i think i understand that but i´m to new to c++ to know how to fix this issue. What do i have to change/add to make it work. – VYZN May 26 '15 at 23:33
  • Just declare `Konto::Konto(int bal = 0);` in your header file `konto.h`, then change the implementation in the `konto.cpp` file to `Konto::Konto(int bal): balance(bal) { }`. Then, pick a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ;) – vsoftco May 26 '15 at 23:35
  • Thanks sir ! I´ll definitely do that :) – VYZN May 26 '15 at 23:48