#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
Point(double xx, double yy)
{
x = xx;
y = yy;
};
void setvalue();
void Getxy();
friend double Distance(Point &a, Point &b);
private:
double x, y;
};
void Point::setvalue(){
cout<<"輸入點x,y的值(空格鍵分開):";
cin>>x;
cin>>y;
}
void Point::Getxy()
{
cout << "(" << x << "," << y << ")" << endl;
}
double Distance(Point &a, Point &b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx*dx + dy*dy);
}
int main(void)
{
Point p1, p2;
p1.setvalue();
p1.Getxy();
p2.setvalue();
p2.Getxy();
double d = Distance(p1, p2);
cout << "Distance is" << d << endl;
return 0;
}
————————————
這個代碼為什么刪掉建構式就可以運行,不刪不可以從鍵盤輸入嗎?還是有其他的問題?
uj5u.com熱心網友回復:
建構式要兩個引數,定義物件時卻沒有帶參uj5u.com熱心網友回復:
你重寫了建構式,就必須在實體化物件的時候顯示呼叫。不刪函式的話,就把你寫的建構式改為無參構造,函式內部不用代碼轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/195643.html
標籤:新手樂園
