#include<iostream>using namespace std;class CMoney{private:int m_nYuan,m_nJiao,m_nFen;public:CMoney(int Yuan,int Jiao,int Fen){m_nYuan=Yuan;m_nJiao=Jiao;m_nFen =Fen ;}CMoney(){m_nYuan=0;m_nJiao=0;m_nFen =0;}void SetValue(int m_nYuan,int m_nJiao,int m_nFen){if(m_nFen>9){m_nJiao+=m_nFen/10;m_nFen=m_nFen%10;}if(m_nJiao>9){m_nYuan+=m_nJiao/10;m_nJiao=m_nJiao%10;}}int GetYuan(){return m_nYuan;}int GetJiao(){return m_nJiao;}int GetFen (){return m_nFen; }void Show(){cout<<GetYuan()<<'\t'<<GetJiao()<<'\t'<<GetFen()<<endl;}};int main(){cout<<"無參構(gòu)造函數(shù)被調(diào)用"<<endl;CMoney M1;M1.Show();int yuan,jiao,fen;cout<<"請(qǐng)輸入元、角、分:";cin>>yuan>>jiao>>fen;CMoney M2(yuan,jiao,fen);M2.SetValue(yuan,jiao,fen);M2.Show();return 0;}
2 回答

HUWWW
TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
局部變量與類的成員變量同名了。換個(gè)名字:
void SetValue( int Yuan, int Jiao, int Fen){ if (Fen > 9){ Jiao += Fen/10; Fen %= 10; } if (Jiao>9){ Yuan += Jiao/10; Jiao %= 10; } * this = CMoney(Yuan, Jiao, Fen); } |

白衣非少年
TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
#include<iostream> using namespace std; class CMoney { private : int m_nYuan,m_nJiao,m_nFen; public : CMoney( int Yuan, int Jiao, int Fen) { m_nYuan=Yuan; m_nJiao=Jiao; m_nFen =Fen ; } CMoney() { m_nYuan=0; m_nJiao=0; m_nFen =0; } void SetValue() { if (m_nFen>9) { m_nJiao+=m_nFen/10; m_nFen=m_nFen%10; } if (m_nJiao>9) { m_nYuan+=m_nJiao/10; m_nJiao=m_nJiao%10; } } int GetYuan(){ return m_nYuan;} int GetJiao(){ return m_nJiao;} int GetFen (){ return m_nFen; } void Show() { cout<<GetYuan()<< '\t' <<GetJiao()<< '\t' <<GetFen()<<endl; } }; int main() { cout<< "無參構(gòu)造函數(shù)被調(diào)用" <<endl; CMoney M1; M1.Show(); int yuan,jiao,fen; cout<< "請(qǐng)輸入元、角、分:" ; cin>>yuan>>jiao>>fen; CMoney M2(yuan,jiao,fen); M2.SetValue(); M2.Show(); return 0; } |
SetValue()函數(shù)沒必要再設(shè)置參數(shù)了
- 2 回答
- 0 關(guān)注
- 195 瀏覽
添加回答
舉報(bào)
0/150
提交
取消