Getting too big of numbers on my returns

Feb 7, 2013 at 3:08am
In my OOD class, we are learning inheritance. I am trying to get each step down before proceeding to the next one for my problem. I have started my program into returning the Area and Volume of a cylinder. The return numbers I get are huge and I cannot figure out why.

My header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class cylinderType
{
public:
	void getRadius(double rad);
	void getHeight(double heigh);
	double area();
	double volume();

private:
	double radius;
	double height;
	double rad;
	double heigh;
};


My implementation file:
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
#include "stdafx.h"
#include <iostream>
#include "cylinderType.h"
#include <cmath>
#include <iomanip>

using namespace std;


void cylinderType::getRadius(double rad)
{
	radius = rad;
}


void cylinderType::getHeight(double heigh)
{
	height = heigh;
}

double cylinderType::area()
{
	return (2 * 3.14 * pow(rad, 2) + 2 * 3.14 * rad * heigh);

}

double cylinderType::volume()
{
	return (3.14 * pow(rad, 2) * heigh);
}


My Program:
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "cylinderType.h"

using namespace std;


int main()
{
	double radius;
	double height;

	cylinderType column;

	cout << fixed << showpoint << setprecision(2);

	cout << "Welcome to Amanda and Tyler's Cylinder Painting and Shipping.\nPlease enter the radius and the height of the cylinder, in feet: " << endl;
	cin >> radius >> height;
	cout << endl;

	column.getRadius(radius);
	column.getHeight(height);

	cout << "The area of your cylinder is: " << column.area() << " in cubic feet." << endl;

	cout << "The volume of your cylinder is: " << column.volume() << " in cubic feet." << endl;
	

	return 0;
}

Feb 7, 2013 at 3:19am
menious wrote:
my OOD class
Are you foreshadowing?

4
5
	void getRadius(double rad);
	void getHeight(double heigh);
This already has me concerned.

10
11
12
13
	double radius;
	double height;
	double rad;
	double heigh;
Now I'm very concerned.

1
2
3
#include "stdafx.h"
//...
using namespace std;
This is not good, but I'm sure you've been lectured and know why.

10
11
12
13
14
15
16
17
18
19
void cylinderType::getRadius(double rad)
{
	radius = rad;
}


void cylinderType::getHeight(double heigh)
{
	height = heigh;
}
O_O

21
22
23
24
25
26
27
28
29
30
double cylinderType::area()
{
	return (2 * 3.14 * pow(rad, 2) + 2 * 3.14 * rad * heigh);

}

double cylinderType::volume()
{
	return (3.14 * pow(rad, 2) * heigh);
}
You should define a PI constant, and make it somewhat more accurate.


Your problem: you assign to radius and height, but you use rad and heigh. My solution: get rid of rad and heigh.

Also, your setters have the word "get" in them.
Last edited on Feb 7, 2013 at 3:26am
Feb 7, 2013 at 1:08pm
I have to use a class for cylinderType, per the instructions. I used rad, heigh, radius, and height from the example the teacher has given me. I never have been lectured nor know why:

1
2
3
#include "stdafx.h"
//...
using namespace std;

is not good. I was told the stdax.h has to be included on all my programs, per the instructor.

From the way I have been taught C++, my syntax is correct. I am obviously overlooking something or i wouldn't have such a huge return number. The end result of this program will include a derived class from cylinderType to determine the cost of shipping said cylinder. In my cylinderType class I will be including a formula to turn the measurements into Liters. In my main program, I will use a bool to determine if they wish it to be painted and how much more that will be.
Feb 7, 2013 at 1:17pm
A cylinder has a radius and a height; its volume and surface area can be computed once we know its radius and a height.

If that be the case, shouldn't the cylinder class have just two member variables, one for holding the radius and another for holding the height? Instead of two for radius and another two for height?
Feb 7, 2013 at 2:04pm
So in my main program, change the input from radius to column.radius and the same for height. Then drop or change all instances of such in my class. Ok, but that still doesn't explain the huge number results I am getting.
Feb 7, 2013 at 2:18pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// header cylinderType.h

// include guard

class cylinderType
{
    public:
        void set_radius( double radius ) ;
        void set_height( double height ) ;
        double area() const ;
        double volume() const ;

    private:
        double its_radius ;
        double its_height ;
};


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
// cpp
#include "cylinderType.h"

void cylinderType::set_radius( double radius )
{
    // validate that radius is positive
    its_radius = radius ;
}

void cylinderType::set_height( double height )
{
    // validate that height is positive
    its_height = height ;
}

const double PI = 3.14 ;

double cylinderType::area() const
{
	return 2 * PI * its_radius * its_radius + 2 * PI * its_radius * its_height ;

}

double cylinderType::volume() const
{
	return PI * its_radius * its_radius * its_height ;
}


Feb 7, 2013 at 2:24pm
@JLBorges


As in fact PI is a part of the realization of class cylinderType and used only within its member functions I would write

