Shape class

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1169 Views - Last Post: 11 July 2013 - 12:10 PM Rate Topic: -----

#1 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Shape class

Posted 07 July 2013 - 07:29 PM

Hi I am working on a project where I create a base Shape class and then create derived classes (eg. circle, square). I am need to create a getShape class that returns a base Shape * pointer. It should read a shape description from an input stream, create the correct type of derived shape with the new operator and parameters to the constructor, and return a base Shape * pointer to the new object. After reading a shape color and type (e.g. blue circle), it reads the additional information specific to that type of shape (e.g. for a circle, it reads the radius), and then uses the new operator to create the specific derived type of shape (e.g. new Circle(color, radius) )

Input can be from cin, or from a stream input file (use iostreams). When there is no more input data (or when 'done' is entered), return a NULL Shape pointer. All character data is kept in C++ strings (no char[] arrays).

This is the only file that #includes the Circle.h, Square.h, and Rectangle.h header files (since it needs them to create the various types of shapes with the new operator).

This is what I have but I'm not sure if I'm on the right track or not with this. Any help would be great thank you.

#include "stdafx.h"
#include "getShape.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
#include "Circle.h"
getShape::getShape(void)
{
}

Shape* getShape(string color, string type, string radius)
{	 do{
		 
	 cout << "Enter the shape's color (or 'done')..." << endl;
	 getline(cin, color);

	 cout << "Enter shape type..." << endl;
	 getline(cin, type);

	 cout << "Enter the radius..." << endl;
	 getline(cin, radius);

	 }while(color != "done");
}

getShape::~getShape(void)
{
}


Is This A Good Question/Topic? 0
  • +

Replies To: Shape class

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: Shape class

Posted 07 July 2013 - 08:18 PM

No, your are not on the right track:
- Your file was supposed to include "Circle.h", "Square.h" and "Rectangle.h". You've only included "Circle.h".
- Your function on line 12 does not look to be part of your getShape class.
- It's usually very bad form to name a method with the same name as the class name. Your function on line 12, has the same name as the class.
- Your method was supposed to get input from any input stream, but have hardcoded it to get input from the cin console input stream.
- You are supposed to find out shape color and type first, construct the object, and then get other details. You are already asking for the radius without knowing for sure that would be appropriate for the shape.
Was This Post Helpful? 1
  • +
  • -

#3 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Shape class

Posted 08 July 2013 - 10:47 AM

Sorry I should've mentioned that I've only created the circle class thus far and will create the others when I can get this working when the user enters a circle as the object. I am having trouble understanding what to do after I create a general shape object, like what do I do with that object? I changed the code some

#include "stdafx.h"
#include "getShape.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
#include "Circle.h"
getShape::getShape(void)
{
}

Shape* newShape(string color, string type)
{	 do{
		 
	 cout << "Enter the shape's color (or 'done')..." << endl;
	 getline(cin, color);

	 
	 cout << "Enter shape type..." << endl;
	 getline(cin, type);
	 
	 Shape* shapeType = new Shape(color);
	if(type == "circle")
	 {
		 double radius;
		cout << "Enter the radius..." << endl;
	 getline(cin, radius);
	 Circle* circle = new Circle(color, radius);

	 }

	 }while(color != "done");
}

getShape::~getShape(void)
{
}

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Shape class

Posted 08 July 2013 - 01:58 PM

Here you don't know which Shape to use yet, so you just want a pointer :

22     Shape* shapeType = new Shape(color);
       // should be
22     Shape* shapeType;




A base class pointer can point to a derived class. So your variable can point to a Circle here :

28     shapeType = new Circle(color, radius);


Was This Post Helpful? 1
  • +
  • -

#5 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Shape class

Posted 08 July 2013 - 07:26 PM

Sorry I'm having a lot of trouble understanding this. Should it be something like this:

Shape* newShape()
{	 
	string color;
	string type;
	do{
		 
	 cout << "Enter the shape's color (or 'done')..." << endl;
	 getline(cin, color);

	 
	 cout << "Enter shape type..." << endl;
	 getline(cin, type);
	 
	
	if(type == "circle")
	 {
		 double radius;
		cout << "Enter the radius..." << endl;
	 getline(cin, radius);
	 Shape* circle = new Circle(color, radius);

	 }

	 }while(color != NULL);
}


If the type entered is "circle" then it asks for radius and creates a new Circle object, right? Also if it is correct, the getline in the if statement is giving me an error saying nothing matches the argument list.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Shape class

Posted 08 July 2013 - 07:55 PM

Hi, getline only read strings

19	     getline(cin, radius);



So you need cin >>

19	     cin >> radius;




cin >> leaves newline characters in the stream so you could change the other getlines to cin >> as well.


Here color is a string and won't equal NULL

24	     }while(color != NULL);




Why are you using the do..while loop ?
Was This Post Helpful? 0
  • +
  • -

#7 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Shape class

Posted 09 July 2013 - 03:49 AM

Ok I changed it to this:

getShape.cpp:

#include <string>
#include <sstream>
#include "stdafx.h"
#include "Shape.h"
#include "Circle.h"
#include <iostream>
using namespace std;

Shape* newShape()
{	 
	string color;
	string type;
	Shape* shapetype = NULL;
	while(color != "done")
	{	 
	 cout << "Enter the shape's color (or 'done')..." << endl;
	 getline(cin, color);

	 
	 cout << "Enter shape type..." << endl;
	 getline(cin, type);
	 
	
	if(type == "circle")
	 {
		 double radius;
		cout << "Enter the radius..." << endl;
	 cin >> radius;
	 shapetype = new Circle(color, radius);
	 return shapetype;

	 }

	
	}
	return shapetype;
}


