自己定义了一个一元多项式的简单类,主要是用来实现一个简单的框架,另外就是验证编译器默认情况下会为我们的类添加那些构造函数。
- /*
- * polynomial.h
- * 该类定义一元多项式
- * MAXDEGREE :定义该类最大可表示的次数
- * MINCOEFF:定义绝对值最小的系数
- * coefficient[ ] :存放一元多项式的系数
- * currentDegree:存放当前对象的最高次数
- * 构造函数:默认构造函数设置coefficient[]和currentDegree全为0
- * 可以使用一个float数组和数组长度来构造一个一元多项式
- * display( ) :用于显示对象的信息
- * value( ):计算给定x时的一元多项式的值
- * operator +/-():计算多项式之间的加减
- * power( ) :用于计算给定x值的x的degree次幂的值
- */
- class polynomial
- {
- public:
- polynomial( );
- polynomial( float coeff [ ],int size );
- ~polynomial( ){}
- void display( );
- double value(float xx);
- friend polynomial operator +( const polynomial& one,const polynomial& two );
- friend polynomial operator -( const polynomial& one,const polynomial& two );
- private:
- double power( float xx,int degree );
- private:
- static const int MAXDEGREE=20;
- static const float MINCOEFF=0.000001;
- float coefficient[ MAXDEGREE+1 ];
- int currentDegree;
- };
- /*
- *polynomial.cpp
- */
- #include "polynomial.h"
- #include <iostream>
- #include <cstdlib>
- polynomial::polynomial( )
- {
- for( int i=0;i<MAXDEGREE+1;i++ )
- coefficient[ i ]=0.0;
- currentDegree=0;
- }
- polynomial::polynomial( float coeff [ ],int size )
- {
- if( size>MAXDEGREE )
- {
- std::cout<<"the degree is bigger than the MAXDEGREE\n";
- exit( -1 );
- }
- if( coeff==NULL )
- {
- std::cout<<"you have input nothing\n";
- exit( -1 );
- }
- for( int i=0;i<size;i++ )
- coefficient[ i ]=coeff[ i ];
- currentDegree=size-1;
- }
- void polynomial::display( )
- {
- std::cout<<"The MAXDEGREE = "<<MAXDEGREE<<"\n";
- std::cout<<coefficient[ 0 ];
- for( int i=1;i<currentDegree+1;++i )
- {
- if( (coefficient[ i ] > (0.0-MINCOEFF)) && (coefficient[ i ] <MINCOEFF )) ;
- else
- {
- if( coefficient[ i ]>0.0 )
- std::cout<<"+"<<coefficient[ i ]<<"x^"<<i;
- else
- std::cout<<coefficient[ i ]<<"x^"<<i;
- }
- }
- std::cout<<std::endl;
- }
- double polynomial::power( float xx,int degree )
- {
- double temp=1.0;
- while( degree-- )
- temp*=xx;
- return temp;
- }
- double polynomial::value( float xx )
- {
- double sum=0.0;
- for( int i=0;i<currentDegree+1;++i )
- {
- sum = sum+coefficient[ i ]*power( xx,i );
- }
- return sum;
- }
- polynomial operator +( const polynomial& one,const polynomial& two )
- {
- polynomial temp;
- int biggerDegree=( one.currentDegree > two.currentDegree ?one.currentDegree:two.currentDegree );
- for( int i=0;i<biggerDegree+1;++i )
- {
- temp.coefficient[ i ] = one.coefficient[ i ]+two.coefficient[ i ];
- }
- temp.currentDegree=biggerDegree;
- return temp;
- }
- polynomial operator -( const polynomial& one,const polynomial& two )
- {
- polynomial temp;
- int biggerDegree=( one.currentDegree > two.currentDegree ?one.currentDegree:two.currentDegree );
- for( int i=0;i<biggerDegree+1;++i )
- {
- temp.coefficient[ i ] = one.coefficient[ i ]-two.coefficient[ i ];
- }
- temp.currentDegree=biggerDegree;
- return temp;
- }
- /*
- * test.cpp
- */
- #include <iostream>
- #include "polynomial.h"
- int main( )
- {
- float one[ ]={1,2,3,4,5};
- polynomial poly_one( one,5 );
- poly_one.display( );
- std::cout<< poly_one.value( 1.0 )<<"\n";
- float two[ ]={1.2,2.5,-3.5,4.5,-5.5};
- polynomial poly_two( two,5 );
- poly_two.display( );
- std::cout<< poly_two.value( 1.0 )<<"\n";
- polynomial poly_three;
- poly_three=poly_two;
- poly_three.display( );
- polynomial poly_four(poly_two);
- poly_four.display( );
- return 0;
- }
- /*
- * test.cpp
- */
- #include <iostream>
- #include "polynomial.h"
- int main( )
- {
- float one[ ]={1,2,3,4,5};
- polynomial poly_one( one,5 );
- poly_one.display( );
- std::cout<< poly_one.value( 1.0 )<<"\n";
- float two[ ]={1.2,2.5,-3.5,4.5,-5.5};
- polynomial poly_two( two,5 );
- poly_two.display( );
- std::cout<< poly_two.value( 1.0 )<<"\n";
- polynomial poly_three;
- poly_three=poly_two;
- poly_three.display( );
- polynomial poly_four(poly_two);
- poly_four.display( );
- return 0;
- }
从main中,poly_three和poly_four可知,如果我们没有声明复制构造函数和赋值构造函数的化,编译器会为我们自动添加。
另外,如果我们没有定义类的默认构造函数的话,编译器会为我们添加一个函数体为空的默认构造函数。
还有就是operator =()函数也会被自动添加,但是只满足同类对象之间相互赋值。