c++ - 成员函数到友元函数的转换

标签 c++ class oop object uml

我有这个任务将所有成员函数转换为友元函数并添加一个复制构造函数。我真的不明白这周给定的讲座。请任何人在这里转换函数的任意两个(一个访问器函数和另一个来自加法/乘法 block )。只有一个功能,我会做剩下的。提前致谢

#include <iostream>
#include <string.h>

using namespace std;
class fraction
{
 long num;
 long den;

 public: 
fraction(long,long);
fraction();
~fraction();
void  setNum ( long ); 
void  setDen ( long );
long  getNum ( void );
long    getDen ( void );  

void  print  (void);

void add  (fraction, fraction);
void sub  (fraction, fraction);
void mult (fraction, fraction);
void div  (fraction, fraction);
void inc  (fraction);

}  ;   // end of class fraction

long gcd (long x, long y); 

fraction::fraction(long l_num, long l_den)
{

    num = l_num;
    den = l_den;
}
fraction::fraction()
{

}
fraction::~fraction()
{


}

void fraction::setNum (long  l_num )
 {
  num = l_num ;
 }


void fraction::setDen (long l_den )
 {
  den = l_den ;
 }

long fraction::getDen ( )
 {
  return den ;
 }

long fraction::getNum ( )
 {
  return num ;
 }          

void fraction:: print (void)
{
   cout<<num/gcd(num,den)<<"/"<<den/gcd(num,den) <<endl;
} 

void fraction::add (fraction f1, fraction f2)
{ 
  num = (f1.getNum ( ) * f2.getDen ( )) + ( f1.getDen ( ) * f2.getNum ( ) );
  den = (f1.getDen ( ) * f2.getDen ( ));
}  

void fraction::sub (fraction f1, fraction f2)
{ 
  num = (f1.getNum ( ) * f2.getDen ( )) - ( f1.getDen ( ) * f2.getNum ( ) );
  den = (f1.getDen ( ) * f2.getDen ( ));
}  

 void fraction::mult (fraction f1, fraction f2)
{ 
  num = (f1.getNum ( ) * f2.getNum ( )); 
  den = (f1.getDen ( ) * f2.getDen ( ));

}  

void fraction::div(fraction f1, fraction f2)
{ 

 num = (f1.getNum ( ) * f2.getDen ( )); 
 den = (f1.getDen ( ) * f2.getNum ( ));
 }

void fraction::inc (fraction f1)
{ 
  num = (f1.getNum ( )) + ( f1.getDen ( ) );
 den = (f1.getDen ( ) );

 }  


long gcd (long x, long y)
{
 return (x == 0) ? y :  gcd (y%x, x); 
} 

int main ( )
{
 //  define seven instances of the class fraction
 fraction f1(1L,2L),f2(3L,4L),f3, f4,f5,f6, f7;

 //set values for the numerator and denominator to f1 and print them
 //f1.setDen( 2L);
 //f1.setNum( 0L);
 f1.print();

 //set values for the numerator and denominator to f2 and print them
 //f2.setDen( 4L);
 //f2.setNum( 3L); 
 f2.print();

 f3.add( f1, f2);
 f3.print();

 f4.sub( f1, f2);
 f4.print();

 f5.mult( f1, f2);
 f5.print();

 f6.div( f1, f2);
 f6.print();

 f7.inc(f1);
 f7.print();

 return 0;

 }

最佳答案

复制构造函数看起来像

fraction( const fraction & );

并定义为

fraction::fraction( const fraction &rhs ) : num( rhs.num ), den( rhs.den )
{
}

我会将算术函数声明为返回分数类型的对象。

例如

friend const fraction add( const fraction &, const fraction & );

并将其定义为

const fraction add( const fraction &lhs, const fraction &rhs )
{
    return fraction( lhs,getNum() * rhs.getDen() + rhs.getNum() * lhs.getDen(), 
                     lhs.getDen() * rhs.getDen() );
}

考虑到函数 getNum 和 getDen 必须声明为

long  getNum() const;
long  getDen() const;  

函数 inc 可以声明为

friend const fraction inc( fraction & ); 

或作为

friend fraction & inc( fraction & ); 

取决于它模拟的是后增量还是前增量操作。

关于c++ - 成员函数到友元函数的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25966590/

相关文章:

c++ - 如何创建一个类对象且具有编译时大小的数组?

PHP OO - 我应该如何处理多个类中的软错误?

c++ - 什么时候必须使用友元函数而不是成员函数?

python - 动态改变 __slots__

c++ - 类声明大括号后的分号

javascript - 较大对象中的 this.objectName 在执行期间变为未定义

c# - 绘制具有多个孔的多边形?

c++ - 向上转换和向下转换对象时出现无效的初始化错误

c++ - WM_TIMER 在 ATL ActiveX 控件中突然停止

c++ - 使用 boost::shared_ptr 时有哪些潜在危险?