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.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "cylinderType.h"
usingnamespace 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;
}
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"
//...
usingnamespace 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.
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?
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.
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"
//...
usingnamespace 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?
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?
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.
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?
> 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
constdouble 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.
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.
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.
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.
> 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 ;)
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...