So I'm trying to make a circle class with three separate files. But for some reason i cannot get the answers for each function to print out. it just ends the program. If someone could tell me what I am doing wrong I would greatly appreciate it.
Heres my header file
#include <iostream>
usingnamespace std;
class circleClass
{
private:
double radius;
public:
//default constructor
//precondition: none
//postcondition: an object has been instantiated with a default radius of 1.
circleClass();
//constructor
//precondition: none
//postcondition: an object has been instantiated with a default radius of r.
circleClass(double r);
//Destructor
//precondition: an object exists with a valid radius.
//postcondition: the object is destroyed
~circleClass();
//Sets Radius
//precondition: an object exists with a valid radius.
//postcondition: the radius is changed to r.
void setradius(double r);
//Gets Radius
//precondition: an object exists with a valid radius.
//postcondition: the radius is changed to r.
double getradius();
//Calculate the area
//precondition: an object exists with a valid radius.
//postcondition: calculates and returns the area of the circle
double area();
//Calculate the circumference
//precondition: an object exists with a valid radius.
//postcondition: calculates and returns the circumference of the circle
double circum();
//returns the diameter
//precondition: an object exists with a valid radius.
//postcondition: returns the diameter of the circle
double diameter();
//prints info for a circle
//precondition: an object exists with a valid radius.
//postcondition: prints all information about a circle
void print();
};//end of class definition
#include <iostream>
#include "circleClass.h"
usingnamespace std;
cont double PI = 3.14159;
//default constructor
//precondition: none
//postcondition: an object has been instantiated with a default radius of 1.
circleClass::circleClass()
{
radius = 1.0;
}
//constructor
//precondition: none
//postcondition: an object has been instantiated with a default radius of r.
circleClass::circleClass(double r)
{
if (r<=0)
{
cout << "An invalid radius has been detected. " <<endl;
cout << "The radius has been set to 1.0"<<endl;
radius = 1.0;
}
else
radius =r;
}
//Destructor
//precondition: an object exists with a valid radius.
//postcondition: the object is destroyed
circleClass::~circleClass()
{
cout << "A circle died"<<endl;
}
//Sets Radius
//precondition: an object exists with a valid radius.
//postcondition: the radius is changed to r.
void circleClass::setradius(double r)
{
//invalid input (r <= 0) is checked for in the calling function.
radius =r;
}
//Gets Radius
//precondition: an object exists with a valid radius.
//postcondition: the radius is changed to r.
double circleClass::getradius()
{
return radius;
}
//Calculate the area
//precondition: an object exists with a valid radius.
//postcondition: calculates and returns the area of the circle
double circleClass::area()
{
return (PI * (radius*radius));
}
//Calculate the circumference
//precondition: an object exists with a valid radius.
//postcondition: calculates and returns the circumference of the circle
double circleClass::circum()
{
return (2 * PI * radius);
}
//returns the diameter
//precondition: an object exists with a valid radius.
//postcondition: returns the diameter of the circle
double circleClass::diameter()
{
return (2 * radius);
}
//prints info for a circle
//precondition: an object exists with a valid radius.
//postcondition: prints all information about a circle
void circleClass::print()
{
cout <<"The info for the circle is as follows"<<endl;
cout <<"Radius = "<<radius<<endl;
cout <<"Diameter = "<<diameter()<<endl;
cout <<"Area = " <<area()<<endl;
cout <<"Circumference = "<<circum()<<endl;
}
You're not calling any of the functions... I'm not trying to be a smart ass, you go from your entry point "int main(...)", take user input casted as a double then "system(...)" call and return 0. Where are you expecting your code to be executed?
P.S. I like the way you document this class it's very specific.
When you create a class you're creating a datatype that has parameters and memeber functions that you define. In your code, you create a 'circleClass' object, "mycircle", on Line 10 but you never use it. After you get "rad" from the user you'll want to call mycircle.setradius(r); to set the "radius" member of your object. Then you call your print function with mycircle.print(); to output to the screen.
AAAARRRGGGHHH!!! My bad, Line 16 should be mycircle.setradius(rad); but you should really read that tutorial now. I'm not trying to be a shill, it really is one of the best out there.