1

I've been searching everywhere but I can't find a solution, though it's probably a simple one since I'm just starting out. Basically, I'm trying to pass in two values through a constructor, but the values I'm passing in aren't correct either when running or debugging.

Transaction.h

#include <string>

class Transaction {
  private:
    int amount;
    std::string type; 

  public:
    Transaction(int amt, std::string kind);
    std::string Report() const;

    // ...irrelevant code...
};

Transaction.cpp

#include "Transaction.h"
using namespace std;

Transaction::Transaction(int amt, std::string kind) { };

string Transaction::Report() const {
  string report;
  report += " ";
  report += type;  // supposed to concat "Deposit" to string
  report += " ";
  report += to_string(amount);  // supposed to concat amount to string

  return report;
  // This doesn't return the word "Deposit", nor does
  // it return the correct amount. I don't think that
  // this is adding "Deposit" to 50, because the values
  // change every time I run the program.
}

Parameters.cpp

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

// ...irrelevant code...

int main() {

  int money = 50;
  cout << "Depositing $" << money << endl;

  Transaction deposit(money, "Deposit");  
  // For some reason, this doesn't pass in int money.

  cout << "Original: " << deposit.Report() << endl;
  // And this cout prints some large value instead of 50.

  // ...irrelevant code...
}

No matter what I do, the value changes. Some output I get:

Depositing $50
Original:   13961048
After pass by value:   13961048
After pass by reference:   27922096

Depositing $50
Original:   11208536
After pass by value:   11208536
After pass by reference:   22417072

Depositing $50
Original:   14092120
After pass by value:   14092120
After pass by reference:   28184240

Any help to point me in the right direction (or just a straight answer) would be great!

2 Answers2

10

The values are being passed to the constructor, alright. The problem is just that you're not doing anything with them!

Look at the implementation for the constructor:

Transaction::Transaction(int amt, std::string kind) { };

This doesn't do anything. In particular, it doesn't save (store) the values of the parameters that were passed.

You probably want this:

Transaction::Transaction(int amt, std::string kind)
   : amount(amt)
   , type(kind)
{ }

The weird colon syntax is called a member initialization list, and does just what it sounds like.

Note that you should have been able to see this in the debugger. What you'd do is set a breakpoint on the definition of the constructor, and then check to see what the values of the parameters were. You'd see that they were being passed correctly (and that the values were not "wrong"). Then you'd have to figure out why they weren't getting saved, and you could have seen that pretty easily by single-stepping through the code from that point forward.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • You're amazing. Thanks for this! Just as a follow up, where was my program grabbing the number from? Just whatever was in memory? – MylesWritesCode May 07 '17 at 12:01
  • 1
    Depends on the type being initialised. When constructing an object, member variables of non-primitive types have their default constructor called unless otherwise initialised. In the case of `std::string` this yields the empty string. For the `int` primitive type the value is undefined so the compiler can place whatever value it likes in that location. – jsr___ May 08 '17 at 10:36
  • Thank you for the clarification! – MylesWritesCode May 08 '17 at 18:04
0

You don't pass the copy the value of the arguments in the class variables

here the correct contructor code :

Transaction::Transaction(int amt, std::string kind) { 
      this->amount=amt; //copy the value of amt to Transaction.amount
      this->type=kind;  //copy the value of kind to Transaction.kind

};
P.Carlino
  • 661
  • 5
  • 21