0

I'm trying to pass down the input values of variables from the base class Watch to the derived class Repair. I have tried to insert getter and setter function inside the Watch class but it doesn't work. Please help! For example, I insert "antique" as the watch variable but it doesn't pass down to the watch variable inside the Repair class.

#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

class Watch
{
protected:
    int price, age;
    string watch;

public:
    void type_watch()
    {
        cout<<"\nHow much is your watch?\n";
        cin>>price;

        cout<<"And, how old is your watch?\n";
        cin>>age;

        if(price>800&&age<=50)
        {
            watch=="designer";
            cout<<"\nYour watch is classified as designer.\n";
        }
        else if(price<=800&&age<=50)
        {
            watch=="non-designer";
            cout<<"\nYour watch is classified as non-designer.\n";
        }
        else
        {
            watch=="antique";
            cout<<"\nYour watch is classified as antique.\n";
        }
    }
};

class Repair:public Watch
{
    string gold_silver, free_repair;
    float repair_price;

public:
    void repair_watch()
    {
       cout<<"\nYour watch is classified as "<<watch<<".\n";

        if(watch=="designer")
        {
            cout<<"Is your watch made of gold or silver?\n";
            cin>>gold_silver;

            if(gold_silver=="gold")
            {
                repair_price=price*(1/4);
            }
            else if(gold_silver=="silver")
            {
                repair_price=price*(1/3);
            }
            else
            {
                cout<<"Sorry, no matches.\n";
                return;
            }
        }
        else if(watch=="non-designer")
        {
            cout<<"Is it on the free repair list?\n";
            cin>>free_repair;

            if(free_repair=="yes")
            {
                repair_price=0;
            }
            else if(free_repair=="no")
            {
                repair_price=price*0.15;
            }
        }
        else if(watch=="antique")
        {
            repair_price=age/2;
        }
    }

    void total_price()
    {
        cout<<"The total cost of repair is $"<<fixed<<setprecision(2)<<repair_price<<".\n";
    }
};

int main(void)
{
    string ans, repair_watch;
    Watch w;
    Repair r;

    cout<<"~~~Welcome to ABC Watches~~~\n\n";
    while(ans!="no")
    {
        cout<<"\nWould you like to classify your watch?\n";
        cin>>ans;

        if(ans=="no")
        {
            cout<<"\nThank you for choosing ABC Watches! Good bye!\n";
        }
        else if(ans=="yes")
        {
            w.type_watch();
            cout<<"\nWould you like to repair your watch?\n";
            cin>>repair_watch;

            if(repair_watch=="yes")
            {
                r.repair_watch();
                r.total_price();
            }
        }
    }
    return 0;
}
  • 1
    Seems like an design problem. Should `repair` inherit `watch`? Or just take `watch` as an input? – Louis Go Jul 27 '20 at 04:01
  • 3
    Voting to close as a typo, since your assignments are actually boolean comparisons (whose results are immediately discarded). – Stephen Newell Jul 27 '20 at 04:02
  • So I used = operator instead of == but still not passing down :( why is it still discarded? –  Jul 27 '20 at 04:17
  • 1
    Repair is an action, something you do to a watch, not a thing unto itself. It probably should not be a class and definitely should not [be a watch](https://en.wikipedia.org/wiki/Is-a). Definitely violates the [Liskov Substitution Principle](https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle). – user4581301 Jul 27 '20 at 04:40
  • 1
    `Watch w;` This defines a `Watch` object `w`, but then `Repair r;` defines a `Repair` object `r` which is derived from `Watch`. So `w.type_watch();` gets called for one object, then `r.repair_watch();` for the other completely different object, whose `Watch` subobject has never been initialized. – dxiv Jul 27 '20 at 04:43
  • oh I understood thanks –  Jul 27 '20 at 17:37

1 Answers1

3

Summary

  1. You don't need both the Watch w; and Repair r; variables - just use r for everything
  2. You're not assigning to the variable watch - instead you're comparing it using operator==()

Comment on design

First of all, I wouldn't recommend using inheritance for this. While it can be done, inheritance is used to model an "x is a y" relationship. In your code, Repair inherits from Watch, but Repair is NOT a Watch. I'd recommend looking into some C++ Inheritance tutorials on YouTube for some more details on this.

Solution to problem

Now for your question... :P

---------- Issue #1 ----------

In your main() function, you create two variables: Watch w; and Repair r;. It is hopefully clear that w and r do NOT share any memory whatsoever, as they are completely separate variables.

When you call w.type_watch();, you store the console input data into w. And then when you call r.repair_watch(); and r.total_price();, you access the data stored in r (NOT w where the data was stored). What ends up happening is that you end up reading empty strings stored in r, so you don't get the result you expect.

HOWEVER, there is a simple fix :)

r technically contains two components built on top of each other - a Watch component and a Repair component - because Repair inherits from Watch. Because of this, r contains all variables and functions found within the Watch class.

Long story short, you DON'T need to create an extra Watch variable yourself. So get rid of that Watch w; variable completely. Simply substitute r in every place where you were using w. Your int main() function then becomes the following:

int main(void)
{
    string ans, repair_watch;
    //Watch w;  <-- REMOVE this variable
    Repair r;

    cout<<"~~~Welcome to ABC Watches~~~\n\n";
    while(ans!="no")
    {
        cout<<"\nWould you like to classify your watch?\n";
        cin>>ans;

        if(ans=="no")
        {
            cout<<"\nThank you for choosing ABC Watches! Good bye!\n";
        }
        else if(ans=="yes")
        {
            r.type_watch();  // <-- Use 'r' variable here
            cout<<"\nWould you like to repair your watch?\n";
            cin>>repair_watch;

            if(repair_watch=="yes")
            {
                r.repair_watch();
                r.total_price();
            }
        }
    }
    return 0;
}

---------- Issue #2 ----------

Finally, you're also using the comparison operator operator==() which returns a bool (true/false) instead of assigning to your watch variable.

Instead of doing:

if(price>800&&age<=50)
{
    watch=="designer";
    cout<<"\nYour watch is classified as designer.\n";
}

You should be doing:

if(price>800&&age<=50)
{
    watch = "designer";
    cout<<"\nYour watch is classified as designer.\n";
}
  • Aside: Careful when searching for tutorials. There's a lot of misleading and outright bad material out there, and if you don't already almost know what you're looking for, odds are you won't be able to tell a good tutorial from a bad one. Bit of a catch 22 there. – user4581301 Jul 27 '20 at 21:52