# | Problem | Pass Rate (passed user / total user) |
---|---|---|
14183 | EE2310_Lab12-1 |
|
14184 | EE2310_Lab12-2 |
|
Description
#include <iostream>
#include <string>
using namespace std;
class GeometricObject {
public:
GeometricObject();
GeometricObject(const string& color, bool filled);
string toString() const;
/* overload the << operator. Google "stream insertion operator" */
/* declare member variables as 'protected' */
};
/*
* Implement GeometricObject's member/friend functions
*
*/
//-------------------------------------
class Circle: public GeometricObject {
public:
/*
* Remove all access functions
* Declare your constructors
* Overload the stream insertion operator <<
*/
private:
double radius;
};
/*
* Implement Circle's member/friend functions
*
*/
//-------------------------------------
class Rectangle: public GeometricObject {
public:
/*
* Do the same things for Circle
*
*/
private:
double width;
double height;
};
/*
* Do the same things for Circle
*
*/
/* Do not change main(), or you will lose points */
int main() {
GeometricObject shape("Red", true);
Circle circle(5, "black", false);
Rectangle rectangle(2, 3, "orange", true);
cout << shape << circle << rectangle;
}
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Modify your GeometricObject class definition
Define and implement its constructors well so that the main function works.
Define the toString() function as a virtual function.
Modify your Rectangle class. Define its toString() as a virtual function, too.
Define another class as follows.
class Square: public Rectangle {
public:
Square();
Square(double, const string& , bool);
virtual string toString() const;
};
/*
* Implement its member functions.
*
*/
//-------------------------------------
// Do not change anything below this line, or you will lose points
//-------------------------------------
void displayByValue(const GeometricObject g) {
cout << g.toString() << endl;
}
void displayByReference(const GeometricObject &g) {
cout << g.toString() << endl;
}
void displayByPointer(GeometricObject *ptr_g) {
cout << ptr_g->toString() << endl;
}
int main() {
GeometricObject shape("Red", true);
Rectangle rectangle(2, 3, "orange", true);
Square square(4, "blue", false);
displayByValue(square);
displayByReference(square);
displayByPointer(&square);
return 0;
}