Shape class

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,659
  • Joined: 19-February 09

Re: Shape class

Posted 10 July 2013 - 06:52 PM

As I say, I don't see the need for a loop in the function. I would just test if the color is equal to done and return a null.

Why do you think it should keep looping?

Here you create a Shape in memory but aren't going to use it. What you are doing is called a Memory leak.

16	    Shape* shapetype = new Shape;




You could have a look at Dynamic Memory
Was This Post Helpful? 0
  • +
  • -

#17 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Shape class

Posted 10 July 2013 - 07:09 PM

Ok I got rid of the loop and just have the loop in main, however, this should loop until shapeType is NULL but it keeps looping.


#include "stdafx.h"
#include <string>
#include <sstream>
#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 [50];
	 while(shape != NULL)
	 {
		 int i = 0;

		shape[i] = getShape();
		i++;
	 }
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#18 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,659
  • Joined: 19-February 09

Re: Shape class

Posted 10 July 2013 - 07:17 PM

View Postferguson32, on 08 July 2013 - 03:29 AM, said:

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.


Your instructions say you need to create a getShape class. Is that correct?

If you are to use a class it is not clear whether your member function would return a

Shape pointer or another function.

You are to use streams so you would pass a stream to the function.

void   input(istream& in);
// or
Shape* input(istream& in);




Or an operator -

Operator Overloading in C++

This post has been edited by #define: 10 July 2013 - 07:18 PM

Was This Post Helpful? 0
  • +
  • -

#19 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Shape class

Posted 10 July 2013 - 07:24 PM

No my professor said just to create the function in its own file, and not to make it a class. These are the instructions for it:
For reading shape data, create a getShape.cpp file containing a getShape() function 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 my function:

#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;
	
	 cout << "Enter the shape's color (or 'done')..." << endl;
	 cin >> color;

	 if(color == "done")
	 {
		 shapetype = NULL;
		 return shapetype;
	 }
	 cout << "Enter shape type..." << endl;
	 cin >> type;
	 
	
	 if(type == "circle")
	 {
		 double radius;
		cout << "Enter the radius..." << endl;
	 cin >> radius;
	 shapetype = new Circle(color, radius);
	 return shapetype;
	 }
	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);
	 return shapetype;
	 }
	else if(type == "square")
	 {
		 double length;
		cout << "Enter the length..." << endl;
		cin >> length;
	 shapetype = new Square(color, length);
	 return shapetype;
	 }
	
}

Was This Post Helpful? 0
  • +
  • -

#20 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,659
  • Joined: 19-February 09

Re: Shape class

Posted 10 July 2013 - 07:24 PM

The variable shape is the array so will not be NULL.

17	     while(shape != NULL)



You could also have a temp Shape* variable. If you use a while loop you would need to use getShape in the while expression. If you use a do..while loop you can have getShape in the body of the loop.
Was This Post Helpful? 0
  • +
  • -

#21 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,659
  • Joined: 19-February 09

Re: Shape class

Posted 10 July 2013 - 08:11 PM

If you set shapetype to NULL, then when a unhandled shape (eg hexagon) is entered, NULL will be returned.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class Shape
{
 public:
   string color;
   Shape(string c) : color(c) {}
   virtual void show() {};
};

class Circle : public Shape
{
 public:
   string radius;

   Circle(string c, string r) : Shape(c), radius(r) {}
   void show() {cout << "Circle : " << color << ", r=" << radius << endl; }
};


Shape* getShape()
{
  // null pointer
  Shape* shapetype = NULL;

  cout << "Enter the shape's color (or 'done')... " ;
  string color;
  cin >> color;

  if(color == "done")
  {
    shapetype = NULL;
    return shapetype;
  }

  cout << "Enter shape type... " ;
  string type;
  cin >> type;

  if(type == "circle")
  {
    cout << "Enter radius : " ;
    string radius;
    cin >> radius;

    shapetype = new Circle(color, radius);
  }

  return shapetype;
}



int main()
{
  Shape* shape = NULL;

  shape = getShape();

  if(shape == NULL)
    cout << "shape is null" << endl;
  else
    shape->show();

  delete shape;
  
  cin.get();
  return(0);
}



Once you are happy with that I can show you how to handle different input streams.
Was This Post Helpful? 0
  • +
  • -

#22 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Shape class

Posted 10 July 2013 - 11:45 PM

When I first saw the problem, I thought that it would be an awesome idea to virtually define the input and output IO operators of the base and derived classes, that way input for any shape type would take the following form

Shape *shape = /* new shape type */;
std::cin>>*shape;



This would allow you to wrapper up and be modular with your input scheme, getting rid of nested input cases for each shape type. But once I attempted to do that myself, I found it a little more drawn out that I imagined. I did come up with a solution, which I will post just for a reference but I don't recommend trying to implement it since it steers away from what you are learning. Maybe another expert/mentor knows an easier way to do this, but this is all I got.
Spoiler

This post has been edited by jjl: 11 July 2013 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#23 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Shape class

Posted 11 July 2013 - 10:43 AM

Why doesn't this work for main? It keeps setting the return value of getShape to the array pointer "shape" and then sends shape to the toString function. Maybe my toString is not right? Here is the one for class circle, because that is what I have been testing with


#include "stdafx.h"
#include <string>
#include <sstream>
#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 = NULL;
	 while(getShape() != NULL)
	 {
		 shape = getShape();
	  shape->toString();

	 }
	 cout << shape << endl;
	return 0;
}


string Circle::toString()
{
	std::ostringstream oss;
	oss << "" << getColor() << "circle with a radius"  << ""<< area();
  std::string s = oss.str();
  return s;

}

Was This Post Helpful? 0
  • +
  • -

#24 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Shape class

Posted 11 July 2013 - 12:10 PM

Your toString returns a string object. You need to capture the return value and print it.

std::string str = shape->toString();
std::cout<<str<<std::endl;


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2