C++ problem with Shape hierarchy ~

This is a discussion on C++ problem with Shape hierarchy ~ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight When we say "compile", we mean "compile without errors". Your code in post #14 should not ...

  1. #31
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    When we say "compile", we mean "compile without errors". Your code in post #14 should not compile, i.e., the compiler should be reporting at least one error.
    what do you think is wrong with the program and how can i fix it?

  2. #32
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    What's wrong is that the main() you provided won't compile, so there's no way to diagnose your problem until you post the real one. I know you think it's compiling, but it's not valid C++.

    Maybe it's not added to your VC++ project and you think it is? Export a makefile and post that, maybe.

  3. #33
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    What's wrong is that the main() you provided won't compile, so there's no way to diagnose your problem until you post the real one. I know you think it's compiling, but it's not valid C++.

    Maybe it's not added to your VC++ project and you think it is? Export a makefile and post that, maybe.
    what i want to know is how do i run a program with function like Shape* getShape()??? That's what i really want to know.

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by jackfraust
    what i want to know is how do i run a program with function like Shape* getShape()??? That's what i really want to know.
    You would do something like this:
    Code:
    #include <iostream>
    
    // other includes etc
    // ...
    
    int main()
    {
        Shape* shape = getShape();
        std::cout << shape->toString() << std::endl;
        delete shape;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #35
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    You would do something like this:
    Code:
    #include <iostream>
    
    // other includes etc
    // ...
    
    int main()
    {
        Shape* shape = getShape();
        std::cout << shape->toString() << std::endl;
        delete shape;
    }
    It doesnt work. It says that getShape(); is unidentified.

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by jackfraust
    It doesnt work. It says that getShape(); is unidentified.
    That means that you did not modify my example correctly.

    Okay, I think we need to do some serious hand holding here. Ditch your code and try this:
    Code:
    #include <iostream>
    
    class Shape
    {
    public:
        virtual ~Shape() {}
        virtual unsigned int area() const = 0;
    };
    
    class Square : public Shape
    {
    public:
        explicit Square(unsigned int length) : length(length) {}
    
        virtual unsigned int area() const
        {
            return length * length;
        }
    private:
        unsigned int length;
    };
    
    class Rectangle : public Shape
    {
    public:
        Rectangle(unsigned int length, unsigned int width)
            : length(length), width(width) {}
    
        virtual unsigned int area() const
        {
            return length * width;
        }
    private:
        unsigned int length;
        unsigned int width;
    };
    
    Shape* getShape()
    {
        /* Okay, let's play a game: if the user enters an odd number, we create
         * a square of side 5. Otherwise, we create a 11 by 7 rectangle.
         */
        std::cout << "Please enter an integer: ";
        int num;
        std::cin >> num;
        if (num % 2 == 0)
        {
            return new Rectangle(11, 7);
        }
        else
        {
            return new Square(5);
        }
    }
    
    int main()
    {
        Shape* shape = getShape();
        std::cout << shape->area() << std::endl;
        delete shape;
    }
    Last edited by laserlight; 04-09-2009 at 11:33 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #37
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    That means that you did not modify my example correctly.

    Okay, I think we need to do some serious hand holding here. Ditch your code and try this:
    Code:
    #include <iostream>
    
    class Shape
    {
    public:
        virtual ~Shape() {}
        virtual unsigned int area() const = 0;
    };
    
    class Square : public Shape
    {
    public:
        explicit Square(unsigned int length) : length(length) {}
    
        virtual unsigned int area() const
        {
            return length * length;
        }
    private:
        unsigned int length;
    };
    
    class Rectangle : public Shape
    {
    public:
        Rectangle(unsigned int length, unsigned int width)
            : length(length), width(width) {}
    
        virtual unsigned int area() const
        {
            return length * width;
        }
    private:
        unsigned int length;
        unsigned int width;
    };
    
    Shape* getShape()
    {
        /* Okay, let's play a game: if the user enters an odd number, we create
         * a square of side 5. Otherwise, we create a 11 by 7 rectangle.
         */
        std::cout << "Please enter an integer: ";
        int num;
        std::cin >> num;
        if (num % 2 == 0)
        {
            return new Rectangle(11, 7);
        }
        else
        {
            return new Square(5);
        }
    }
    
    int main()
    {
        Shape* shape = getShape();
        std::cout << shape->area() << std::endl;
        delete shape;
    }
    That's the thing, the getShapte() function is suppose to get the dimensions from the input stream, then store them in an array. The getShape function is supposed to be in getShape.cpp. Then the main function is suppose to be a different source file with only the Shape.h as #include header while the getShape is supposed to have the circle.h, rectangle.h and square.h as headers.

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by jackfraust
    That's the thing, the getShapte() function is suppose to get the dimensions from the input stream, then store them in an array.
    In that case, getShape() should return a std::vector<Shape*> instead, or if you are not allowed to use a standard container, then it should return a Shape**, i.e., you would create a dynamic array of Shape pointers. Each Shape pointer would then point to a derived class object.

    Quote Originally Posted by jackfraust
    The getShape function is supposed to be in getShape.cpp. Then the main function is suppose to be a different source file with only the Shape.h as #include header while the getShape is supposed to have the circle.h, rectangle.h and square.h as headers.
    That is not a problem, but I would expect a declaration of getShape() to be in a header. If you are not allowed to have a header for that, then just forward declare the function in the source file where the global main function resides.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    It still doesn't work
    here is what i wrote. I wrote again.
    Code:
    #include "Circle.h"
    #include "Square.h"
    #include "Rectangle.h"
    
    
    
    using std::string;
    using namespace std;
    
    Shape* getShape()
    {
    
    do {
    
    string color;
    double length, width, rad, side;
    string shapetype;
    Shape *myshape;
    
    
    
    cout << "Enter the shape's color (or 'done')..." << endl;
    cin >> color;
    cout << "Enter shape type..." << endl;
    cin >> shapetype;
    if (shapetype == "done")
    {
    return NULL;
    }
    if (shapetype == "circle")
    {
    cout << "Enter the radius..." << endl;
    cin >> rad;
    myshape = new circle (color, rad);
    return myshape;
    }
    
    
    if (shapetype == "square")
    {
    cout << "Enter the side length..." << endl;
    cin >> side;
    myshape = new square (color, side);
    return myshape;
    }
    
    if (shapetype == "rectangle")
    {
    cout << "Enter the length..." << endl;
    cin >> length;
    cout << "Enter the width..." << endl;
    cin >> width;
    myshape = new rectangle (color, width, length);
    return myshape;
    }
    else{
    cout << "Invalid input. Please enter \"circle\", \"square\", \"rectangle\" or \"done\"" << endl;
    continue;
    }
    
    
    }while (true);
    
    }

    It still doesn't show anything when i run main.

  10. #40
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Where's main?

  11. #41
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    Where's main?
    this is the instruction for main:
    The main.cpp file contains the main() function. It should define an array of base Shape pointers, and loops calling getShape() and storing the returned Shape pointer into the next available element of the array. The loop completes when a NULL Shape pointer is returned from getShape.

    main() next sorts the array of Shape pointers into ascending order by area. Note that the areas of the shapes need to be compared, but the array to be sorted contains Shape pointers. When the sort is complete, main then loops and prints the sorted list of shapes.

    Finally, main loops and calls delete on the Shape pointers to delete the shape objects.

    main.cpp() #includes only the base Shape.h header file

    Code:
    	Shape *dshape[6];
    	int i;
    	for ( i = 0; i < 6; ++i){
    		dshape[i]->getShape();
    		dshape[i]->toString();
    		}
    Last edited by jackfraust; 04-11-2009 at 10:14 PM.

  12. #42
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    why is it that the compiler can't recognize the getShape() function.

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by jackfraust
    why is it that the compiler can't recognize the getShape() function.
    Where does the compiler not recognise it?

    Let's check: are you able to compile the source files for Circle, Square and Rectangle? Are you are to compile the source file that contains the definition (i.e., implementation) of getShape()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #44
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Where does the compiler not recognise it?

    Let's check: are you able to compile the source files for Circle, Square and Rectangle? Are you are to compile the source file that contains the definition (i.e., implementation) of getShape()?
    the source file shouldn't contain the implementation of getShape(). getshape is supposed to be the source file getShape.cpp and include the headers of circle.h, rectangle.h and square.h. Main.cpp is supposed to be a different source file and include only shape.h header.

    I've already test the source codes i wrote for circle, rectangle and square. .The problem is with getShape() because i can't call it in main.

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by jackfraust
    the source file shouldn't contain the implementation of getShape(). getshape is supposed to be the source file getShape.cpp and include the headers of circle.h, rectangle.h and square.h.
    You are contradicting yourself. In the first sentence, you say that the source file should not contain the implementation of getShape(), then in the second second you say that the source file should contain the implementation of getShape().

    Quote Originally Posted by jackfraust
    I've already test the source codes i wrote for circle, rectangle and square.
    Good. What about getShape.cpp? If it compiles, show your current Main.cpp. If not, show your current getShape.cpp.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 3 of 4 FirstFirst 1234 LastLast
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, 10: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, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21