inheriting mutator functions?

So I'm trying to figure out what is wrong with this program but Cay Horstmann doesn't want to explain it to me :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef ENTREE_H
#define	ENTREE_H

#include <string>

using namespace std;

// Constructor for an entree object
class Entree
{
public:
    /**
     * Makes an entree object
     * @param type description of entree type
     * @param price the entree price
     * @param veggie if the entree does not contain meat
     */
    Entree(string type, double price, bool veggie);
    Entree();

    void set_type(string new_type);

    void set_price(double new_price);

    void set_veggie(bool new_veggie);

    string get_type() const;

    double get_price() const;

    bool is_veggie() const;

    virtual void print() const;

    virtual void howToCook() const;

private:
    string type;
    double price;
    bool veggie;
};

#endif	/* ENTREE_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef ITALIAN_H
#define	ITALIAN_H
#include "entree.h"

class Italian : public Entree
{
public:
    /**
     * Creates an Italian Entree
     * @param type entree type
     * @param price entree price
     * @param veggie entree veggie
     * @param oliveOil the cups of olive oil
     */
    Italian(string type, double price, bool veggie, int oliveOil);
    Italian();

    /**
     * changes the amount of olive oil
     * @param new_oliveOil amount of cups
     */
    void set_oliveOil(int new_oliveOil);

    /**
     * @return oliveOil
     */
    int get_oliveOil() const;

    virtual void print() const;

    virtual void howToCook() const;

private:
    string type;
    double price;
    bool veggie;
    int oliveOil;
};


#endif	/* ITALIAN_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "entree.h"
#include <iostream>

Entree::Entree(string type, double price, bool veggie)
:   type(type), price(price), veggie(veggie)
{
}

Entree::Entree()
:   type(""), price(0), veggie(false)
{
}

void Entree::set_type(string new_type)
{
    type = new_type;
}

void Entree::set_price(double new_price)
{
    price = new_price;
}

void Entree::set_veggie(bool new_veggie)
{
    veggie = new_veggie;
}

string Entree::get_type() const
{
    return type;
}

double Entree::get_price() const
{
    return price;
}

bool Entree::is_veggie() const
{
    return veggie;
}

void Entree::print() const //called "virtual" in header
{
    cout << "\"" << get_type << "\":\t$" << get_price;
    if(veggie)
        cout << " (Vegetarian)";
    cout << endl;
}

void Entree::howToCook() const //also virtual
{
    cout << "No directions" << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "Italian.h"
#include <iostream>

Italian::Italian(string type, double price, bool veggie, int oliveOil)
:   type(type), price(price), veggie(veggie), oliveOil(oliveOil)
{
}

Italian::Italian()
:   type("Dough"), price(1.5), veggie(true), oliveOil(2)
{
}

void Italian::set_oliveOil(int new_oliveOil)
{
    oliveOil = new_oliveOil;
}

int Italian::get_oliveOil() const
{
    return oliveOil;
}

void Italian::print() const
{
    cout << "\"" << type << "\":\t$" << price << "\t(" << oliveOil << " cups olive oil)";
    if(veggie)
        cout << " (Vegetarian)";
    cout << endl;
}

void Italian::howToCook() const
{
    cout << "Just throw oregano and garlic on everything" << endl;
}


Now when I try this:

1
2
3
4
5
6
    Italian entree;
    entree.set_type("Pizza");
    entree.set_price(0.66);
    entree.set_veggie(1);
    entree.set_oliveOil(100);
    entree.print();


My output is
"Dough":      $1.5    (100 cups of olive oil) (Vegetarian)


I tried to fix it by replacing "type" with "this->get_type" (as well as price and oliveOil) in Italian::print, but the compiler got angry with me.
Last edited on
I see what happened! when you created the Italian class, you didn't take into account that the entree class has some of the same variable names as Italian, so now when you output your price, you are outputting the price of Italian.price but since set_price is only part of Entree, it can not access the Italian.price so it sets the Entree.price.

Solution, take out some of the variables in Italian that you do not need since you can access the variables in Entree directly!
I don't understand, which variables I can omit without the compiler complaining? If I have to overwrite my set functions so I can access variables in Italian then why did I inherit Entree in the first place?
Last edited on
When you create a class that is also a part of another class

class dog : class animal

Then dog has access to all that animal has BUT animal does not have acces to all that dog has. Think of it this way.

1
2
3
4
5
6
7
8
9
10
class animal
{
   void breathe;
};

class dog : animal
{
   void Bark;
   void WagTail;
};


Now in this very crude and basic example, class animal is set up to only breathe. All animals breathes in this program's world. Now a dog is an animal, but it does a lot more. So a dog case Bark and Wag its little or huge tail, but it still can also breathe since it is also an animal. It is like dog was created like this:

1
2
3
4
5
6
class dog
{
   void breathe;
   void Bark;
   void WagTail;
}


Both the codes basically say the same thing. Hopefully this helps you to understand. The term for this is class inheritance.

Now if you have the same variables or functions in dog and animal, it will run the one in Dog first, but you will have potential problems if you are not careful like you have. You used the same exact variables in Entree and Italian, so if a function is only part of Entree, then it can not access the variables in Italian but Italian can access both Italian's variables and Entree's variables. Since it has the choice, it will always choose Italian first though so since the function you ran could only access the Entree, it changed that but when you wanted a display, it has access to the Italian variables.

An easier way to say it, take out every variable in Italian that is also in Entree since Italian can use those variables without a problem. That will eliminate your problems.
Topic archived. No new replies allowed.