Why doesnt this work?

I am trying to tell the program that cow's under 1 year old and over 10 years old do not, produce milk.
The equation or formula or whatever you want to call it is in the .cpp at the bottom.

Main.cpp
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
#include <iostream>
#include "DairyAnimal.h"
#include "Cow.h"

using namespace std;



int main()
{
DairyAnimal DA, DA1(1.0, 100);
DA.feed(20);
DA1.feed(2);
Cow bertha(0.9, 400), mary(1.0, 900), gretchen(10, 1100), lucy(10.1, 1000);
bertha.feed(20);
mary.feed(15);
gretchen.feed(25);
lucy.feed(30);
    cout<<"Gallons of Milk :: "<< DA.milk()<< endl;
    cout<<"Gallons of Milk :: "<< DA1.milk()<< endl;
    cout<<"Gallons of Milk :: "<< bertha.milk()<< endl;
    cout<<"Gallons of Milk :: "<< mary.milk()<< endl;
    cout<<"Gallons of Milk :: "<< gretchen.milk()<< endl;
    cout<<"Gallons of Milk :: "<< lucy.milk()<< endl;
    cout << "Hello world!" << endl;
    return 0;
}


Cow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef COW_H
#define COW_H
#include "DairyAnimal.h"


class Cow : public DairyAnimal
{
    public:
        Cow();
        Cow(float a, int w){age = a; weight = w;};
        float milk();
        virtual ~Cow();
    protected:
    private:

};

#endif  



Cow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Cow.h"

Cow::Cow()
{
    //ctor
}

float Cow::milk()
{
    static float m = 0;
if(Cow().age >= 1 && Cow().age <= 10)

    m = 0.5 * weight / food;

else m = 0;
    return m;
}


Cow::~Cow()
{
    //dtor
}
When you're asking for help, it's common courtesy and common sense to tell us how "it's not working". Do you get compilation errors? Do you get run-time errors? Is the program behaving unexpectedly? Keep that in mind next time you ask a question.

As for your problem - fortunately, it's an easy one.

The Cow class doesn't have any members named "age", "weight" or "food", so how do you expect line 11 in Cow.cpp to work? Even if the Cow class had those members, the way you're trying to access them makes very little to no sense.

Woops, wasn't paying attention....
Last edited on
Ah that is because they are variables initialized in the parent class DairyAnimal. DairyAnimal.h is included in this class.

What I need to happen is for the cows age to factor into the call to the member function milk(), which is a overload function for milk() in the DairyAnimal class.

If it will help I will put the DairyAnimal class code in here, I just figured this answer could go without it.
DairyAnimal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef DAIRYANIMAL_H
#define DAIRYANIMAL_H


class DairyAnimal
{
    public:
        float age = 1;
        int weight = 1000;
        int food = 0;
        DairyAnimal();
        DairyAnimal(float a, int w){age = a; weight = w;}
        float milk(){float gal = 0; gal = weight / food; return gal;};
        void feed(int f){food = f;}
        virtual ~DairyAnimal();
    protected:
    private:

};

#endif  


DairyAnimal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "DairyAnimal.h"

DairyAnimal::DairyAnimal()
{
    //ctor
}



DairyAnimal::~DairyAnimal()
{
    //dtor
}
Im trying to overload this derived class... I thought this was how you do it.. im looking in my book but having a hard time finding the right lesson..

Its like the program ignores that there is an if else.

im sure my mistake is in the Cow.cpp file between line 8-17 can someone tell me what im doing that is making the program disregard the if statement.
I'm sorry, I wasn't paying attention in my previous post.

The problem is on line 11 of your cow.cpp file.

The if control structure looks like this:

if(Cow().age >= 1 && Cow().age <= 10)

However, there's a problem - Cow().age is accessing the age member of a temporary Cow object, not of the actual cow you think whose age you're requesting. Since this temporary cow hasn't been explicitly initialized with an age, the age will be set to 1, as the DairyAnimal class default value indicates.

Basically, it's the same as writing:

if(1 >= 1 && 1 <= 1)

What you want to write instead is either:

if(age >= 1 && age <= 10)

or, to be more explicit (but it's not necessary):

if(this->age >= 1 && this->age <= 10)

Last edited on
Topic archived. No new replies allowed.