任何帮助编写关于课程的程序都会受到赞赏,并且已经尝试了一段时间 [英] Any help writing this program about classes would be appreciated, been trying for awhile
问题描述
任何帮助撰写此计划的所有部分的人都将不胜感激。我完全迷失了需要做什么。
类Coord有两个x和y坐标属性。该课程有两个成员函数,如下所示。
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.
#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)几何类Circle和类Line是从GeoObj派生的。定义并提供类Circle类和Line [10 Marks]的实现。定义并提供名为distance的函数模板的实现,该函数模板处理GeoObj [10 Marks]的两个对象之间的重心距离。计算应该使用运算符 - 用于两个对象的重心。这个距离函数的用法如下所示:
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)定义并提供运算符的实现<<输出类型和center_of_gravity。例如,对于Circle对象,运算符<<输出Circle类型,后跟center_of_gravity的X和Y坐标。同样,对于Line对象,运算符<<输出Line,后跟center_of_gravity的X和Y坐标。您需要确定此运算符的位置(例如,类GeoObj,类Circle或类Line)<<应该定义。此操作符允许输出到标准显示,如控制台或文件。
c)定义一个驱动程序来测试类Circle,Line,function template distance和operator< ;< cout和文件流重载。
我尝试过:
我一直无法想出任何东西。我一直试图理解这个问题大约一周。
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.
推荐答案
我一直无法想出任何东西。我一直试图了解这个问题大约一周
I have been unable to come up with anything. I have been trying to understand this problem for around a week
为了完成你的作业,你需要了解
- center-of-mass是 [ ^ ](请注意,找到一个圆或一条线的质心是一项非常简单的任务。)
- 如何编写派生类(有许多教程免费提供。)
- 如何定义函数模板(再次, Google 是你的朋友)。
- 如何在C ++中重载运算符(例如,operator overloading - cppreference.com [ ^ ])。
- 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"[^]).
In order to complete your homework you need to understand
这篇关于任何帮助编写关于课程的程序都会受到赞赏,并且已经尝试了一段时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!