Yahoo奇摩知識+將於 2021 年 5 月 4 日 (美國東部時間) 終止服務。自 2021 年 4 月 20 日 (美國東部時間) 起,Yahoo奇摩知識+服務將會轉為唯讀模式。其他Yahoo奇摩產品與服務或您的Yahoo奇摩帳號都不會受影響。如需關於Yahoo奇摩知識+ 停止服務以及下載您個人資料的資訊,請參閱說明網頁。

建立類別Point,並修正main() 之程式碼,以計算a(2, 4)與b(7, 9)兩點間之距離。?

Point a(2, 4), b(7, 9);

float xa = a.x, ya = a.y, xb = b.x, yb = b.y; (※本行有誤)

float dist = sqrt(pow(xa – xb, 2) + pow(ya – yb, 2));

cout << "The distance between a and b is " << dist << endl;

2 個解答

評分
  • 5 年前

    非常感謝,請幫我再解以下三題,謝謝。

    1.請修改與補充下列的程式碼,建立一Calculate類別。並註解程式功能。

    class Calculate{private: int x; PUBLIC: Calculate(int n) {x=n} Calculate sum(Calculate cal) {x += cal.x; return *this} void show(Calculate a,b){cout << "a = " << a.x << "\nb = " << b.x << "\na + b = " << this->x << endl;}};

    int main(){ Calculate a(100), b(100), c(0);b.x=200 c=a.sum(b);

    c.show(a,b); system("PAUSE"); return 0;}

    2.請參考第11.3.2節之例題,建立一類別Record,內僅有一私有資料int num,並設計相關的函式成員set、show、out。然後並修改main()。

    ※將字串"Nothing!"換成數值0,"This is a string."換成數值2015。

    第11.3.2節之例題:

    1 // Demo of pointer as data member

    2 #include <iostream>

    3 using namespace std;

    4

    5 class Astring {

    6 public:

    7 void set() { str = "Nothing!"; } // 設定字串之函式

    8 void set(char * p) { str = p; } // 多載

    9 void show() { cout << str << endl; } // 印出字串之函式

    10 char *out() { return str; } // 輸出字串之函式

    11 private:

    12 char *str; // 字串指標作為私有資料成員

    13 };

    14

    15 int main()

    16 {

    17 Astring a, b;

    18 a.set("This is a string."); // 設定字串 a

    19 b.set(); // 字串 b 為 Nothing!

    20 a.show();

    21 cout << b.out() << endl;

    22 return 0;

    24 }

    3.請參考第11.6節之例題,建立一類別Record,內僅有一私有資料int num,並設計相關的建構式及函式成員show。然後並修改main()。

    ※將字串"This is a string."換成數值2015。

    第11.6節之例題:

    1 // Demo of copy constructor

    2 #include <iostream>

    3 using namespace std;

    4

    5 class Astring {

    6 public:

    7 Astring(char *); // 建構式原型

    8 Astring(int); // 多載

    9 Astring(const Astring&); // 複製建構式原型

    10 void show() { cout << str << endl; } // 印出字串之函式

    11 private:

    12 char *str; // 字串指標作為私有資料成員

    13 int len; // 字串長度作為私有資料成員

    14 };

    15

    16 Astring::Astring(char *s) // 建構式定義

    17 {

    18 int i = 0;

    19 while(*(s + i) != '\0') i++;

    20 len = i;

    21 str = new char[len];

    22 str = s;

    23 }

    24

    25 Astring::Astring(int n = 10) // 建構式多載定義

    26 {

    27 if(n <= 0) n = 10;

    28 len = n;

    29 str = new char[len];

    30 for(int i = 0; i < n; i++) str[i] = ' ';

    31 str[n] = 0;

    32 }

    33

    34 Astring::Astring(const Astring& s) // 複製建構式定義

    35 {

    36 len = s.len;

    37 str = new char[len];

    38 for(int i = 0; i < len; i++) str[i] = s.str[i];

    39 str[len] = 0;

    40 }

    41

    42 int main()

    43 {

    44 Astring a("This is a string."); // 設定字串 a

    45 Astring b = a; // 字串 b 複製自 a

    46 a.show();

    47 b.show();

    48 return 0;

    49 }

  • ?
    Lv 7
    5 年前

    #include<math.h>

    #include<iostream>

    #include<sstream>

    using namespace std;

    template<class T>

    class P{

    public:

    T x,y;

    P():x((T)0),y((T)0){}

    P(T x0,T y0):x(x0),y(y0){}

    ~P(){}

    T dist(P<T> &p){T x1=(x-p.x),y1=(y-p.y);return (T)sqrt(x1*x1+y1*y1);}

    T GetX(){return x;}

    T GetY(){return y;}

    T SetX(T xx){return x=xx;}

    T SetY(T yy){return y=yy;}

    void mv(P<T>p){x+=p.x;y+=p.y;}

    string prt(){ostringstream os; os<<"("<<x<<","<<y<<")"; return os.str();}

    };

    int main(int gc, char *gv[]) {

    P<double> p0(2.0,4.0),p1(7.0,9.0);

    cout<<"the distance between "<<p0.prt()<<" and "<<p1.prt()<<" is "<< p0.dist(p1)<<endl;

    return 0;

    }

    test$ g++ point.cpp -o point -Wall -O3

    test$ ./point

    the distance between (2,4) and (7,9) is 7.07107

    test$ vim point.cpp

    test$

還有問題?馬上發問,尋求解答。