$30
A class Coord has two attributes of x and y coordinates. The class has two member functions as defined below.
#include
class Coord {
private:
int x, y;
public:
Coord (int i1, int i2) : x(i1), y(i2) {
}
friend Coord operator- (Coord const& c1, Coord const& c2) {
return Coord(c1.x-c2.x, c1.y-c2.y);
}
Coord abs() {
return Coord(std::abs(x),std::abs(y));
}
int getX() {return x};
int getY() {return y};
};
/*Note int abs (int n); Returns the absolute value of parameter n*/
A base class GeoObj is for geometric objects.
class GeoObj {
public:
// return center of gravity of geometric object.
virtual Coord center_of_gravity() const ;
//...
private:
Coord center; // center_of_gravity;
};
a) A geometric class Circle and class Line is derived from GeoObj. Define and provide implementation of class Circle class and Line [10 Marks]. Define and provide implementation of a function template called distance that processes the distance of center of gravity between two objects of GeoObj [10 Marks]. The calculation is supposed to use the operator – for the center of gravity of two objects. The usage of this distance function is shown below.
int main()
{
Line l;
Circle c, c1, c2;
Coord d1 = distance(c1,c2);
Coord d2 = distance(l,c);
}
b) Define and provide implementation of operator << that outputs the type and center_of_gravity. For example, for a Circle object, the operator << outputs the type “Circle” followed by the X and Y coordinate of center_of_gravity. Likewise, for a Line object, the operator << outputs “Line” followed by the X and Y coordinate of center_of_gravity. You need to decide where (e.g. class GeoObj, class Circle or class Line) this operator << should be defined. This operator allows outputs to standard display such as the console or to a file.
c) Define a driver to test the class Circle, Line, function template distance and operator << overloaded for both cout and file streams.