Thread: C++ problem with Shape hierarchy ~

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    72

    C++ problem with Shape hierarchy ~

    Code:
    #ifndef SHAPE_H
    #define SHAPE_H
    
    #include <string.h>
    using std::string;
    
    using namespace std;
    
    
    
    class Shape
    
    {
    public:
    	Shape(const string &color); //constructor
    	~Shape();
    	string getColor()const; //returns objects color value
    	virtual double area()const = 0;// a const pure virtual member function that computes and returns the object's area
    	virtual string toString() const = 0; //a const pure virtual member function that returns the shape's description
    private:
    	string s_color;
    
    };
    
    
    #endif //SHAPE_H
    
    #include <string>
    using std::string;
    
    #include <sstream>
    
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include "square.h"
    
    //ctor
    square::square(const string &color, double length)
    :Shape(color)
    {
    	length = len;
    }//end ctor
    
    //dtor
    double square::getLength()
    {
    	return len;
    }
    //Function to calculate are
    double square::area()const
    {
    	return len * len;
    }//end function to calculate area
    
    //Function to returns square's description
    string square::toString()const
    {
      ostringstream os;
      os << getColor() <<"square with side length of " << len << " " << "and area of " << area();
      return os.str();
    
    }//end of function to return's square description
    
    #include <string>
    using std::string;
    
    #include <sstream>
    
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include "rectangle.h"
    
    //ctor
    rectangle::rectangle(const string &color, double width, double length)
    :Shape(color)
    {
    length = len;
    wid = width;
    }//end ctor
    
    //dtor
    double rectangle::getWidth()
    {
    	return wid;
    }
    
    double rectangle::getLength()
    {
    	return len;
    }
    //function to calculate rectangle area
    double rectangle::area() const
    {
    	return len * wid;
    }//end function to get rectangle area
    
    //returns rectangle's description
    string rectangle::toString()const
    {
      ostringstream os;
      os << getColor() <<"rectangle with length of " << len << " and width of " << wid << " and area of " << area();
      return os.str();
    }//end function to return rectangle's description
    
    
    #include <string>
    using std::string;
    
    #include <sstream>
    
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include "circle.h"
    
    //ctor
    circle::circle(const string& color, double radius)
    :Shape(color)
    {
    	radius = rad;
    }//end ctor
    
    double circle::getRadius()
    {
    	return rad;
    }
    //function to calculate circle area
    double circle::area()const
    {
    	return rad * rad * 3.14;
    }//end function to get circle area
    
    //returns circle description
    string circle::toString()const
    {
      ostringstream os;
      os << getColor() <<" circle with radius of " << rad << " and area of " << area();
      return os.str();
    }//end function to return circle description
    
    
    #include <string>
    using std::string;
    
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include "circle.h"
    #include "rectangle.h"
    #include "square.h"
    
    
    Shape* getShape()
    {	
    
    
    	int i;
    	string shapetype;
    	char choice;
    
    	Shape* myShape[6];
    	
    	
    	
    
    	cout << "Creating a Shape  ";
    	cout << "============================================== "<<endl;
    	cout << " 1: Create a circle "<<endl;
    	cout << " 2: Create a rectangle "<<endl;
    	cout << " 3: Create a square "<<endl;
    	cout << " 4: Done "<<endl;
    	cout << "============================================== "<<endl;
    	for ( i = 0; i < 6; ++i){
    	cout << "Enter number for shapetype" <<endl;
    
    	switch(choice){
    	
    	case '1':
    	cout << "\nEnter the shape's color (or 'done')...";
    	cin >>  s_color   ;
    	cout << "\nEnter shape type..." ;
    	cin >> shapetype;
    	cout << "\nEnter radius.... ";
    	cin >> radius;
    	myShape[i] = new circle(color, radius);
    	break;
    	
    	case '2':
    	cout << "\nEnter the shape's color (or 'done')...";
    	cin >> color;
    	cout << "\nEnter shape type...";
    	cin >> shapetype;
    	cout << "\nEnter the length and width....  ";
    	cin >> width >> length;
    	myShape[i] = new rectangle(color, length, width); 
    	break;
    	
    	case '3':
    	cout << "\nEnter the shape's color (or 'done')...";
    	cin >> color;
    	cout <<"\nEnter shape type...";
    	cin >> shapetype;
    	cout << "\nEnter the length of a side...";
    	cin >> length;
    	myShape[i] = new square(color, length);
    	break;
    	
    	case '4':
    	cout <<"\nEnter the shape's color (or 'done')....";
    	cout << "done"<< endl;}
    
    	}
    	return myShape[i];
    
    }


    For some reason when i try to call the getShape function in main, it doesn't work. the toString function doesnt work neither.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Define "doesn't work". Doesn't compile? Doesn't do what you want?

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by tabstop View Post
    Define "doesn't work". Doesn't compile? Doesn't do what you want?
    It does compile with no errors but when i run the program, nothing comes up. It is suppose to get the shape, the color, and the dimension(size) of the shape. then print the information out.

    Once that is done, it is suppose to sort the area.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since getShape creates shapes and then throws them away and returns a pointer-to-nothing-in-particular-but-especially-not-the-created-shapes, that is perhaps not very surprising.

    Remember, arrays can not be returned. I would suggest putting the big ol' Loop Of Asking in your main program and have getshape just make one shape and return it. (You could ask the question in main and pass the user's answer into the function, say.) (Edit: Alternatively, you could pass the array into the function to be filled.)

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by tabstop View Post
    Since getShape creates shapes and then throws them away and returns a pointer-to-nothing-in-particular-but-especially-not-the-created-shapes, that is perhaps not very surprising.

    Remember, arrays can not be returned. I would suggest putting the big ol' Loop Of Asking in your main program and have getshape just make one shape and return it. (You could ask the question in main and pass the user's answer into the function, say.) (Edit: Alternatively, you could pass the array into the function to be filled.)
    you mean something like this:

    Code:
    cout << creating a shape <<endl; 
    ....
    
    ....
    
    for (i = 0; i < 6; ++i){
    cout << "Enter number to create Shape" << endl;
    
    getShape();}
    ??
    But the thing is, the guy said the loop should be in the getShape function. and the main function is supposed to call the getShape function and toString function to print out info.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can leave the loop in the getShape function, but since the array cannot be returned from there, it must be passed in.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    getLength and getWidth should be const functions, and don't write an empty, non-virtual destructor. If you don't need to implement or override it, then it's just extra clutter.

    You could learn about vectors and return a vector<shape*> from getShape. Then you're code could work.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by iMalc View Post
    getLength and getWidth should be const functions, and don't write an empty, non-virtual destructor. If you don't need to implement or override it, then it's just extra clutter.

    You could learn about vectors and return a vector<shape*> from getShape. Then you're code could work.
    I can't use vector. thing is everytime i call the function in main, it comes up blank even though there is no error in the source(or at least i think).

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Are you saying that you're not seeing the input prompts from getShape at all? Post your main() function and/or whatever calls getShape.

    Also, tabstop was right both times that you can't return a local array from getShape. Either pass in the array, or allocate it with new inside of getShape and return it.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    Are you saying that you're not seeing the input prompts from getShape at all? Post your main() function and/or whatever calls getShape.

    Also, tabstop was right both times that you can't return a local array from getShape. Either pass in the array, or allocate it with new inside of getShape and return it.
    It's' suppose to return a base Shape* to the new object. I called like i declared it in getShape cpp, shape* getShape*(); it compiles but nothings comes on the screen

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    My mistake - you're already returning something allocated with new (a Shape *). Can you show us the exact code snippet where getShape() is called?

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    My mistake - you're already returning something allocated with new (a Shape *). Can you show us the exact code snippet where getShape() is called?
    Shape* getShape(); thats how i called it

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    That's a function declaration, not a function call. Post your main() function that alledgedly calls getShape().

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    That's a function declaration, not a function call. Post your main() function that alledgedly calls getShape().
    i called it like this:
    Code:
    int main();
    {
    Shape getShape();
    Shape::toString();
    }

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by jackfraust View Post
    i called it like this:
    Code:
    int main();
    {
    Shape getShape();
    Shape::toString();
    }
    Is this function static?
    Don't you need an object of type Shape?

    Code:
    Shape objShape;
    objShape.toString();
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM