Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1.     List the problems with the C++ code here:
  2.  
  3. #include <stdio.h>
  4.  
  5. class Feature
  6. {
  7. public:
  8.     enum FeatureType {eUnknown, eCircle, eTriangle, eSquare};
  9.  
  10.     Feature() : type(eUnknown), points(0) { }
  11.  
  12.     ~Feature()
  13.     {
  14.         if (points)
  15.             delete points;
  16.     }
  17.  
  18.     bool isValid()
  19.     {
  20.         return type != eUnknown;
  21.     }
  22.  
  23.     bool read(FILE* file)
  24.     {        
  25.         if (fread(&type, sizeof(FeatureType), 1, file) != sizeof(FeatureType))
  26.             return false;
  27.         short n = 0;
  28.         switch (type)
  29.         {
  30.         case eCircle: n = 3; break;
  31.         case eTriangle: n = 6; break;
  32.         case eSquare: n = 8; break;
  33.         default: type = eUnknown; return false;
  34.         }
  35.         points = new double[n];
  36.         if (!points)
  37.             return false;
  38.         return fread(&points, sizeof(double), n, file) == n*sizeof(double);
  39.     }
  40.     void draw()
  41.     {
  42.         switch (type)
  43.         {
  44.         case eCircle: drawCircle(points[0], points[1], points[2]); break;
  45.         case eTriangle: drawPolygon(points, 6); break;
  46.         case eSquare: drawPolygon(points, 8); break;
  47.         }
  48.     }
  49.  
  50. protected:
  51.     void drawCircle(double centerX, double centerY, double radius);
  52.     void drawPolygon(double* points, int size);
  53.  
  54.     double* points;
  55.     FeatureType type;        
  56. };
  57.  
  58. int main(int argc, char* argv[])
  59. {
  60.     Feature feature;
  61.     FILE* file = fopen("features.dat", "r");
  62.     feature.read(file);
  63.     if (!feature.isValid())
  64.         return 1;
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement