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 jackfraust i called it like this: Code: int main(); { Shape getShape(); Shape::toString(); } What? That won't ...

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by jackfraust View Post
    i called it like this:
    Code:
    int main();
    {
    Shape getShape();
    Shape::toString();
    }
    What? That won't compile with what you've posted so far. I thought you said your code compiled?

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    What? That won't compile with what you've posted so far. I thought you said your code compiled?
    the code is posted in the 1st page. this is the main function to run the functions

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Well, there's your problem. Your main() doesn't compile.

  4. #19
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    Well, there's your problem. Your main() doesn't compile.
    That's what i have been saying. it compiles with no errors when the program runs and exit without showing nothing on the terminal.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Here's an abbreviated version with the same main:

    Code:
    #include <string>
    
    using namespace std;
    
    class Shape {
    public:
    	virtual string toString() const = 0;
    };
    
    class Circle : public Shape {
    public:
    	virtual string toString() const { return "Circle"; }
    };
    
    Shape * getShape()
    {
    	Shape * circle = new Circle;
    
    	return circle;
    }
    
    int main();
    {
    	Shape getShape();
    	Shape::toString();
    }
    When I try to compile with g++, I get this:

    shape.cpp:23: error: expected unqualified-id before '{' token

    When I remove the ';' after main, I get this:

    shape.cpp: In function `int main()':
    shape.cpp:24: error: invalid return type for function `Shape getShape()'
    shape.cpp:24: error: because the following virtual functions are abstract:
    shape.cpp:7: error: virtual std::string Shape::toString() const
    shape.cpp:25: error: cannot call member function `virtual std::string Shape::toString() const' without object

    Am I missing something?
    Last edited by medievalelks; 04-07-2009 at 09:09 PM.

  6. #21
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    Here's an abbreviated version with the same main:

    Code:
    #include <string>
    
    using namespace std;
    
    class Shape {
    public:
    	virtual string toString() const = 0;
    };
    
    class Circle : public Shape {
    public:
    	virtual string toString() const { return "Circle"; }
    };
    
    Shape * getShape()
    {
    	Shape * circle = new Circle;
    
    	return circle;
    }
    
    int main();
    {
    	Shape getShape();
    	Shape::toString();
    }
    When I try to compile with g++, I get this:

    shape.cpp:23: error: expected unqualified-id before '{' token

    When I remove the ';' after main, I get this:

    shape.cpp: In function `int main()':
    shape.cpp:24: error: invalid return type for function `Shape getShape()'
    shape.cpp:24: error: because the following virtual functions are abstract:
    shape.cpp:7: error: virtual std::string Shape::toString() const
    shape.cpp:25: error: cannot call member function `virtual std::string Shape::toString() const' without object

    Am I missing something?
    Yes the getShape() function is used to get the dimension the shape. The myShape[6] is a fixed size array which is suppose to contain the information for all the 6 shapes that will be created.
    The main function is suppose to call the getShape() function and the toString function that will print the information that the getShapte() gets. The toString function is supposed to be called in the main function. Here is the code i made
    getShape.cpp
    Code:
    #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;
    	string color;
    	double width;
    	double length;
    	double radius;
    	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;
    	cin >> choice;
    	switch(choice){
    	
    	case '1':
    	cout << "\nEnter the shape's color (or 'done')...";
    	cin >>  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[6];
    
    }
    Shape.h and Shape.cpp
    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 <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include "Shape.h"
    
    Shape::Shape(const string &color)
    :s_color(color)
    {}
    
    string Shape::getColor()const
    {
    	return s_color;
    }
    Circle.h and Circle.cpp
    Code:
    #ifndef CIRCLE_H
    #define CIRCLE_H
    
    #include <string>
    using std::string;
    
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    #include "shape.h" //shape class definition
    
    class circle : public Shape
    {
    public:
    	circle (const string &color, double);//constructor
    	~circle();//dtor 
    	double getRadius();
    	virtual double area()const;//calculate area
    	virtual string toString()const;
    private:
    	double rad; //radius of circle
    
    };
    
    #endif //CIRCLE_H
    
    =============================
    #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
    square.h and square.cpp
    Code:
    #ifndef SQUARE_H
    #define SQUARE_H
    
    
    #include "shape.h"
    
    
    class square : public Shape
    {
    public:
    	square(const string &color, double length); //constructor
    	~square();//destructor
    	double getLength();
    	virtual double area()const;//calculate area
    	virtual string toString()const;
    
    private:
    	double len; //length of square
    };
    #endif //SQUARE_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
    rectangle.h and rectangle.cpp
    Code:
    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    #include <string>
    using std::string;
    
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include "shape.h"
    
    
    class rectangle : public Shape
    {
    
    public:
    	rectangle(const string &color, double width, double length);//constructor
    	~rectangle();//dtor
    	double getLength();
    	double getWidth();
    	virtual double area()const; //calculate area
    	virtual string toString()const;
    
    private:
    	double len;  //length of rectangle
    	double wid;	//width of rectangle
    };
    
    
    #endif //RECTANGLE_H
    ========================
    
    #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)
    {
    len = length;
    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

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    No, I meant, am I missing something about your alleged main() function, which doesn't work as you posted it? This is broken. Fix it, and stop saying it compiles. It doesn't.

    Code:
    int main();
    {
            Shape getShape();
            Shape::toString();
    }

  8. #23
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by medievalelks View Post
    No, I meant, am I missing something about your alleged main() function, which doesn't work as you posted it? This is broken. Fix it, and stop saying it compiles. It doesn't.

    Code:
    int main();
    {
            Shape getShape();
            Shape::toString();
    }
    it compiled in visual C++. How do you supposed i call the getShape function in main()?

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    If Visual C++ compiles that, get rid of it and download g++. I would expect your main to look something like this:

    Code:
    int main()
    {
        Shape * s = getShape();
    
        string str = s->toString();
    
        ...
    
        delete s;
    
        return 0;
    }
    Last edited by medievalelks; 04-09-2009 at 05:34 AM.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by jackfraust
    it compiled in visual C++.
    What compiled in Visual C++?

    Quote Originally Posted by medievalelks
    If Visual C++ compiles that
    It does not.
    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

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Yeah, that surprised me, too, unless there was some onknown flag to allow the parsing of random English text.

  12. #27
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    What compiled in Visual C++?


    It does not.
    I'm telling you it really did compiled for me and the terminal wouldn't display anything. But tell me, where do you think the error is?

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by jackfraust
    I'm telling you it really did compiled for me and the terminal wouldn't display anything. But tell me, where do you think the error is?
    I ask again? What is it? What compiled for you? Post it in your reply, even if you have posted it earlier.
    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. #29
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    I ask again? What is it? What compiled for you? Post it in your reply, even if you have posted it earlier.
    when i say compiled i dont mean "work". It did compile without errors but the program doesn't run meaning there's a problem in the code. suppose the program is named shape.exe; I run shape.exe but nothing happens, no crash or nothing, it just doesnt run.

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    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.
    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 2 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