Point類用于描述平面上的一個點, Distance類用于描述兩點之間的距離,其資料成員包括兩個Point類物件,要求計算兩點之間距離。
參考程式:
#include<iostream>
#include <cmath>
using namespace std;
class Point
{
int x,y;
public:
Set_xy(int a=0, int b=0) {x=a; y=b;}
int xcord(){ return x;}
int ycord(){ return y;}
};
class Distance
{
Point p1,p2;
double dist;
public:
Distance(Point q1,Point q2)
{
p1=q1;
p2=q2;
double x=double(p1.xcord()-p2.xcord());
double y=double(p1.ycord()-p2.ycord());
dist=sqrt(x*x+y*y);
}
double getdist() {return dist; }
};
int main()
{
Point p,q;
p.Set_xy();
q.Set_xy(1,1);
Distance dis(p,q);
cout<<"The distance is: "<<dis.getdist()<<endl;</p>
return 0;
}
以上面程式為基礎,將Set_xy函式的功能用建構式實作,改寫程式并上機運行程式。
uj5u.com熱心網友回復:
就是自己實作以下Point的建構式,可能還要實作一些別的比如拷貝構造等。uj5u.com熱心網友回復:
大佬可以幫忙寫個程式么
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63026.html
標籤:C++ 語言
上一篇:Simulink永磁同步電機搭塊
