Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A class Coord has two attributes of x and y coordinates. The class has two membe

ID: 3685505 • Letter: A

Question

A class Coord has two attributes of x and y coordinates. The class has two member functions as defined below.

#include <cstdlib>
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.

Explanation / Answer

class line
{
//variables required-
two coordinates using which line has to be drawn, because centre of gravity lies on line of symmetry

}

class circle
{
//variables required-
radius of the circle, the centre of gravity for a semi-circle is at 4*r/(3*pi) where r is
the radius and pi=3.14

}

b) operator overloading using '<< ' operator

line line:: operator <<(coord centre,int d1, int d2,ostream & out)
{
out<<d1<<d2<<centre;
return out;
}

circle circle::operator<<(coord centre,ostream& out)
{
out<<centre;
return out;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote