自己定义了一个一元多项式的简单类,主要是用来实现一个简单的框架,另外就是验证编译器默认情况下会为我们的类添加那些构造函数。

 

 
  
  1. /* 
  2. * polynomial.h 
  3. * 该类定义一元多项式 
  4. * MAXDEGREE :定义该类最大可表示的次数 
  5. * MINCOEFF:定义绝对值最小的系数 
  6. * coefficient[  ] :存放一元多项式的系数 
  7. * currentDegree:存放当前对象的最高次数 
  8. * 构造函数:默认构造函数设置coefficient[]和currentDegree全为0 
  9. *                可以使用一个float数组和数组长度来构造一个一元多项式 
  10. * display( ) :用于显示对象的信息 
  11. * value(  ):计算给定x时的一元多项式的值 
  12. * operator +/-():计算多项式之间的加减 
  13. * power(  ) :用于计算给定x值的x的degree次幂的值 
  14. */ 
  15. class polynomial 
  16. public
  17.     polynomial(  ); 
  18.     polynomial( float coeff [ ],int size ); 
  19.     ~polynomial(  ){} 
  20.  
  21.     void  display(  ); 
  22.     double value(float xx); 
  23.  
  24.     friend polynomial operator +( const polynomial& one,const polynomial& two ); 
  25.     friend polynomial operator -( const polynomial& one,const polynomial& two ); 
  26.      
  27. private
  28.     double power( float xx,int degree ); 
  29.      
  30.          
  31. private
  32.     static const int MAXDEGREE=20; 
  33.     static const float MINCOEFF=0.000001; 
  34.     float coefficient[ MAXDEGREE+1 ]; 
  35.     int currentDegree; 
  36.  
  37. }; 

 

 
  
  1. /* 
  2.  *polynomial.cpp 
  3.  */ 
  4. #include "polynomial.h" 
  5. #include <iostream> 
  6. #include <cstdlib> 
  7.  
  8. polynomial::polynomial(  ) 
  9.     forint i=0;i<MAXDEGREE+1;i++ ) 
  10.         coefficient[ i ]=0.0; 
  11.     currentDegree=0; 
  12.      
  13.  
  14. polynomial::polynomial( float coeff [  ],int size ) 
  15.    if( size>MAXDEGREE ) 
  16.        { 
  17.            std::cout<<"the degree is bigger than the MAXDEGREE\n"
  18.            exit( -1 ); 
  19.             
  20.        } 
  21.    if( coeff==NULL ) 
  22.        { 
  23.            std::cout<<"you have input nothing\n"
  24.            exit( -1 ); 
  25.             
  26.        } 
  27.  
  28.    forint i=0;i<size;i++ ) 
  29.        coefficient[ i ]=coeff[ i ]; 
  30.  
  31.    currentDegree=size-1; 
  32.  
  33.  
  34. void polynomial::display(  ) 
  35.     { 
  36.         std::cout<<"The MAXDEGREE = "<<MAXDEGREE<<"\n"
  37.          
  38.         std::cout<<coefficient[ 0 ]; 
  39.         forint i=1;i<currentDegree+1;++i ) 
  40.             { 
  41.                 if( (coefficient[ i ] > (0.0-MINCOEFF)) && (coefficient[ i ] <MINCOEFF )) ; 
  42.                 else 
  43.                 { 
  44.                     if( coefficient[ i ]>0.0 ) 
  45.                         std::cout<<"+"<<coefficient[ i ]<<"x^"<<i; 
  46.                     else 
  47.                         std::cout<<coefficient[ i ]<<"x^"<<i; 
  48.                 } 
  49.             } 
  50.         std::cout<<std::endl; 
  51.     } 
  52.  
  53. double polynomial::power( float xx,int degree ) 
  54.     { 
  55.         double temp=1.0; 
  56.         while( degree-- ) 
  57.             temp*=xx; 
  58.         return temp; 
  59.          
  60.     } 
  61.  
  62. double polynomial::value( float xx ) 
  63.     { 
  64.         double sum=0.0; 
  65.         forint i=0;i<currentDegree+1;++i ) 
  66.             { 
  67.                 sum = sum+coefficient[ i ]*power( xx,i ); 
  68.                  
  69.             } 
  70.         return sum; 
  71.          
  72.     } 
  73.  
  74. polynomial operator +( const polynomial& one,const polynomial& two ) 
  75.     { 
  76.         polynomial temp; 
  77.          
  78.         int biggerDegree=( one.currentDegree > two.currentDegree ?one.currentDegree:two.currentDegree ); 
  79.         forint i=0;i<biggerDegree+1;++i ) 
  80.             { 
  81.                 temp.coefficient[ i ] = one.coefficient[ i ]+two.coefficient[ i ]; 
  82.                  
  83.             } 
  84.         temp.currentDegree=biggerDegree; 
  85.          
  86.         return temp; 
  87.          
  88.     } 
  89.  
  90. polynomial operator -( const polynomial& one,const polynomial& two ) 
  91.     { 
  92.         polynomial temp; 
  93.          
  94.         int biggerDegree=( one.currentDegree > two.currentDegree ?one.currentDegree:two.currentDegree ); 
  95.         forint i=0;i<biggerDegree+1;++i ) 
  96.             { 
  97.                 temp.coefficient[ i ] = one.coefficient[ i ]-two.coefficient[ i ]; 
  98.                  
  99.             } 
  100.         temp.currentDegree=biggerDegree; 
  101.          
  102.         return temp; 
  103.          
  104.     } 

 

 
  
  1. /* 
  2. * test.cpp 
  3. */ 
  4. #include <iostream> 
  5. #include "polynomial.h" 
  6.  
  7. int main(  ) 
  8.     { 
  9.         float one[  ]={1,2,3,4,5}; 
  10.         polynomial poly_one( one,5 ); 
  11.         poly_one.display(  ); 
  12.         std::cout<< poly_one.value( 1.0 )<<"\n"
  13.  
  14.         float two[  ]={1.2,2.5,-3.5,4.5,-5.5}; 
  15.         polynomial poly_two( two,5 ); 
  16.         poly_two.display(  ); 
  17.         std::cout<< poly_two.value( 1.0 )<<"\n"
  18.  
  19.         polynomial poly_three; 
  20.         poly_three=poly_two; 
  21.         poly_three.display(  ); 
  22.  
  23.         polynomial poly_four(poly_two); 
  24.         poly_four.display(  ); 
  25.          
  26.          
  27.         return 0; 
  28.          
  29.     } 
  30. /* 
  31. * test.cpp 
  32. */ 
  33. #include <iostream> 
  34. #include "polynomial.h" 
  35.  
  36. int main(  ) 
  37.     { 
  38.         float one[  ]={1,2,3,4,5}; 
  39.         polynomial poly_one( one,5 ); 
  40.         poly_one.display(  ); 
  41.         std::cout<< poly_one.value( 1.0 )<<"\n"
  42.  
  43.         float two[  ]={1.2,2.5,-3.5,4.5,-5.5}; 
  44.         polynomial poly_two( two,5 ); 
  45.         poly_two.display(  ); 
  46.         std::cout<< poly_two.value( 1.0 )<<"\n"
  47.  
  48.         polynomial poly_three; 
  49.         poly_three=poly_two; 
  50.         poly_three.display(  ); 
  51.  
  52.         polynomial poly_four(poly_two); 
  53.         poly_four.display(  ); 
  54.          
  55.          
  56.         return 0; 
  57.          
  58.     } 

从main中,poly_three和poly_four可知,如果我们没有声明复制构造函数和赋值构造函数的化,编译器会为我们自动添加。

另外,如果我们没有定义类的默认构造函数的话,编译器会为我们添加一个函数体为空的默认构造函数。

还有就是operator =()函数也会被自动添加,但是只满足同类对象之间相互赋值。