Click here to Skip to main content
15,888,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Any help writing all the parts for this program would be greatly appreciated. I am completely lost as to what needs to be done.

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

C++
 #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.

C++
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.

What I have tried:

I have been unable to come up with anything. I have been trying to understand this problem for around a week.
Posted
Updated 10-Apr-16 21:21pm
v3
Comments
Richard MacCutchan 11-Apr-16 3:17am    
Assuming thi is part of a college course you need to reread your course notes, or talk to your tutor or co-students.
Aescleal 11-Apr-16 6:13am    
From the unfocussed nature of the question I'd ask for your money back...

1 solution

Quote:
I have been unable to come up with anything. I have been trying to understand this problem for around a week

In order to complete your homework you need to understand
  • what the center-of-mass is[^] (please note, locating the center-of-mass of a circle or a line is really a trivial task).
  • How to write a derived class (there are many many tutorials freely available).
  • How to define a function template (again, Google is you friend).
  • How to overload operators in C++ (see, for instance "operator overloading - cppreference.com"[^]).
 
Share this answer
 
Comments
Member 12450567 11-Apr-16 2:52am    
I have tried all of that and still don't understand much. I have no idea what to do

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900