为了账号安全,请及时绑定邮箱和手机立即绑定

c++编程点和圆(类与对象)?

/ 猿问

c++编程点和圆(类与对象)?

翻翻过去那场雪 2019-05-24 19:15:16

c++编程点和圆(类与对象)


查看完整描述

3 回答

?
慕斯卡3215842

您好,这样:
#include <iostream>
using namespace std;
class Point
{
public:
Point(int xx,int yy) { X=xx; Y=yy; } //构造函数
Point(Point &p);
int GetX(void) { return X; } //取X坐标
int GetY(void) { return Y; } //取Y坐标
private:
int X,Y; //点的坐标
};
Point::Point(Point &p)
{
X = p.X;
Y = p.Y;
cout << "Point拷贝构造函数被调用" << endl;
}
class Distance
{
public:
Distance(Point a,Point b); //构造函数
double GetDis() { return dist; }
private:
Point p1,p2;
double dist; // 距离
};
// 组合类的构造函数
Distance::Distance(Point a, Point b):p1(a),p2(b)
{
cout << "Distance构造函数被调用" << endl;
double x = double(p1.GetX() - p2.GetX());
double y = double(p1.GetY() - p2.GetY());
dist = sqrt(x*x + y*y);
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
Point myp1(1,1), myp2(4,5);
Distance myd(myp1, myp2);
cout << "The distance is:";
cout << myd.GetDis() << endl;
return 0;
}
这段程序的运行结果是:
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Distance构造函数被调用
The distance is:5



查看完整回答
反对 回复 2019-05-26
?
aluckdog

#include <iostream>

#include <cmath>

 

#include <iomanip>

 

using namespace std;

class Point

{

    double x,y;

public:

    Point();

    Point(double x_value, double y_value);

    double getX();

    double getY();

    void setX(double x_value);

    void setY(double y_value);   

    double distanceToAnotherPoint(Point p);

};

Point::Point()

{

    x=y=0;

}

Point::Point(double x_value, double y_value):x(x_value),y(y_value)

{

}

double Point::getX()

{

    return x;

}

double Point::getY()

{

    return y;

}

void Point::setX(double x_value)

{

    x = x_value;

}

void Point::setY(double y_value)

{

    y = y_value;

}

double Point::distanceToAnotherPoint(Point p)

{

    return sqrt(pow(x-p.getX(), 2) + pow(y-p.getY(), 2));

}

 

int main()

{

    int n;

    cin >> n;

    while(n --)

    {

        double a,b;

        cin >> a >>b;

        Point p1(a,b);

        cin >> a >> b;

        Point p2(a,b);

        cout << "Distance of Point(" <<fixed<<setprecision(2)<< p1.getX() <<',' << p1.getY() <<") to Point("<< p2.getX() <<',' << p2.getY()<< ") is " << p1.distanceToAnotherPoint(p2) << ' ';

    }

}


查看完整回答
反对 回复 2019-05-26
?
POPMUISE

#include<iostream>
#include<string>
using namespace std;
class traffic_tool
{protected:
double speed;
string function;
public:
traffic_tool(double s,string f) {speed=s; function=f;}
void show_feature()
{cout<<speed<<" "<<function<<endl;}
};

class traffic_tool_ludi:public traffic_tool
{private:
int wheels;
public:
traffic_tool_ludi(double s,string f,int w0):traffic_tool(s,f)
{wheels=w0;}
void show_feature()
{cout<<speed<<" "<<function<<" "<<wheels<<endl;}
};

class traffic_tool_sky:public traffic_tool
{private:
string kind;
public:
traffic_tool_sky(double s,string f,string k):traffic_tool(s,f)
{kind=k;}
void show_feature()
{cout<<speed<<" "<<function<<" "<<kind<<endl;}
};

class traffic_tool_water:public traffic_tool
{private:
string weight;
public:
traffic_tool_water(double s,string f,string w1):traffic_tool(s,f)
{weight=w1;}
void show_feature()
{cout<<speed<<" "<<function<<" "<<weight<<endl;}
};
void main()
{traffic_tool t1(80,"运货");
traffic_tool_ludi t2(120,"运货或载人",4);
traffic_tool_sky t3(234,"运货或载人","直升机");
traffic_tool_water t4(60,"运货或载人","100吨");
t1.show_feature();
t2.show_feature();
t3.show_feature();
t4.show_feature();
}

查看完整回答
反对 回复 2019-05-26

添加回答

回复

举报

0/150
提交
取消
意见反馈 邀请有奖 帮助中心 APP下载
官方微信