954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Debugging

Hello guyz!
Im conducting a debugging contest...

Im writing some questions... I wrote a code implementing virtual functions..

Im not sure where to include bugs here.. Can anyone help me add bugs to this code??
Please bugs must make people think.. THey must not just be syntactical errors..


Thank you..
Output wil be given to the debugger.. and he will asked to debug the code to produce desired result Thanks!

#include <iostream>
   using namespace std;


   class Shape
   {
   public:
      Shape()
      {}

      virtual long GetArea()
      {
        cout<<"Area";
      }
      virtual long GetPerimeter()
      {
        cout<<"Perimeter";
      }
      virtual void Draw() = 0;

   };

   void Shape::Draw()
   {
      cout << "Shape Drawn!!";
   }

   class Circle : public Shape
   {
   private:
      int radius;
      int circumference;
   public:
         Circle(int rad):radius(rad)
         {
         }

         long GetArea() { 
         return 3 * radius * radius; 
         }
         long GetPerim() { 
         return 9 * radius; 
         }
         void Draw()
         {
            cout << "Circle drawing routine here!\n";
         }
   };

   class Rectangle : public Shape
   {
   private:
      int Width;
      int Length;

   public:
         Rectangle(int len, int width):
            Length(len), Width(width)
          {}

         long GetArea() 
         { 
          return Length * Width; 
         }
         long GetPerim() 
         {
          return 2*Length + 2*Width; 
         }
         virtual int GetLength() 
         {  
            return Length; 
         }
         virtual int GetWidth() 
         { 
            return Width; 
         }
         void Draw()
         {
            for (int i = 0; i<Length; i++)
            {
               for (int j = 0; j<Width; j++)
                  cout << "x ";

               cout << "\n";
            }
          
         }
   };

   class Square : public Rectangle
   {
   public:
         Square(int len):
            Rectangle(len,len)
            {}
         Square(int len, int width):
             Rectangle(len,width)

             {
                if (GetLength() != GetWidth())
                   cout << "Error, not a square... a Rectangle??\n";
             }

         long GetPerim() {
          return 4 * GetLength();
         }
   };

   int main()
   {
      int choice;

      Shape * sp;

      while (1)
      {
         cout << "1.Circle\n2.Rectangle\n3.Square\n4.Quit\nEnter Choice.. ";
         cin >> choice;

         switch (choice)
         {
            case 1: sp = new Circle(5);
            break;
            case 2: sp = new Rectangle(4,6);
            break;
            case 3: sp = new Square (5);
            break;
         }
         if (choice==4)
            break;

         sp*Draw();
         cout << "\n";
      }
     return 0;
 }
stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

Remove the word public from a couple of the classes.
Put an =0 on a constructor.
Use the same shape twice on two of your choices.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Well, the most obvious bug is that you don’t have a default case for your switch, and you don’t bounds check the user input. Also, you don’t use Pi to calculate the area of the circle. You don’t use Pi to calculate the perimeter, but if you did, the formula is incorrect.

If I were trying to add more bugs, I would want the user to input the length and width of the shapes. It is very easy to have bug in you code when you are validating or handling data. An example would be reversing the height and width of a rectangle. You could also allow non-integer input from the user. That would make things more interesting.

FortranIV
Newbie Poster
16 posts since Jun 2007
Reputation Points: 17
Solved Threads: 6
 

@FortranIV. Good suggestions.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Thanks guyz!!

stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

Write a templated class that makes some assumptions about the template type. Instantiate it with a type that violates said assumptions. Watch the debugger point you to the constructor when the error is in the instantiation.

Zcool31
Junior Poster in Training
55 posts since Mar 2009
Reputation Points: 10
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You