I am new to C++ and I have a program that is suppose to find the area of a Circle or a Square.
The program will run and request input, but the output is not what should be expected for finding the area. For example, choosing square and inputting 10 for the length of one side should output an area of 100. Instead it outputs 3.74921e+018
If you see something else that could or should be improved, I would appreciate the feedback.
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <string>
class Shape //Class name is "Shape"
{
private:
//Data member declarations
std::string shapeID; //ID or name of instance
std::string shapeName; //Circle, Square, Rectangle
std::string unitOfMeasure; //Inches or centimeters
public:
//Member function prototypes
Shape();
/* Constructor for derived class will pass a constant string
to the base class's constructor, so 2nd param is const.*/
Shape(std::string &, const std::string &, std::string &);
/* The "get" functions are declared with trailing const because
they are not allowed to modify any variables in the class.
*/
std::string getShapeID() const;
std::string getShapeName() const;
std::string getUnitOfMeasure() const;
/* Each derived class must provide its own getPerimeter().
*/
virtualdouble getArea() const = 0;
void setShapeID(std::string &);
void setShapeName(std::string &);
void setUnitOfMeasure(std::string&);
void print(); //Prints the information
};
#endif // SHAPE_H_INCLUDED
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include "Shape.h" //Base class
class Square : public Shape //Class Square inherits from Shape
{
private:
//Data member declarations
double side;
public:
//Constructor: shapeID, side, unit of measure
Square(std::string &, double, std::string &);
// Virtual function from parent must be declared in class
double getArea() const;
//Member function prototypes
double getside() const; //Gets the side
void setside(double); //Sets the side
};
#endif // SQUARE_H_INCLUDED
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include "Shape.h" //Base class
class Circle : public Shape //Class Circle inherits from Shape
{
private:
//Data member declarations
double radius;
public:
// Constructor: shapeID, radius, unit of measure
Circle(std::string &, double, std::string &);
// Virtual function from parent must be declared in class
double getArea() const;
//Member function prototypes
double getradius(); //Gets the Radius
void setradius(double); //Sets Radius (rad)
};
#endif // CIRCLE_H_INCLUDED
#include <iostream>
#include "Circle.h" //Defines Circle
#include "Square.h" //Defines Square
int main()
{ // This starts the main function
usingnamespace std;
char letter;
//This array calls for the user to select either 1 for Circle or 2 for Square
cout << "To find the area of a Circle or a Square," << "\n";
cout << "select c for Circle, or s for Square: ";
{
cin >> letter;
{
if (letter == 'c' )
{
double rad;
cout << "\nWhat is the radius in inches? ";
cin >> radius;
string name = "myCircle";
string unit = "in";
Circle C(name, 'radius', unit);
cout << "\nThe area of the circle is " << C.getArea() << " inches." << endl;
}
elseif (letter == 's')
{
double side;
cout << "\nWhat is the length of one side in inches? ";
cin >> side;
string name = "mySquare";
string unit = "in";
Square S(name, 'side', unit);
cout << "\nThe area of the square is " << S.getArea() << " inches." << endl;
}
}
}
cout << "\nGoodbye.\n"; //Just saying goodbye to the user
return 0;
} // This ends the main function
Circle C(name, 'rad', unit);
Square S(name, 'side', unit);
Look at lines 24 and 35, notice anything? Why do you have single quotes around variable names? Fixing this will fix your issues. The reason is that you are not passing the variables but some rubbish value, in your case the value 193628682 will be passed to the constructor of Square no matter what values the user inputs.
To find the area of a Circle or a Square,
select c for Circle, or s for Square: s
What is the length of one side in inches? 10
The area of the square is 100 inches.
Goodbye.