Overloading Operators and Class Inheritance

I was wondering how I would do this, I have a class called Shape2D with overloaded + and - operators and then I have 2 inherited classes called Circle and Rectangle.

The overloaded operators just add the protected variable area defined in the Shape2D class together. I looked at the overloading operator tutorial section and made it exactly like that. I'll post part of my code so you guys can look over it and help me a little bit.

I had it like the overloaded - operator first but I kept getting a "Shape2D is not an accessible base of Circle" or something along those lines and then I overloaded it accepting either a circle or rectangle thinking that was the problem but now it's saying "declaration of 'operator+' as non-function".

Hope I provided enough information, looking for help quick because it's due in an hour. I'd still like to know what's wrong if I can't get it done in time though. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Shape2D
{
  protected:
          float area;
          
  public:
         //constructor/destructor
         Shape2D()
           { area = 0; }
         Shape2D(float A)
           { area = A; }
         ~Shape2D()
           { area = 0; }
         
         //overloaded operators
         Shape2D operator+(Circle);
         Shape2D operator+(Rectangle);
         Shape2D operator-(Shape2D);
         
         //acessors and mutators
         float getArea()
           { return area; }
         
         void setArea(float A)
           { area = A; }
};

//SHAPE 2D FUNCTIONS
Shape2D Shape2D::operator+ (Circle param)
{
  Shape2D temp;
  temp.area = area + param.area;
  return temp;
}

Shape2D Shape2D::operator+ (Rectangle param)
{
  Shape2D temp;
  temp.area = area + param.area;
  return temp;
}

Shape2D Shape2D::operator- (Shape2D param)
{
  Shape2D temp;
  temp.area = area - param.area;
  return temp;
}



class Rectangle: Shape2D
{
  protected:
          float length;
          float width;
  
  public:
         //constructors/destructors
         Rectangle()
           { length = 0; width = 0; Shape2D(); }
         Rectangle(float L, float W)
           { length = L; width = W; Shape2D(L * W);}
         ~Rectangle()
           { Rectangle(); }
         
         //accessors and mutators
         float getLength()
           { return length; }
         float getWidth()
           { return width; }
         void setLength(float L)
           { length = L; }
         void setWidth(float W)
           { width = W; }
};


and then in the main function I'm declaring a seperate Shape2D class called ShA that holds the temp.area value returned from the overloaded operators, it's like this (CirA and CirB being Circle objects):

1
2
 Shape2D ShA;
ShA = CirA + CirB;
Last edited on
This is most likely because you have no forward declaration of either of the classes you're using in the operator overloads. The compiler isn't very picky, but it at least needs to know what objects and functions exist, even if it can't find any definitions. Therefore, all you need to do is add a very basic declaration of the classes you're using, and the compiler should assume that you will fill in methods and properties at some point later in the code.

1
2
3
4
class Circle; // The compiler now knows these exist
class Rectangle; // No further information is required yet

// ...Continue the definition of your classes, as you had them 

I think you may need to do it this way:

1
2
3
4
class Rectangle: public Shape2D
{
// code
};
Topic archived. No new replies allowed.