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 You are contradicting yourself. In the first sentence, you say that the source file should not ...

  1. #46
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    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().


    Good. What about getShape.cpp? If it compiles, show your current Main.cpp. If not, show your current getShape.cpp.
    I'm not talking about the getShape.cpp, i'm talking about shape and other derived classes, they shouldn't include getShape.cpp. ON the other hand, getShape.cpp is supposed to include circle.h, rectangle.h and square.h. and Main.cpp is only supposed to include shape.h.

    Code:
    #include <string>
    using std::string;
    #include <sstream>
    #include <iostream>
    using namespace std;
    #include <iomanip>
    
    #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" << endl;
    continue;
    }
    
    
    }while (true);
    
    }

  2. #47
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by jackfraust
    I'm not talking about the getShape.cpp, i'm talking about shape and other derived classes, they shouldn't include getShape.cpp. ON the other hand, getShape.cpp is supposed to include circle.h, rectangle.h and square.h. and Main.cpp is only supposed to include shape.h.
    Yes, but I was talking about getShape.cpp, so there was a miscommunication

    Anyway, I gather that you posted getShape.cpp. What are the errors that you get when you compile it?
    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

  3. #48
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Yes, but I was talking about getShape.cpp, so there was a miscommunication

    Anyway, I gather that you posted getShape.cpp. What are the errors that you get when you compile it?
    nothing, its just that main.cpp doesn't recognize getShape(). It doesn't recognize, circle, square or rectangle either.

    Code:
    int main()
    
    {
          shape* dshape[10];
          for ( int i = 0; i < 10; ++i)
          dshape[i]->getShape();
          dshape[i]->toString();
    }
    I tried to call getShape() other ways but it doesnt recognize it either.

  4. #49
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by jackfraust
    nothing, its just that main.cpp doesn't recognize getShape(). It doesn't recognize, circle, square or rectangle either.
    Right, then you should have posted your main.cpp instead.

    Quote Originally Posted by jackfraust
    It doesn't recognize, circle, square or rectangle either.
    One idea here is that your global main function does not need to know about circle, square, or rectangle. The code that handles the creation of the correct object is in getShape(). All that the global main function needs to know is the shape interface, and that is provided by including shape.h.

    However, to call getShape(), the global main function needs to know something about it, and that can be provided by declaring it:
    Code:
    #include "shape.h"
    
    shape* getShape();
    
    int main()
    {
        shape* shape_obj = getShape();
        shape_obj->toString();
    }
    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. #50
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Right, then you should have posted your main.cpp instead.


    One idea here is that your global main function does not need to know about circle, square, or rectangle. The code that handles the creation of the correct object is in getShape(). All that the global main function needs to know is the shape interface, and that is provided by including shape.h.

    However, to call getShape(), the global main function needs to know something about it, and that can be provided by declaring it:
    Code:
    #include "shape.h"
    
    shape* getShape();
    
    int main()
    {
        shape* shape_obj = getShape();
        shape_obj->toString();
    }
    Can i sort toString by area like this?
    Code:
    for (i=0; i<10-1; i++)
    for (j=i+1; j< 10; j++)
    
    if (dshape[i]->area()>dshape[j])
    {
     temp=dshape[i];
     dshape[i]=dshape[j];
     dshape[j]=temp;
     }
    dshape[i]->toString();

  6. #51
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by jackfraust
    Can i sort toString by area like this?
    Your getShape() function only returns a pointer to one shape, so there is nothing to "sort".
    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. #52
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Your getShape() function only returns a pointer to one shape, so there is nothing to "sort".
    I did this but it craches
    Code:
    	shape* mshape[10];
    	int i;
    	
    	for ( i = 0; i < 2; ++i){
        shape *shape_obj = getShape();
    	mshape[10] = shape_obj ;}

  8. #53
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    mshape[10] does not exist, since mshape is an array of 10 shape pointers.
    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. #54
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    mshape[10] does not exist, since mshape is an array of 10 shape pointers.
    How do you suppose i can store the shape* in an array and pring them all at one time with toString()

  10. #55
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    here is what i did.



    Code:
    	shape* mshape[10];
    	int i, number;
    	cout << "CREATING SHAPE(circle, rectangle or square)\n\n" <<endl;
    	cout << "\nHow many shape you wish to create?  " << endl;
    	
    	for ( i = 0; i < number; ++i){
        shape *shape_obj = getShape();
    	mshape[i] = shape_obj;} //store shapes into array
    	
    	for (i = 0; i < number; ++i){
    		cout << mshape[i]->toString() << endl;} //print description of shapes
    Now i need to sort it

  11. #56
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Regarding code formatting/layout: Try to put your braces on separate lines - in particular the end brace should be on a line of it's own, so that it is easy to see where the block ends.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #57
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    how come i can't sort the mshape[i]->toString() in ascending order depending on the area?

    Code:
    	 for (int nStartIndex = 0; nStartIndex < number; nStartIndex++)  
     {     
         int nSmallestIndex = nStartIndex;  
       
          
         for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < number; nCurrentIndex++)  
         {  
                
             if ( mshape[nCurrentIndex]->area() < mshape[nSmallestIndex]->area() )  
                  
                 nSmallestIndex = nCurrentIndex;  
         }  
       
           
         swap(mshape[nStartIndex]->area(), mshape[nSmallestIndex]->area()); 
    
     cout << mshape[nStartIndex]->toString << endl;
    
    }
    that is what i wrote but it doesn't work.

Page 4 of 4 FirstFirst 1234
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