I am doing an assignment in which we are supposed to code a very basic scenario of a company to manage their products. So when a new product is entered we are supposed to check if it already exists (which we are supposed to do by comparing the names of the products with the name of the product entered; empty products have an empty string as their name), if we do not find it, then we are supposed to add it to the list of products of the company(there can be a maximum of 3 product only so I am checking each one of them individually).
class Store : public Product {
public:
...
Product& return_product_1() {return product1;}
Product& return_product_2() {return product2;}
Product& return_product_3() {return product3;}
private:
Product product1;
Product product2;
Product product3;
...
};
So I thought that I can use a function which returns a reference to an object which is currently empty (Since all the three Product objects are individually stored in the class, so adding or removing an object is simply asking for its details from the user or setting the data members to NULL respectively), so I created the following function to find a particular Product
object. I pass a string n
to check whether any Product
object has a same name as n
and I also pass an empty Product
object so that i can make p
synonymous with the returned Product
since both of them are references. ( I did this because i wanted to return both the correct Product
object as well as a boolean. I could have used std::pair
to return both of them, but i did not wanted to do that.
bool StarberksInterface::search(string n, Product& p) {
if(store.return_product_1().product_name() == n) {
p = store.return_product_1();
return true;
}
else if(store.return_product_2().product_name() == n){
p = store.return_product_2();
return true;
}
else if(store.return_product_3().product_name() == n) {
p = store.return_product_3();
return true;
}
else return false; // The function returns true when a Product object which the same name as the first parameter is found
}
product_name
is defined as
std::string product_name() const {return name;}
where name
is a data member of type std::string
of Product
class.
The function search(string, Product&)
is called using the function call search("", p);
where i pass an empty string to check for a currently empty product and p is declared as Product p;
.
I declare another Product
object Product temp = input();
, where input
is declared as Product input();
and it asks the user for the information about the product and returns a Product
object. I have overloaded the assignment operator for Product
class.
I then write
p = temp;
Now I was thinking was that since return_product_1
would have returned a reference, and p
was assigned the value of return_product_1
, therefore p
and product1
should be synonymous ( Assuming that the search
function initially finds that product1
indeed has an empty string as a name). Any changes made to p
should be reflected in product1
, but it seems that is not the case. I wanted that the details of temp
should also be assigned to product1
since i assigned temp
to p
. Is there something wrong with my code or perhaps my logic?
Any help is appreciated. Thank You.