Making a copy is different from making a reference. Card second = first; creates a new object. For the class below, the compiler supplies a copy constructor that copies its members, so you do not need to write one. With value members such as the std::string here, changing the copied object's string does not change the original's string. A vector of integers behaves the same way when copied.
Compare this with Card& alias = first;. A reference names the existing object, so a change through the alias is a change to first. Neither form is automatically better. Choose a copy when you need an independent value and a reference when you intentionally need access to the existing object.
A class made only of members that already manage themselves usually needs no hand-written copying code. Do not generalize that statement to an owning raw pointer: copying a pointer copies an address, not the pointed-to object. We will deal with explicit ownership in the next section.
#include <iostream>
#include <string>
class Card {
public:
std::string heading;
Card(std::string text) : heading(text) {}
};
int main() {
Card first("Workshop");
Card second = first;
second.heading = "Meeting";
std::cout << first.heading << ' ' << second.heading << '\n';
}
The example prints Workshop Meeting. Both objects started with equal strings, but they have separate values. In the starter, Notebook copy = original has already made the copy. The call copy.rename(...) selects which object to change; inside rename, title refers to that object's member. Replace the empty body rather than changing the copy declaration or the original notebook.
rename() by assigning its text parameter to this object's title. The copy must become Weekend meals while the original stays Recipes.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.