I have some code which adds a few objects to a vector. I then wish to retrieve a specific object from the vector and be able to both write out and edit its private member variables.
This is the code I currently have:
class Product {
public:
Product(int n, const string& na, int p)
: number(n), name(na), price(p) {};
void info() const;
private:
string name;
int number, price;
};
The member function looks like this:
void Product::info() const {
cout << number << ". " << name << " " price << endl;
}
I then create a vector and push into it some objects, like so:
vector<Product> range;
range.push_back(Product(1, "Bagpipe", 25));
To retrieve and list the information about all objects, I have the following function:
void listProducts (const vector<Product>& range1) {
for_each (range1.begin(), range1.end(), mem_fun_ref(&Product::info));
}
But this is where I get stuck.
To boil my problem down: I have no idea how to retrieve individual objects from the vector and edit them. I need to be able to search my vector for objects containing a specific number or name, and be able to retrieve either the information about all its private members, and also be able to edit all members.
Ideas I have about solutions thusfar are:
to create additional member functions that can return individual members
to create functions that, similar to the function I already have above, can search through each object in the vector and use the return from these additional member functions to compare with what I'm looking for
I don't quite know how I would go about editing an objects private members, but my current guess is that I would need members functions for this as well, and functions that tie in to those
Any help would be greatly appreciated! Even vague nudges in the right direction!