定义一个人民币类money,类中数据成员为元,角,分。用成员函数重载“++”运算符,实现人民币对象的加一分
钱运算。在主函数中定义人民币对象m1=10元8角5分以及对象m2,m3。对m1作前置“++”并赋给m2,对m1作后置“++”并赋给m3。显示m1,m2,m3的结果,谢谢,...
钱运算。在主函数中定义人民币对象m1=10元8角5分以及对象m2,m3。对m1作前置“++”并赋给m2,对m1作后置“++”并赋给m3。显示m1,m2,m3的结果,谢谢,在线等,麻烦了
展开
1个回答
展开全部
// 1.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
class money //定义money类
{
public: //数据成员为公用的
int yuan;
int jiao;
int fen;
money( int y = 0, int j = 0, int f = 0 ) ;
money operator ++ ( ) ;
money operator ++ ( int ) ;
void display () { cout << yuan <<" yuan " << jiao <<" jiao " << fen << " fen " << endl ; } ;
};
money :: money ( int y, int j, int f ): yuan (y) , jiao ( j ) , fen ( f ) {}
money money :: operator ++ ()
{
if( ++fen >= 10 )
{
fen -= 10 ;
++jiao;
}
return * this ;
}
money money :: operator ++ ( int )
{
money temp ( *this ) ;
fen ++;
if (fen >= 10 )
{
fen -= 10 ;
++jiao;
}
return temp ;
}
int main( )
{
money m1 (10 , 8 , 5 ), m2 , m3;
cout<< " m1 : " ;
m1.display ( ) ;
m2 = ++m1 ;
m2.display( ) ;
m3 = m1++ ;
m3.display( ) ;
return 0;
}
//
#include <iostream>
using namespace std;
class money //定义money类
{
public: //数据成员为公用的
int yuan;
int jiao;
int fen;
money( int y = 0, int j = 0, int f = 0 ) ;
money operator ++ ( ) ;
money operator ++ ( int ) ;
void display () { cout << yuan <<" yuan " << jiao <<" jiao " << fen << " fen " << endl ; } ;
};
money :: money ( int y, int j, int f ): yuan (y) , jiao ( j ) , fen ( f ) {}
money money :: operator ++ ()
{
if( ++fen >= 10 )
{
fen -= 10 ;
++jiao;
}
return * this ;
}
money money :: operator ++ ( int )
{
money temp ( *this ) ;
fen ++;
if (fen >= 10 )
{
fen -= 10 ;
++jiao;
}
return temp ;
}
int main( )
{
money m1 (10 , 8 , 5 ), m2 , m3;
cout<< " m1 : " ;
m1.display ( ) ;
m2 = ++m1 ;
m2.display( ) ;
m3 = m1++ ;
m3.display( ) ;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询