Cylinder volume = PI*radius^2*Height
Cone volume = 1/3(PI*radius^2*Height)
I was having some trouble getting the calculations to work, and so in the name of troubleshooting I implemented the calculation with the same formula to verify that I am getting the same results. Given the same data, I am returning different answers. So here is some code:
cone.h
#pragma once
class cone
{
public:
double volume;
int x, y, z;
public:
cone(double base_radius, double height, int x, int y, int z);
~cone(void);
private:
// Since this will be handled by the constructor, no reason to let people mess with it.
double calculateVolume(double radius, double height);
};
cone.cpp
#include "cone.h"
#define _USE_MATH_DEFINES
#include<cmath>
cone::cone(double base_radius, double height, int x, int y, int z)
{
this->volume = this->calculateVolume(base_radius, height);
this->x = x;
this->y = y;
this->z = z;
}
cone::~cone(void)
{
}
double cone::calculateVolume(double radius, double height)
{
return (M_PI * (radius * radius) * height);
}
cylinder.h
#pragma once
class cylinder
{
public:
double volume;
int x, y, z;
public:
cylinder(double radius, double height, int x, int y, int z);
~cylinder(void);
private:
// Since this will be handled by the constructor, no reason to let people mess with it.
double calculateVolume(double radius, double height);
};
cylinder.cpp
#include "cylinder.h"
#define _USE_MATH_DEFINES
#include<cmath>
cylinder::cylinder(double radius, double height, int x, int y, int z)
{
this->calculateVolume(radius, height);
this->x = x;
this->y = y;
this->z = z;
}
cylinder::~cylinder(void)
{
}
double cylinder::calculateVolume(double radius, double height)
{
return (M_PI * (radius * radius) * height);
}
main.cpp
// File : main.cpp
// Author: Brendon Dugan [------@gmail.com] (c) 2010
//
// Parses a text file and creates certain shapes based upon the data within the file.
#include<iostream>
#include "box.h"
#include "cone.h"
#include "cylinder.h"
using std::cout;
using std::endl;
// Purpose: The main function, allows the user to specify a file to be parsed, parses the file,
// and then prints relevant information to the screen.
// Returns: Nothing
void main(void)
{
box box1 = box(3.0, 4.5, 2.2, 1,8,10);
cone cone1 = cone(7, 4, 3, 5, 9);
cylinder cyl1 = cylinder(7, 4, 7, 300, 5);
cout << "Box 1 volume = " << box1.volume << endl;
cout << "Cone 1 volume = " << cone1.volume << endl;
cout << "Cylinder 1 volume = " << cyl1.volume << endl;
}
Output:
Box 1 volume = 29.7 Cone 1 volume = 615.752 Cylinder 1 volume = -9.25596e+061 Press any key to continue . . .
I'm not sure what's happening, and since I'm new to C++ I'm not sure where to go next. Any ideas?

New Topic/Question
Reply




MultiQuote







|