3

I have an assignment in which I need to translate some Ada code to C++ code, making it as similar as possible to the Ada code. The Ada code is as follows

    type Shape is (Circle, Triangle, Rectangle);
    type Colors is (Red, Green, Blue);
    type Figure(Form: Shape := Circle) is
        record
            Filled: Boolean;
            Color: Colors;
            case Form is 
                when Circle =>
                    Diameter: Float;
                when Triangle =>
                    Left_Side: Integer;
                    Right_Side: Integer;
                    Angle: Float;
                when Rectangle =>
                    Side_1: Integer;
                    Side_2: Integer;
            end case;
        end record;

I know I could use a class but judging by the language of the question and the personality of the teacher I assume he is looking for a struct. I am just unclear as to how to create the logic required for the different cases inside of a struct. The teacher is very particular so I assume that the smallest memory footprint is required.

I am a CSc student so forgive me if the solution is simple. Thanks!

Update: So the final answer was simpler than I thought.

enum Shape {Circle, Triangle, Rectangle};
enum Colors {Red, Green, Blue};
struct Figure {
  bool Filled;
  Colors Color;
  Shape Form;
  union {
    float Diameter;
    struct {
      int Left_Side;
      int Right_Side;
      float Angle;
    } tri;
    struct {
      int Side_1;
      int Side_2;
    } rect;
  };
 };

As usual I overthought it. Thanks for all your input!

hurricaneMitch
  • 163
  • 1
  • 8
  • 1
    Welcome to StackOverflow! You say "could use **class** … looking for a **struct**". FYI, `class` and `struct` are nearly synonymous in C++ see http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c. – Robᵩ Mar 26 '12 at 14:33
  • @Robᵩ Thank you. I understand the permission difference, the problem is just complicated by the fact that I am implicitly only allowed to use **struct**, **enum**, and **union** types. Thank you for the clarification. – hurricaneMitch Mar 26 '12 at 16:41
  • As @Robᵩ says, `class` and `struct` are nearly synonymous in C++ -- but as a matter of style, the `struct` keyword is used mostly for [POD](http://en.wikipedia.org/wiki/Plain_old_data_structure) types. I think what you're trying to say is that you're not supposed to use inheritance. – Keith Thompson Mar 26 '12 at 17:28
  • @KeithThompson Well put. Thank you for the clear distinction. – hurricaneMitch Mar 27 '12 at 23:41

2 Answers2

5

It looks like you are suppose to derive classes Circle, Triangle, and Rectangle from a base Shape class. There are common properties (Filled, Color) that needs to go into your base Shape, and derived classes will have diameter, or left-right sides, or other additional properties.

An alternative would be to have a type-id field (enum {Circle, Rectangle, ..})in a Shape struct and a union field holding different sub-structures for diameter and other type dependent members. This would look more like the example (and C) but less like C++.

perreal
  • 94,503
  • 21
  • 155
  • 181
  • 2
    I do think the derived Classes are what are expected, too.. But then, this could have been done in Ada, too, without the variant record. – Rommudoh Mar 26 '12 at 15:00
  • @perreal Thanks! I am sure the required code does not use classes. As such, it is a total kludge. The idea you had of the Shape **struct** with different sub-structures sounds like just the thing I am looking for. I just wrapped my head around it. Thanks again for you help. – hurricaneMitch Mar 26 '12 at 16:21
  • 1
    See also this [comparison](http://en.wikipedia.org/wiki/Tagged_union#1970s_.26_1980s). – trashgod Mar 26 '12 at 16:33
  • @trashgod More information! Thanks a lot. That provided the finishing touches. I totally understand the difference being default permissions but this example is intentionally complicated by the constraint of only **struct**, **enum**, and **union**. I appreciate everyone's help. Thanks! – hurricaneMitch Mar 26 '12 at 16:38
  • 2
    The main difference between the Ada code and the C++ equivalent (using simple structs and unions) is that the latter doesn't enforce the variants. In Ada, if `Obj.Form = Circle`, referring to `Obj.Left_Side` will raise a `Constraint_Error` exception. In C++, referring to `Obj.u.Left_Side` would simply access that part of the structure, quietly giving you garbage. (That's assuming you don't use inheritance.) – Keith Thompson Mar 26 '12 at 17:31
1

For non-class based answer:

  1. Make the enumerations for the Shape and Color types.
  2. Make structs for the Circle, Triangle, and Rectangle data.
  3. Make a Figure struct containing the filled field, colored field, and a field which is a union of the structs made in #2.
  4. [optional] Make "constructors" for the various shapes that return the struct from #3, properly initialized.
Shark8
  • 4,095
  • 1
  • 17
  • 31
  • This was what I finally came up with. I will report back as to exactly what craziness the instructor came up with as the "correct" code. Thank you for the clear explanation! – hurricaneMitch Mar 27 '12 at 23:44