8 Replies - 2803 Views - Last Post: 06 August 2012 - 08:17 PM Rate Topic: -----

#1 Carlos311  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-August 12

C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 03:58 PM

Hello,

I am currently getting an error that I am not sure how to fix. First a little background. I am supposed to create 5 header classes named Point, Circle, Rectangle, Square, FigureGeometry and one cpp file TestAll. TestAll will use fixed values to test the header files and provide output.

Here is what I have so far.

FigureGeometry.h
#ifndef FIGUREGEOMETRY_H
#define FIGUREGEOMETRY_H

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

class FigureGeometry
{
public:
	static const int PI = 3.14f; 
	virtual float getArea() const = 0;
	virtual float getPerimeter() const = 0;

};

#endif



Point.h
#ifndef POINT_H
#define POINT_H

#include<iostream>

using namespace std;

class Point 
{
private:
	int width;
	int height;
public:
	Point() 
	{
		width = 0;
		height = 0;
	}
	Point(int theWidth, int theHeight)
	{
		width = theWidth;
		height = theHeight;
	}
	int getWidth()
	{
		return width;
	}
	int getHeight()
	{
		return height;
	}
	void setWidth(int theWidth)
	{
		width  = theWidth;
	}
	void setHeight(int theHeight)
	{
		height = theHeight;
	}
};


#endif



Square.h
#ifndef SQUARE_H
#define SQUARE_H

#include<iostream>
#include<iomanip>
#include<string>
#include "Point.h"

using namespace std;

class Square
{
private:
	Point point;
public:
	Square(){;}
	Square(Point p1)
	{
		point = p1;
	}
	~Square(){;} 
	int getSideLength()
	{
		return point.getWidth();
	}
	virtual float getArea()
	{
		return getSideLength() * getSideLength();
	}
	virtual float getPerimeter() 
	{
		return getSideLength() * 4;
	}
	void setPoint(Point p1)
	{
		point = p1;
	}

};

#endif



Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H

#include<iostream>
#include<iomanip>
#include<string>
#include "FigureGeometry.h"

using namespace std;

class Circle : public FigureGeometry
{
private:
	float radius; 
public:
	Circle(){;}
	Circle(float theRadius)
	{
		radius = theRadius;
	}
	~Circle(){;} 
	float getRadius() const
	{
		return radius;
	}
	float getArea() const
	{
		return getRadius() * getRadius() * PI; 
	}
	float getPerimeter() const
	{
		return getRadius() * 2 * PI;
	}
	void setRadius(float theRadius)
	{
		radius = theRadius;
	}
};


#endif



Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H

#include<iostream>
#include<iomanip>
#include<string>
#include "Point.h"

using namespace std;

class Rectangle 
{
private: 
	Point point;
public:
	Rectangle(){;}
	Rectangle(Point p1)
	{
		point = p1;
	}
	~Rectangle(){;} 
	int getWidth()
	{
		return point.getWidth();
	}
	int getHeight()
	{
		return point.getHeight();
	}
	float getArea()
	{
		return getWidth() * getHeight(); 
	}
	float getPerimeter()
	{
		return ( getWidth() + getHeight() ) * 2;
	}
	void setPoint(Point p1)
	{
		point = p1;
	}

};

#endif



TestAll.cpp
#include<iostream>
#include<iomanip>
#include<string>
#include"Circle.h"
#include"Square.h"
#include"Rectangle.h"

using namespace std;

void main() 
{
	Circle* c1 = new Circle(5.0f);
	cout << "Details of c1: " << endl
		 << "Radius: " << c1->getRadius() << endl		 
		 << "Area: " << c1->getArea() << endl
		 << "Perimeter: " << c1->getPerimeter() << endl
		 << endl;

	Square* s1 = new Square(5.0f);
	cout << "Details of s1: " << endl
		 << "Radius: " << s1->getSideLength() << endl
		 << "Area: " << s1->getArea() << endl
		 << "Perimeter: " << s1->getPerimeter() << endl
		 << endl;

	Rectangle* r1 = new Rectangle(5);
	cout << "Details of r1: " << endl
		 << "Width: " << r1->getWidth() << endl
		 << "Height: " << r1->getHeight() << endl
		 << "Area: " << r1->getArea() << endl
		 << "Perimeter: " << r1->getPerimeter() << endl
		 << endl;

	system("pause");



Here is what the output is supposed to look like when I compile and run.
Details of c1:
radius: 5
area: 78.5
perimeter: 31.4

Details of s1:
side length: 5
area: 25
perimeter: 20

Details of r1:
width: 5
height: 7
area: 35
perimeter: 24

The values we are supposed to use in TestAll are;
Create an instance of Circle, called c1, with a radius of 5.
Create an instance of Square, called s1, with a side length of 5.
Create an instance of Rectangle, called r1, with a width of 5 and a height of 7.

The error I get when I compile is...
\testall.cpp(19): error C2664: 'Square::Square(Point)' : cannot convert parameter 1 from 'float' to 'Point'

That is the part where am I'm not sure how to correct.

On a side note, if I comment out the square and rectangle objects on the TestAll file it does compile and run but the output is not the same as the example output I was given.

Thanks ahead of time for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ - Question on project using 5 class files and 1 cpp file.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,112
  • Joined: 05-May 12

Re: C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 04:16 PM

On line 17 of Square.h and line 17 of Rectangle.h you wrote down that the constructor takes a Point as the parameter for the constructor. If that was not your intent, change the type to something like an integer or a float. You seemed to manage to make Circle take a float as a parameter for its constructor.

This post has been edited by Skydiver: 06 August 2012 - 04:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 Carlos311  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-August 12

Re: C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 04:31 PM

I made those two files using specifications given to me by my instructor.

Variables:
Modifiers Type Name Purpose
private Point point stores the Point of Square object

Constructors:
Modifiers Parameters Implementation
public Point p1 initializes the Point of a Square object in the following manner:
point= p1;

Variables:
Modifiers Type Name Purpose
private Point point stores the Point of Rectangle object

Constructors:
Modifiers Parameters Implementation
public Point p1 initializes the Point p1 of a Rectangle object in the following manner:
point= p1;
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,112
  • Joined: 05-May 12

Re: C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 04:52 PM

I would suggest talking to your teacher because, as I recall my high school geometry, a point has no volume, area, or length. Somehow, though, in Point.h he can get width and height out of a point. Amazing! Euclid is probably rolling in his grave somewhere.

On the other hand, if he is abusing the concept of a point, to transport the parameters, then you could do:
Rectangle myRect(Point(8, 13));


To create a rectangle with width 8 and height 13.
Was This Post Helpful? 1
  • +
  • -

#5 Carlos311  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-August 12

Re: C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 05:55 PM

Thanks! That helped me figure out the problem.

Any thoughts on why my output on c1 differs from the example I was given?
Example -
Details of c1:
radius: 5
area: 78.5
perimeter: 31.4

My output -
Details of c1:
radius: 5
area: 75
perimeter: 30

When I check the math, the example is correct.

The other values for square and rectangle match the example I was provided.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,112
  • Joined: 05-May 12

Re: C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 06:17 PM

At worse, I was only expecting only the perimeter to be incorrect, but not the area. Strange that both are incorrect.

Just guessing here, but it looks like PI is just #define'd to be the integer 3, rather than a floating point 3.14159.

Just to test out that theory, change your lines where you are using PI, and use 3.14159f instead and see if that changes the values.

And also for my curiousity, can you add a cout << PI << endl; somewhere in main() to see what value you are getting.

Nevermind. I see the problem. Line 13 of FigureGeometry.h. You are assigning the floating point value to an integer and thereby losing the fractional part. Change the type to a float or double.

This tells me that you are not compiling your code with warnings turned or, or at least not turned on to maximum. I would have expected a compiler warning about that line.
Was This Post Helpful? 1
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4069
  • View blog
  • Posts: 12,555
  • Joined: 25-December 09

Re: C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 06:18 PM

This looks like a problem to me:
static const int PI = 3.14f; 

An int doesn't have any fractional part. Maybe this should be a floating point number?

Jim
Was This Post Helpful? 1
  • +
  • -

#8 Carlos311  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-August 12

Re: C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 07:20 PM

Thanks to everyone I was able to figure it out. I could not change the int to a float or double inside the class itself so I declared it outside of the class.

#ifndef FIGUREGEOMETRY_H
#define FIGUREGEOMETRY_H

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

static const float PI = 3.14f;

class FigureGeometry
{
public:
	virtual float getArea() const = 0;
	virtual float getPerimeter() const = 0;

};

#endif



This gave me the correct output I was looking for. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4069
  • View blog
  • Posts: 12,555
  • Joined: 25-December 09

Re: C++ - Question on project using 5 class files and 1 cpp file.

Posted 06 August 2012 - 08:17 PM

Quote

I could not change the int to a float or double inside the class itself

You can declare this variable inside the class, but you must initialize it outside the class.

class FigureGeometry
{
public:
	static const float PI; // = 3.14; 
	virtual float getArea() const = 0;
	virtual float getPerimeter() const = 0;

};

const float FigureGeometry::PI=3.14;



Jim

This post has been edited by jimblumberg: 06 August 2012 - 10:23 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1