GPoint在C++中怎么用

发布网友 发布时间:2022-04-20 15:35

我来回答

1个回答

热心网友 时间:2023-07-15 07:39

#include <iostream>
using namespace std;

struct Position{
int x;
int y;
Position(int x=0,int y=0):x(x),y(y){}
};

class Point{
Position* p;
static int total;
public:
Point(){p=new Position;++total;}
Point(int x,int y){p=new Position(x,y),++total;}
Point(const Point& pt){
p=new Position(pt.p->x,pt.p->y);
++total;
}
~Point(){delete p;}

static void showtotal(){cout<<"total:"<<total<<endl;}

void showx(){
cout<<"x="<<p->x<<endl;
}
void showx()const{
cout<<"x="<<p->x<<endl;
}
void showy(){
cout<<"y="<<p->y<<endl;
}
void showy()const{
cout<<"y="<<p->y<<endl;
}
void showpoint(){
cout<<"x="<<p->x<<endl;
cout<<"y="<<p->y<<endl;
}
void showpoint()const{
cout<<"x="<<p->x<<endl;
cout<<"y="<<p->y<<endl;
}

friend ostream& operator<<(ostream& os,const Point& pt){
os<<"x:"<<pt.p->x<<endl;
os<<"y:"<<pt.p->y<<endl;
return os;
}
};
int Point::total=0;

class Line:public Point{
Position p2;
public:
Line(int x=0,int y=0,int i=0,int j=0):Point(x,y),p2(x,y){}
void showp(){
cout<<p2.x<<p2.y<<endl;
}
};

int main()
{
Point p1,p2(2,4);
const Point cp(4,6);
cout<<p1<<p2<<endl;//调用operator<<()
Point::showtotal();
p1.showpoint();
p2.showpoint();
cp.showpoint();
return 0;
}

//vc6.0中测试通过

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com