1
2
3
4
5
6
7
8
9
10
11
12
13
class cylinderType
{
    public:
        void set_radius( double radius ) ;
        void set_height( double height ) ;
        double area() const ;
        double volume() const ;

    private:
        static constexpr double PI = 3.14 ;
        double its_radius ;
        double its_height ;
};


Last edited on Feb 7, 2013 at 2:26pm
Feb 7, 2013 at 2:33pm
menious wrote:
I have to use a class for cylinderType, per the instructions. I used rad, heigh, radius, and height from the example the teacher has given me.
Was the assignment to fix the code? Because it is not correct.
menious wrote:
I never have been lectured nor know why:
1
2
3
#include "stdafx.h"
//...
using namespace std;
is not good. I was told the stdax.h has to be included on all my programs, per the instructor.
stdafx.h is used when yo have Visual C++ set to use precompiled headers. It's annoying to some people, but that wasn't what I was trying to point out. I was trying to point out your "using namespace std;" as being wrong - it's fine for small test programs, but you should never do it for actual programs - especially ones you intend to turn in.
menious wrote:
From the way I have been taught C++, my syntax is correct. I am obviously overlooking something or i wouldn't have such a huge return number.
Yes, you overlooked the bolded text in my first post.
menious wrote:
The end result of this program will include a derived class from cylinderType to determine the cost of shipping said cylinder. In my cylinderType class I will be including a formula to turn the measurements into Liters. In my main program, I will use a bool to determine if they wish it to be painted and how much more that will be.
That's great and all, but it's not even close to Object Oriented Programming. How much experience does your teacher have?
Last edited on Feb 7, 2013 at 2:34pm
Feb 7, 2013 at 2:35pm
I'll admit it; I'm an idiot. In my initial implementation file,I reversed setting rad and heigh equal to radius and height. That would make big numbers. I had it right initially; now i wonder why I changed it. The only reason I redeclared radius and height in the class is I was getting an error that they weren't defined. Why would that be?
Feb 7, 2013 at 2:37pm
You assign to "radius" and "height" but then you use "rad" and "heigh" in your calculations, so I can only guess that you forgot to change it to assign to "rad" and "heigh" instead of "radius" and "height" in your setters.
Feb 7, 2013 at 2:51pm
We are only in the second week of class; the class is called Object Oriented Design and all online. What is so bad about using 'using namespace std' in programs?
Feb 7, 2013 at 2:56pm
> As in fact PI is a part of the realization of class cylinderType and used only within its member functions I would write ...

I wouldn't. To the user, a cylinder is a geometric object with an area and a volume. That we use PI as a part of some formulae used to calculate these is an implementation detail that is of no interest to the user; and I wouldn't want to pollute the header file with things that are not actually required for the use of the object.


> used only within its member functions

const double PI = 3.14 ; in the implementation file is better encapsulated; it is programatically visible only to the member functions of cylinder. On the other hand, private means visible but not accessible.

The general rule is:
If something need not be visible outside the implementation, it should not be.
If some implementation detail must be made visible to the entire world, then access spcifiers should be used to guard it.
Feb 7, 2013 at 3:01pm

JLBorges


In this case you prevent to define member functions inside the class definition.
Feb 7, 2013 at 3:08pm
Thank you vlad, JLBorges and L B for your help. I'll fix the program tonight when I get home and add more to get it finished. So don't be surprised if you see more problems I am having. Then I get to start pointers.
Feb 7, 2013 at 3:12pm
menious wrote:
We are only in the second week of class; the class is called Object Oriented Design and all online. What is so bad about using 'using namespace std' in programs?
That's forgivable. I'm assuming you're not doing actual OOP yet and just learning how C++ classes work? That's ok.

As for "using namespace std;"
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c
It's a bad habit. And, it doesn't just apply to other things - it applies right now.

The <algorithm> header defines a function std::count. If you write "using namespace std;", it is now just "count". What if you wanted to make or use a counter variable and you named it count?

The point of namespaces is to prevent problems with multiple things having the same name. When you write "using namespace xxx;" you circumvent this precaution and open yourself to the possibility of problems with clashing names.

Like I said, it is fine for small test programs, but you should try to get out of the habit of doing it - it will cause problems if you pursue C++ as a career.
Feb 7, 2013 at 3:41pm
> The <algorithm> header defines a function std::count.
> If you write "using namespace std;", it is now just "count".
> What if you wanted to make or use a counter variable and you named it count?
Don't use global variables ;)
Feb 7, 2013 at 3:45pm
The point is that when your count shadows the other count, you have primed a bomb. When you remove or rename your count and don't rename it in all places, your compiler will give you error messages you didn't expect...
Feb 7, 2013 at 4:28pm
> When you remove or rename your count and don't rename it in all places,
> your compiler will give you error messages
Great, it does not compile

> you didn't expect...
It would be different of `identifier was not declared', but I do expect an error
Topic archived. No new replies allowed.