C++ 多边形类的实现
我正在编写一个包含三个类的程序:它们是C++ 多边形类的实现,c++,C++,我正在编写一个包含三个类的程序:它们是点,线和多边形。前两篇我都写了,但最后一篇我写不好 这个类必须有两个构造函数,其中一个用点(多边形的尖端)构建对象,另一个用线构建对象。这些功能必须如下所示: 多边形(点**arr,int size)和多边形(线**arr,int size) 我不知道为什么点和线是指针指向指针的?polygon类的属性是什么?如何编写polygon的构造函数 class point { private: int first; int second; publ
点
,线
和多边形
。前两篇我都写了,但最后一篇我写不好
这个类必须有两个构造函数,其中一个用点(多边形的尖端)构建对象,另一个用线构建对象。这些功能必须如下所示:
多边形(点**arr,int size)
和多边形(线**arr,int size)
我不知道为什么点
和线
是指针指向指针的?polygon
类的属性是什么?如何编写polygon
的构造函数
class point
{
private:
int first;
int second;
public:
point(void);
point (int x,int y);
point (const point& other);
int getX();
int getY();
int distance(point* other);//distance of two point
line* Line(point*);//build a line with two point
polygon* triangle(point*,point*);//build a triangle with three point
~point(void);
};
point::point(void)
{
first=0;
second=0;
}
point::point (int x,int y)
{
first=x;
second=y;
}
point::point(const point& other)
{
first=other.first;
second=other.second;
}
int point::gha(int a)
{
if(a>=0)
return a;
else
return -a;
}
int point::pow(int a)
{
return a*a;
}
int point::getX(){return first;}
int point::getY(){return second;}
int point::distance(point* other)
{
int d= sqrt((pow(first-other->first))+(pow(second-other->second)));
return d;
}
line* point::Line(point* other)
{
line l(this,other);
return &l;
}
polygon* point::triangle(point*,point*){
}
point::~point(void)
{
}
////////////////////////////
class line
{
private:
int m;
int c;
public:
line(void);
line(point*,point*);
line(int ,point*);
bool isParallel(line*);
bool isPrependicular(line*);
point* intersection(line*);
line* parallel(point*);
polygon* triangle(line*,line*);
~line(void);
};
line::line(void)
{
m=1;
c=0;
}
line::line(point* a,point* b)
{
m=((a->getY())-b->getY())/(a->getX()-b->getX());
c=a->getY()-(m*(a->getX()));
}
line::line(int dip,point* a)
{
m=dip;
c=a->getY()-(dip*(a->getX()));
}
bool line:: isParallel(line* other)
{
if(m==other->m)
return true;
else
return false;
}
bool line::isPrependicular(line* other)
{
if((m*other->m)==1 || (m*other->m)==-1)
return true;
else
return false;
}
point* line::intersection(line* other)
{
int x=(other->c-c)/(m-other->m);
int y=(m*x)+c;
point p (x,y);
return &p;
}
line* line::parallel(point* other)
{
line l(m,other);
return &l;
}
line::~line(void)
{
}
///////////////////////////////////
class polygon
{
private:
int count;
point* tip;
line* l;
public:
polygon(void);
polygon(point** arr,int size);
polygon(line** arr,int size);
bool isTriangle();
bool isSquare();
~polygon(void);
};
尝试通过
const
引用传递点
类,例如在距离
方法中。尽可能使用常量引用而不是指针。指针指向指针是修改传递给函数的指针变量的一种方法。也就是说,它指向指针,为什么不使用构造函数参数的<代码> STD::vector < /代码>?如果可能的话,请告诉我如何写多边形的多边形构造函数。如果点*Tip和Lex*L是多边形类的属性。在做更高级的事情之前,你应该学习更多关于C++的知识:你应该学习容器,关于对象成员初始化等,也不要将C与C++(裸指针)混合,空隙在C++中不需要表示非参数函数,只需使用()等。