1>------ Build started: Project: Assignment5, Configuration: Debug Win32 ------
1> Circle.cpp
1>c:\users\jrook32\documents\visual studio 2012\projects\assignment5\assignment5\circle.h(2): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>getShape.obj : error LNK2005: "class Shape * __cdecl newShape(void)" (?newShape@@YAPAVShape@@XZ) already defined in Assignment5.obj
1>C:\Users\jrook32\Documents\Visual Studio 2012\Projects\Assignment5\Debug\Assignment5.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


These are my build errors, is there something wrong with circle.cpp:

#include "stdafx.h"
#include "Circle.h"
#include <iostream>
#include <sstream>

Circle::Circle(const string& color, double radius)
	:Shape(color)
{
	m_radius = radius;
}

double Circle::area()
{
	double area;
	area = 3.14 * m_radius * m_radius;
	return area;
}

string Circle::toString()
{
	std::ostringstream oss;
  oss << "" << area();
  std::string s = oss.str();
  return s;

}

Circle::~Circle(void)
{
}

Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Shape class

Posted 09 July 2013 - 06:00 PM

We might need more code to discover why the errors occur.

I think stdafx.h is supposed to be first.

03	#include <string>
04	#include <sstream>
05	#include "stdafx.h"




You are supposed to be able to turn off using stdafx.h if you want.
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Shape class

Posted 09 July 2013 - 08:11 PM

Hi,

You have probably forgotten header guards

You include Circle.h in Circle.cpp but also include it in GetShape.cpp.


Typical Header Guard

#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED

//code goes here

#endif



The same goes for all mutiply defined headers such as Rectangle.h etc..
Regards

Snoopy
Was This Post Helpful? 0
  • +
  • -

#10 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Shape class

Posted 10 July 2013 - 04:14 PM

I am having issues with my while loop. It is supposed to keep looping until the user enters done as the color but it just keeps going.

#include "stdafx.h"
#include <string>
#include <sstream>
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>
using namespace std;

Shape* getShape()
{	 
	string color;
	string type;
	Shape* shapetype = new Shape;
	while(true)
	{	 
	 cout << "Enter the shape's color (or 'done')..." << endl;
	 getline(cin, color);

	 if(color == "done")
	 {
		 return false;
	 }
	 
	 cout << "Enter shape type..." << endl;
	 getline(cin, type);
	 
	
	if(type == "circle")
	 {
		 double radius;
		cout << "Enter the radius..." << endl;
	 cin >> radius;
	 shapetype = new Circle(color, radius);

	 }
	else if(type == "rectangle")
	 {
		 double length, width;
		cout << "Enter the length..." << endl;
	 cin >> length;
	 cout << "Enter the width..." << endl;
	 cin >> width;
	 shapetype = new Rectangle(color, length, width);

	 }
	else if(type == "square")
	 {
		 double length;
		cout << "Enter the length..." << endl;
		cin >> length;
	 shapetype = new Square(color, length);

	 }
	else
	{
		shapetype = NULL;
	}
	
	}

	return shapetype;
}


Well actually after it takes input for the first shape. It skips color and asks for the type, not sure why.

This post has been edited by ferguson32: 10 July 2013 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#11 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Shape class

Posted 10 July 2013 - 04:21 PM

Hi,

you need to break;

from the loop...

don't you ?

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#12 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Shape class

Posted 10 July 2013 - 04:25 PM

Well actually after it takes input for the first shape. It skips color and asks for the type, not sure why.
Was This Post Helpful? 0
  • +
  • -

#13 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Shape class

Posted 10 July 2013 - 05:02 PM

Nevermind it was because I was using getline() and cin >>.

Can you help with my main method. It fills an array of pointers to various types of shapes, prints them, sorts them, and prints them again and this is created from the getShape method in another file. I know it needs to loop and add it to an array but I'm not sure how to do this.

main.cpp

#include "Shape.h"
#include <iostream>

using namespace std;

Shape* getShape();

int _tmain(int argc, _TCHAR* argv[])
{


	 cout << "Enter a list of shapes - 'done' to end" << endl;
	 Shape* shape = new Shape;
	 while(shape != NULL)
	 {
		 int i = 0;

		getShape();
	 }
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Shape class

Posted 10 July 2013 - 05:58 PM

The problem with your getShape function is that it can create more than one shape in memory but only return a pointer to one. Would it would be better if it only made one shape?
Was This Post Helpful? 0
  • +
  • -

#15 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Shape class

Posted 10 July 2013 - 06:13 PM

Actually my getShape function isn't working properly as well. So I should focus in that first. It should keep looping until the user enters "done" as the color but it keeps looping. Any idea?

#include "stdafx.h"
#include <string>
#include <sstream>
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>
using namespace std;
std::istringstream;

Shape* getShape()
{	 
	string color;
	string type;
	Shape* shapetype = new Shape;
	while(color != "done")
	{	 
	 cout << "Enter the shape's color (or 'done')..." << endl;
	 cin >> color;

	 
	 cout << "Enter shape type..." << endl;
	 cin >> type;
	 
	
	if(type == "circle")
	 {
		 double radius;
		cout << "Enter the radius..." << endl;
	 cin >> radius;
	 shapetype = new Circle(color, radius);

	 }
	else if(type == "rectangle")
	 {
		 double length, width;
		cout << "Enter the length..." << endl;
	 cin >> length;
	 cout << "Enter the width..." << endl;
	 cin >> width;
	 shapetype = new Rectangle(color, length, width);

	 }
	else if(type == "square")
	 {
		 double length;
		cout << "Enter the length..." << endl;
		cin >> length;
	 shapetype = new Square(color, length);

	 }
	else
	{
		shapetype = NULL;
	}
	
	
	}

	return shapetype;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2