-1

In this code, once I run my program, the output for my death rate becomes corrupt. But all my variables are defined as double and there is no variable type change. Why is my output corrupt? Is it because my value is not being passed correctly?

#include <iostream>;
#include <iomanip>;

using namespace std;

//variable definition
class Population
{
    private:
        double population, births, deaths;
        double b_rate, d_rate;

    public:
        void setpopulation(double p);
        void setdeath(double death);
        void setbirth(double b);

        double getdrate();
        double getbrate();
};

void Population::setpopulation(double p)
{
    population = p;
}

void Population::setdeath(double death)
{
    deaths = death;
    d_rate = births / population;
}

void Population::setbirth(double b)
{
    births = b;
    b_rate = births/population;
}

double Population::getdrate()
{
    cout << d_rate << endl;
    return d_rate;
}

double Population::getbrate()
{;
    return b_rate;
}

int main()
{
    Population pop;
    pop.setpopulation(100000);
    pop.setdeath(7500);
    pop.setbirth(8000);
    cout << fixed << setprecision(2);
    cout << "Death rate = " << pop.getdrate() << endl;
    cout << "Birth rate = " << pop.getbrate() << endl;
    return 0;
}
  • TBH looks like a typo in `setdeath` that should be `deaths / population` not `births`, which is uninitialized and consequently undefined behaviour – Tas Oct 30 '19 at 05:19
  • You should rather use the constructor to set the values. – Destructor Oct 30 '19 at 06:12

2 Answers2

1

This is the reason. Try to be careful. This is how you are calling you are functions.

pop.setpopulation(100000);
pop.setdeath(7500);
pop.setbirth(8000);

And here is your setdeadth() -

void Population::setdeath(double death)
{
deaths = death;
d_rate = births / population;
}

Since in main you are calling setdeadth first and in setdeath. You are using births which are not known, or you can say births is uninitialized. So it is undefined. To correct it do.

pop.setpopulation(100000);
pop.setbirth(8000);
pop.setdeath(7500);

Call setbirth fist. Btw in don't think your formulas are correct. I didn't get why you are using birth in calculating death rate. It should be deaths. But I'm not pointing as it may be possible that you did that by choice. That is up to you.

Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
0

From the code you provided, in the method setdeath you are making d_rate = births/population, you must be using death there. But at that time births is not initialized, so it takes the garbage value(not zero in all cases, it can be anything as inbuilt default are unknown) which is why you are getting the death rate wrong!!!

Thanks

Rajkumar

  • 3
    _taking the default rate as 0_ no the rate is uninitialized, it can have any value and using it is undefined behaviour – Tas Oct 30 '19 at 05:29
  • Actually here there is no default constructor, in such cases there will be an implicitly declared constructor as mentioned in https://en.cppreference.com/w/cpp/language/default_constructor . And with this explanation the member varaibles will be initialized to default values. – Rajkumar Ananthu Oct 30 '19 at 05:52
  • Yes it declares a default constructor so that `Population pop` compiles, but that constructor doesn't do anything, similar to if you'd have written `Population() = default` or `Population() {}` All its member remain uninitialised, and using any of them before initialising them is undefined behaviour. Try it out: `Population pop; std::cout << pop.deaths` will very likely print a garbage value – Tas Oct 30 '19 at 05:58
  • I have added correction. And it's false that it will be initialized by zero in built members are default initialised when there is no in-class initialiser. So they are not guaranted to be zero. – Nikhil Badyal Oct 30 '19 at 06:11
  • My bad, the members will only get initialized if the object is defined as 'Population pop = Population()' , even if there is a default constructor provided, please follow the thread https://stackoverflow.com/questions/2417065/does-the-default-constructor-initialize-built-in-types – Rajkumar Ananthu Oct 30 '19 at 07:10