Anonymous

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

#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;

}

Rating

#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