Homework project(need help)

This is a discussion on Homework project(need help) within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. Here is my problem.I am given four classes (Point,Shape,Circle,Rectangle) and I must complete theme so as the program ...

  1. #1
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    Homework project(need help)

    Hello everyone. Here is my problem.I am given four classes (Point,Shape,Circle,Rectangle)
    and I must complete theme so as the program works.
    Here is my project:
    Code:
       //shapes.h
    #ifndef shapes_h
    #define shapes_h
    
    1 class Point{
               private:
                  int x;
                  int y;
    
           public:
                  Point();
                  Point(int k, int l);
                  ~Point();
                  void setX(int );
                  int getX() const;
                  void setY(int );
                  int getY() const;
                  void print() const;
                 Point & operator=(const Point &p1);
                  };
    
     
    //Abstract class
    2 class Shape {
                public:
                //.....
                virtual double Area() =0;
                virtual double Perifereia() =0;
                virtual void print() const = 0;
    
                };
    3 class Rectangle : public Shape {
                 private:
                 Point *myUpperLeft;
                 Point *myLowerRight;
                 public:
                 double Area();
                 void print() const;
                 double Perifereia();
                 int GetWidth() const; 
                 int GetHeight() const; 
    
                 Rectangle(Point *pUL, Point *pLR);
               };
    
    4 class Circle : public Shape, public Point 
                                 {
                                  public:
                                  // .......
                                   private:
                                   double radius;
                                  virtual double Area() =0;
                                  };
        #endif
    shapes.cpp
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    #include "shapes.h"
    
    //Class Point Functions
    Point::Point(int k,int l )
    {x=k;y=l;}
    Point::Point ()
    {x=y=0;}
    Point::~Point()
    {}
    void Point::setX(int xValue) {x=xValue;}
    int Point::getX() const{return x;}
    void Point::setY(int yValue) {y=yValue;}
    int Point::getY() const{return y;}
    void Point::print()const{cout<<"["<<x<<","<<y<<"]"<<endl;}
    
     Point & Point::operator=(const Point &p1)
     {
          if(this !=&p1)
            {
             x=p1.x;
             y=p1.y;
            }
     return *this;
     }
    
    //Class  Rectangle functions
    int Rectangle::GetWidth()const{return myUpperLeft->getX()-myLowerRight->getX();} /*line1*/
    
    Rectangle::Rectangle(Point *pUL, Point *pLR)    //error here                 /*line2*/
    {
     myUpperLeft=pUL;
     myLowerRight=pLR;
    };
    and main.cpp has nothing special.


    I use Dev-C++ and the error message is
    "[Linker error] undefined reference to 'vtable for Rectangle'"
    Last edited by kantze; 12-15-2006 at 09:39 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,787
    hmm... Dev-C++? If I remember correctly there was some strange bug such that a custom class with the name "Rectangle" causes problems. You may have to change the class name, or perhaps place it in some namespace (recommendation not tested).

    EDIT:
    Okay, I did a test and could not duplicate the error that I had with a class named "Rectangle" some time back. It could be that I am mistaken.
    Last edited by laserlight; 12-15-2006 at 09:09 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Well,maybe. In fact, I tested it in MS Visual C++ and the message is
    Linking...
    shapes.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Rectangle:rint(void)const " (?print@Rectangle@@UBEXXZ)
    shapes.obj : error LNK2001: unresolved external symbol "public: virtual double __thiscall Rectangle::Area(void)" (?Area@Rectangle@@UAENXZ)
    Debug/main.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,480
    Another bug in the "flawless" devC++. It would not hurt to have a back up IDE. Code::blocks is quite good as is any MS one apart from version 6.0. Bloodshed have abandoned working on DevC++. 4.992 the final version. It has a few bugs still, which were never filtered out. Most of the help files are missing or painfully incomplete too. As Laserlight pointed out.
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Maybe swgh. But my PROBLEM is that allthough line1 works line2 doesn't. Any ideas?

  6. #6
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    do you have a body for Area function in Rectangle class?
    To be or not to be == true

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,787
    Wait - Rectangle is an abstract base class itself, since you did not implement some of the virtual functions inherited from Shape. I jumped to a premature conclusion.

    May I suggest a few changes? For one thing, you might want to improve your code formatting to make your code easier to read: one reason why I made my earlier assumption was that I could not be bothered to examine your code carefully in the messed up form that was displayed in my browser.

    Also, Point does not need to define a copy assignment operator or a destructor as the compiler generated versions will work perfectly fine.
    Last edited by laserlight; 12-15-2006 at 09:27 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Well I haven't completed it yet. I guess I would write in shapes.cpp
    Code:
    int Rectangle::GetHeight()const{  return myUpperLeft->getY()  -  myLowerRight->getY();  }
    and then
    Code:
    double Rectangle::Area{return GetWidth() * GetHeight()}

  9. #9
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Well I tried to make it a bit more readable. And as for the destructors and constructors I don't care since they do not create any problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

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