Need some help with polymorphism and overloading the extraction operator

I have an assignment to learn about polymorphism. I'm suppose to create a class hierarchy to represent shapes. I have included the code I have which does work. I haven't included all of my classes since they are mostly the same, just enough to get the idea across. I was having trouble with the virtual print and the overloaded << operator working right. After posting and researching this i what i came up with, which does work. But unfortunately my prof said this:

"What you saw posted on the forum is not the correct code for this assignment. You would overload the << operator for the Shape class just as you normally would. You would then create a virtual print function in all your classes similar to how the example starting on page 554 has a virtual print function in all the classes. The virtual print function for your assignment will only be called in the overloaded << operator."

Which i kind of figured. What i was doing didn't seem to be jiving with the intent of the assignment and what i had learned in the chapter.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class Shape
{
    friend ostream &operator<<( ostream &, const Shape &); // overloaded insertion operator
        
public:
    Shape(); // default constructor
    virtual void print(ostream&) const = 0; //pure virtual print function

};

#include <iostream>
#include <iomanip>
#include "Shape.h"
using namespace std;

Shape::Shape()
{
}
ostream &operator<< (ostream& output, const Shape & aShape)
{
    aShape.print(output);
    return output;
}
//-----------------------------------------

Circle::Circle(double r)
{
    setRadius(r);
}

void Circle::setRadius(double r)
{
    radius = r;
}

double Circle::getRadius() const
{
    return radius;
}

double Circle::getArea() const
{
    return  radius * radius * M_PI;
}

void Circle::print(ostream& output) const
{
    //TwoDimensionalShape::print(output);
    output << "Circle\n" << "Radius: " << getRadius() << endl << "Area: " << getArea() << endl;
}
//-------------------------

#include "Shape.h"

class TwoDimensionalShape : public Shape
{
public:
    virtual double getArea() const = 0; //pure virtual fuction
    virtual void print(ostream&) const=0; //pure virtual print function
};
//---------------------------

#include "TwoDimensionalShape.h"

class Circle : public TwoDimensionalShape
{
public:
    Circle(double); //default constructor
    void setRadius(double); //sets radius
    double getRadius() const; //gets radius
    virtual double getArea() const; //return area of circle
    virtual void print(ostream&) const; //print function

private:
    double radius;
};

#include <iostream>
#include <math.h>
#include "Circle.h"

Circle::Circle(double r)
{
    setRadius(r);
}

void Circle::setRadius(double r)
{
    radius = r;
}

double Circle::getRadius() const
{
    return radius;
}

double Circle::getArea() const
{
    return  radius * radius * M_PI;
}

void Circle::print(ostream& output) const
{
    //TwoDimensionalShape::print(output);
    output << "Circle\n" << "Radius: " << getRadius() << endl << "Area: " << getArea() << endl;
}

//----------------------------

#include "Shape.h"

class ThreeDimensionalShape : public Shape
{
public:
    virtual double getSurfaceArea() const = 0;
    virtual double getVolume() const = 0;
    virtual void print(ostream&) const=0;

};
//-----------------------------------

#include "ThreeDimensionalShape.h"

class Cube : public ThreeDimensionalShape
{
public:
    Cube(double);
    virtual double getSurfaceArea() const;
    virtual double getVolume() const;
    void setSide(double);
    double getSide() const;
    virtual void print(ostream&) const; 
private:
    double side;
};

#include <iostream>
#include "Cube.h"

Cube::Cube(double s)
{
    setSide(s);
}

void Cube::setSide(double s)
{
    side = s;
}

double Cube::getSide() const
{
    return side;
}

double Cube::getSurfaceArea() const
{
    return 6 * side * side;
}

double Cube::getVolume() const
{
    return side * side * side;
}

void Cube::print(ostream&  output) const
{
    //ThreeDimensionalShape::print(output);
    output << "Cube\n" << "Side Length: " << getSide() << endl << "Volume: " << getVolume() << endl << "Surface Area: " << getSurfaceArea() << endl;
}
//-----------------------------------------


When i try to do it as he said, or how i think he meant, i get an error. Here's what i have when trying to follow he's description:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Shape
{
    friend ostream &operator<<( ostream &, const Shape &); // overloaded insertion operator
    
public:
    Shape(); // default constructor
    virtual void print() const = 0; //pure virtual print function
 
Shape::Shape()
{
}
ostream &operator<< (ostream& output, const Shape & aShape)
{
    output << aShape.print();
    return output;
}

i get this error: Invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'void')
Last edited on
('ostream' (aka 'basic_ostream<char>') and 'void')
It's saying the operands ostream aka output and void aka the return value of aShape.print() are invalid and rightfully so.

As you have it coded you will need to do this...

 
aShape.print(output);


and not
 
output << void; //returned from print, makes sense now? 
Last edited on
That is what i have done in my code that is posted above. If you look at the Shape class and its implementation you'll see that. But that seems to be the problem for my prof. Maybe i'm mistaken on his intent but it seems that i'm suppose to just have a simple virtual void print(); member function that is called from the overloaded << operator in the the shape class
Topic archived. No new replies allowed.