0

I'm beginner in OOP and I have problem with definition of constructor. headfile.h:

#ifndef RACHUNEK_H_INCLUDED
#define RACHUNEK_H_INCLUDED

#include <string>
class Rachunek
{
   std::string surname;
   std::string nr_account;
   double balance;
public:
   Rachunek();
   Rachunek(std::string & name,std::string & nr,double s = 0.0);
   ~Rachunek(){};
   void show();
   void give(const double m);
   void get(const double m);

};

#endif // RACHUNEK_H_INCLUDED

file .cpp:

#include <iostream>
#include <string>
#include "rachunek.h"
using namespace std;

Rachunek::Rachunek() //default
{
  surname = "not specified";
  nr_account = "0000-0000-0000-0000";
  balance = 0.0;
}
Rachunek::Rachunek(string & name, string & nr, double s = 0.0) //mysecond
{
  surname = name;
  nr_account = nr;
  balance = s;
}

the problem is the definition of a constructor. I don't know what is wrong...

karkas22
  • 13
  • 4
  • And what is the error you are getting? –  Jan 09 '17 at 18:19
  • Ok. Error was in assignment in definition :I I was looking for this for a long time – karkas22 Jan 09 '17 at 18:25
  • 1
    In addition to the compile error, you would never ever use floating point for at bank account balance. That makes the auditors go nuts over the rounding errors. Instead use cents and fix the output to show 2 decimals. Also, you can never create an account with an initial non-zero balance. The funds must always be transferred from some other account. (And yes, I'm working for a bank :-) – Bo Persson Jan 09 '17 at 18:29
  • Ok ok :D This is exercise from book and I'm don't focus on the detail of the programme and I'm not working for a bank :) Thanks for economy lessons ;) – karkas22 Jan 09 '17 at 18:43

1 Answers1

2

the problem is the definition of a constructor. I don't know what is wrong...

You are not allowed to have default values in the definition of the function. Default values are allowed only in the declaration. Use:

Rachunek::Rachunek(string & name, string & nr, double s) :
   surname(name),
   nr_account(nr),
   balance(s)
{
}

I suggest changing the implementation of the other constructor to initialize the member variables using initializer list syntax.

Rachunek::Rachunek() :
  surname("not specified"),
  nr_account("0000-0000-0000-0000"),
  balance(0.0)
{
}

If you are able to use a C++11 compiler that can be further simplified by using a delegating constructor.

Rachunek::Rachunek() : Rachunek("not specified", "0000-0000-0000-0000", 0.0)
{
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270