1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

class polynomial with operator overloading

Discussion in 'C++' started by imported_nahid, Nov 21, 2010.

  1. imported_nahid

    imported_nahid New Member

    Joined:
    Nov 21, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hi ,
    I'm new in C++ programming and I need help.
    I wanna write a class named polynomial that can be used like this :

    Code:
    int main()
    {
    	vector<double> ac;
    	ac.push_back(10);
    	ac.push_back(3);
    	const Poly a(ac); // a contains 3x + 10
    
    	vector<double> bc;
    	bc.push_back(1);
    	bc.push_back(0);
    	bc.push_back(4);
    	const Poly b(bc); // b contains 4x^2 + 1
    
    	Poly c = a + b; // c contains 4x^2 + 3x + 11
    	c += b; // c contains 8x^2 + 3x + 12
    
    	Poly d = c * 2; // d contains 16x^2 + 6x + 24
    	cout << d << endl; // prints 16x^2+6x+24 in the output
    
    	d = -1 * b; // d contains -4x^2 - 1
    	cout << d << endl; // prints -4x^2-1 in the output
    
    	cout << d(2) << endl; // evaluates d with x=2, the result is -17       // same as caling d.operator()(2)
    
    	cout << d[0] << endl; // prints -1 which is the coefficient of x^0
    	cout << d[1] << endl; // prints 0 which is the coefficient of x^1
    	cout << d[2] << endl; // prints -4 which is the coefficient of x^2
    	cout << d[3] << endl; // prints 0 which is the coefficient of x^3
    
    	d[1] = 7; // d now contains -4x^2 + 7x -1
    	d[4] = 1; // d now contains x^4 - 4x^2 + 7x -1
    	cout << b[2] << endl; // prints 4
    
    	return 0;
    }
    
    and I don't understand how to write inside the member functions.
    
    //poly.h
    
    class Poly
    {
    public:
    	Poly( vector<double> a ):p(a){}
    	
    	Poly& operator+= ( const Poly& c );              //this+=c
    	Poly operator* ();
    	Poly operator+ ( Poly& a ) const;
    	double operator()( const double b ) const;
    	double operator[]( const double d ) const;
    
    	friend Poly operator<< ( ostream& out , Poly& q );
    private:
    	vector<double> p ;
    };
    
    I think my class should be something like this

    please help to complete my class.
    thanks
     
  2. imported_nahid

    imported_nahid New Member

    Joined:
    Nov 21, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I some how completed my class and I need help to make it the best

    plz help me.

    " code has been attached "
     
  3. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    What do you mean by best?
     
  4. imported_nahid

    imported_nahid New Member

    Joined:
    Nov 21, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    It isn't important
    It's just my slogan
    I just wanna make my program run
    just help me

    thanks;
     
  5. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    So consider that is the best method.
     
  6. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
  7. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
  8. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    Great!! now u know this...
     
  9. imported_nahid

    imported_nahid New Member

    Joined:
    Nov 21, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    excuse me!
    are you having fun here or really really wanting to help persons like me solve their problems :-?
     
  10. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    This is one of the main reason for this
     
  11. imported_nahid

    imported_nahid New Member

    Joined:
    Nov 21, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I don't understand!!!!!!
    every one here just had answered the questions for competition and now they won't answer?????(after this )
     
  12. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    That is not true from my end because I am member of this forum for quite sometime and yes some people used the competition in the wrong way and I think I had to reply back and cannot sit back see someone bashing on me.

    Anyways your program is quite good.
     
  13. imported_nahid

    imported_nahid New Member

    Joined:
    Nov 21, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    ok never mind
    I made my prject run myself
    with my friend's help at university.
    and I won't come here for solving my problems anymore.
    bye
     

Share This Page