This code:
Code:
Copy::Copy(int aID, string aName)
{
id = aID;
copyName = aName;
Borrower b(0, "");
}
is incorrect in both Java and C++. In Java, if you create an object at local scope inside a function and you don't return a reference to the object or assign the object to a member variable, then there won't be any references to the object in existence, and the object will be destroyed once the function finishes executing(more accurately the object will be subject to automatic garbage collection). In C++, it doesn't matter either way: an object created at local scope is destroyed once the function terminates, and any reference to the object will be invalid and cause your program to crash if you try to use it.
Your question about how to pass objects in C++ gets to the heart of many of the differences between the two languages. You will need to get a C++ book, and learn about "passing by value" versus "passing by reference", which in turn requires you to learn about pointers and references. Also important will be learning how to dynamically allocate memory for a pointer, and the differences between statically and dynamically allocating memory for variables. Essentially, that means you have to read a whole book on beginning C++.
Here is a taste of what you will learn. In java, all memory is dynamically allocated with the new operator, e.g.:
Borrower borrower = new Borrower(10, "some text");
You probably weren't even aware that in java you were "dynamically allocating memory", and you used that syntax as a matter of course because it's the only way you can create objects in java. In C++, that is not the case, and you can create objects in different ways. You can do this:
Borrower borrower(10, "some text");
or this:
Borrower* myPointer = new Borrower(10, "some text");
And, you can create a reference to an object like this:
Borrower b(10, "some text");
Borrower& myRef = b;
Confused yet? It only gets more complicated.
The purpose of a constructor is to initialize the data members, so you want to initialize the borrower data member as well. In java, you might do this:
Code:
public Copy(int id, string name, Borrower b)
{
this.id = id;
this.name = name;
borrower = b;
}
In C++, however, objects can be passed to a function "by value" or "by reference". When you pass an object by value, it is copied, and the copy of the object is sent to the function. Inside the function, if you try to change the original object using the copy, it won't work. All you will end up doing is changing the copy, and when the function ends, the copy will be destroyed, leaving the original object unchanged. To pass an object by value, the syntax works like this:
public Copy(int id, string name, Borrower b)
That looks identical to the Java syntax for sending a reference to a function. In C++, the effect is totally different. Since there is no such thing as passing by value in java, it will be a new concept you will have to learn about.
In C++, if you want to pass an object by reference, then you either use a pointer type or a reference type for the parameter:
public Copy(int id, string name, Borrower* ptr) //pointer variable
public Copy(int id, string name, Borrower& ref) //reference variable