0

I'm trying to alter that values that have been passed into my swimmingPool class. I'm trying to multiply the length, depth, and width values together to get the value for my capacity. However, where I try to use my capacity member function to do this, it returns a junk value. I think my syntax is correct, so I'm why I'm not sure why I'm getting a junk value. Below is my code.

This is my implementation file.

#include <iostream>
#include "SwimmingPoolHeader.h"
using namespace std;

int main()
{
swimmingPool len;
swimmingPool wid;
swimmingPool dep;
swimmingPool cap;

int length;
int width;
int depth;

cout << "Please enter the length of the pool." << endl;
cin >> length;
len.setLength(length);

cout << "Please enter the width of the pool." << endl;
cin >> width;
wid.setWidth(width);

cout << "Please enter the depth of the pool." << endl;
cin >> depth;
dep.setDepth(depth);


cout << "The capacity of the pool is " << cap.capacity() << endl;

system("pause");
return 0;
}

This is my header file.

class swimmingPool {
public:

    void setLength(int l)
    {
        length = l;
    }
    int getLength()
    {
        return length;
    }

    void setWidth(int w)
    {
        width = w;
    }
    int getWidth()
    {
        return width;
    }

    void setDepth(int d)
    {
        depth = d;
    }
    int getDepth()
    {
        return depth;
    }

    int capacity()
    {
        return length * depth * width;
    }
private:
int length;
int width;
int depth;
};
Wyatt Bush
  • 5
  • 1
  • 2
  • 5
  • 1
    /OT Interesting profile. How did you manage to keep with 1 point rep, 1 gold badge, 3 silver and 5 bronze? Besides that your question is _off-topic_, sorry. –  Jan 28 '18 at 00:19
  • 2
    You have 4 different instances of your class. You want one. You might consider adding a constructor to initialize your member variables to 0; – Retired Ninja Jan 28 '18 at 00:20
  • 1
    It seems to me that you need [a couple of good beginners books](https://stackoverflow.com/a/388282/440558) to read. – Some programmer dude Jan 28 '18 at 00:23
  • 4
    Don't take this the wrong way, but your design seems to rely on voodoo. Passing over the fact that you have three different swimming pools with *one dimension each*, you have a fourth one that you seem to think will be imbued with those values by some kind of sympathetic magic, perhaps when the compiler uses its AGI to infer your intention from the variable's name (and its knowledge of swimming pools). Simple as this program is, it relies on some basic principles of software which you have neglected to study. You must master simpler exercises before you attempt this one. – Beta Jan 28 '18 at 00:31
  • Your program does the equivalent of measuring the length of one pool, driving to another location to measure the width of a different pool, going somewhere else to measure some other pool's depth, and finally making a visit to a fourth pool and making a guess at its total capacity. Probably you mean it to deal with just one swimming pool, which means you should have just one `swimmingPool` object. – aschepler Jan 28 '18 at 01:20

2 Answers2

1

Do you know what a constructor is? Why not add the length, width, and depth parameters when you create an swimmingPool object?

swimmingPool(int l = 0, int w = 0, int d = 0) : length(l), width(w), depth(d) {}

Then, you can create a swimmingPool like this:

swimmingPool pool(6, 7, 8);
Mike Borkland
  • 714
  • 1
  • 5
  • 12
-1

You may want to replace your main() with something like

int main()
{
    int length, width, depth;

    cout << "Please enter the length of the pool." << endl;
    cin >> length;

    cout << "Please enter the width of the pool." << endl;
    cin >> width;

    cout << "Please enter the depth of the pool." << endl;
    cin >> depth;

    swimmingPool pool;
    pool.setLength(length);
    pool.setWidth(width);
    pool.setDepth(depth);

    cout << "The capacity of the pool is " << pool.capacity() << endl;

    return 0;
}
Sid S
  • 6,037
  • 2
  • 18
  • 24