Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

1個class內含2個建構子.其中一個有參數(急.20點)

設計一個class內含2個constructor.其中一個有參數一個無參數

以下是我的程式碼

可以幫我除錯嗎?謝謝

#include<iostream>

using namespace std;

class Avatar

{

public:

int x;

int y;

Avatar();

Avatar(int,int);

~Avatar();

};

Avatar:Avatar()

{

cout<<\"請輸入x,y\"<<endl;

cin>>x;

cin>>y;

cout<<\"(x,y)=(\"x<<\",\"<<y<<\")\" <<endl;

}

Avatar::Avatar:int x(0),int y(0)

{

cout<<\"建構開始\"<<endl;

cout<<\"(x,y)=(\"x<<\",\"<<y<<\")\" <<endl;

}

~Avatar::Avatar;

{

cout<<\"解構開始\"<<endl;

int main()

{

Avatar alpha;

Avatar beta(0,0);

system(\"pause\");

return 0;

}

3 Answers

Rating
  • 1 decade ago
    Best Answer

    #include<iostream>

    using namespace std;

    class Avatar

    {

    public:

    int x;

    int y;

    Avatar();

    Avatar(int,int);

    ~Avatar();

    }; //上面的部分應該都沒有問題

    Avatar::Avatar() //少了一個冒號

    {

    cout<<"請輸入x,y"<<endl;

    cin>>x;

    cin>>y;

    cout<<"(x,y)=(" << x << "," << y << ")" <<endl; //少了一個<< 我加在那邊你自己看

    }

    Avatar::Avatar(int a, int b):x(a),y(b) //初始化參數正確的用法是這樣

    {

    cout<<"建構開始"<<endl;

    cout<<"(x,y)=(" << x << "," << y << ")" <<endl; //一樣少了一個<<

    }

    ~Avatar::Avatar() //不能加分號 還有要有括號

    {

    cout<<"解構開始"<<endl;

    } //要加上}

    int main()

    {

    Avatar alpha;

    Avatar beta(0,0);

    system("pause");

    return 0;

    }

    改最多的是在有參數的建構子的定義那邊....

    Source(s): 自己
  • Lv 7
    1 decade ago

    ☆〃Computer Amateur♂,好像cout的部分忘了改. :)

  • 1 decade ago

    Avatar::Avatar() //少一個:,應該是::{cout<<"請輸入x,y"<<endl;cin>>x;cin>>y;cout<<"(x,y)=("x<<","<<y<<")" <<endl; } Avatar::Avatar(int x,int y) //這行寫錯 我直接幫你修正{cout<<"建構開始"<<endl; cout<<"(x,y)=("x<<","<<y<<")" <<endl;}~Avatar::Avatar;{cout<<"解構開始"<<endl;} //你少了}其他的大致上看起來沒問題,你有引數建構子好像把你亂改不過你那樣寫我實在看不懂是什麼意思@@

    2006-04-18 19:28:41 補充:

    龍指的是這裡嗎?cout<<"(x,y)=(" x<< "," << y << ")" << endl;正確是cout<<"(x,y)=(" << x<< "," << y << ")" << endl;我現在神志不清楚~等哪天頭腦好一點我再重頭看一遍

Still have questions? Get your answers by asking now.