Question: Can you please correct my code? Main.cc -------------------------- #include #include #include product.h int main() { std::shared_ptr avocado = std::make_shared (avocado, 0.68, nullptr, nullptr); std::shared_ptr apple

Can you please correct my code?

Main.cc

--------------------------

#include #include

#include "product.h"

int main() { std::shared_ptr avocado = std::make_shared("avocado", 0.68, nullptr, nullptr); std::shared_ptr apple = std::make_shared("apple", 0.84, nullptr, nullptr); std::shared_ptr banana = std::make_shared("banana", 0.20, apple, avocado); std::shared_ptr pineapple = std::make_shared("pineapple", 1.98, nullptr, nullptr); std::shared_ptr lemon = std::make_shared("lemon", 0.54, banana, pineapple);

std::shared_ptr root = lemon;

std::cout << "Price of a pineapple: "; std::cout << root->FindPrice("pineapple") << " "; }

--------------------------

product.h

-----------------------

#include #include

class Product { public: Product(std::string &name, double price, std::shared_ptr right, std::shared_ptr left) : name_(name), price_(price), right_(right), left_(left) {} const std::string &Name() const { return name_; } double Price() const { return price_; }

// Implement a recursive `FindPrice` member function that accepts the name of a `Product` and returns its price. If the product name does not match the product or any of its linked products, return -1.0.

double FindPrice(std::string &name) { if (name_ == name) { return price_; } else if (name_ != name && right_ != nullptr && left_ != nullptr) { return -1; } else if (name > name_ && right_ != nullptr) { return right_->FindPrice(name); } else { return left_->FindPrice(name); } }

private: std::string name_; double price_; std::shared_ptr right_; std::shared_ptr left_; };

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!