Sunday, July 12, 2009

Polynomial addition using operator overloading(+) inC plus plus?

Hi could u pls type me a prog for polynomial addition in C++???


using operator overloading(+)..Thanks...

Polynomial addition using operator overloading(+) inC plus plus?
I would like to help





BUT





BETTER DO YOUR ASSIGNMENT YOURSELF
Reply:www.cprogramming.com
Reply:create two arrays for coefficients of each polynomial.


use for for a loop of coefficients such as


n=4;


for(int i=0;i%26lt;n;i++){


a[i]=a[i]+b[i];


printf("%d. coefficient = %d",i,a[i]);


}








N.B.:


use cout command instead of printf
Reply:#include%26lt;iostream.h%26gt;


class poly


{


int a,b,c;


public:


void getdata()


{


cout%26lt;%26lt;"\n Enter the square term coefficient :";


cin%26gt;%26gt;a;


cout%26lt;%26lt;"\n Enter the linear (x) term coefficient :";


cin%26gt;%26gt;b;


cout%26lt;%26lt;"\n Enter the constant term:";


cin%26gt;%26gt;c;


}


void putdata()


{


cout%26lt;%26lt;a%26lt;%26lt;"x^2 +"%26lt;%26lt;b%26lt;%26lt;"x +"%26lt;%26lt;c;


}


poly operator +(poly ob2)


{


poly res;


res.a =ob2.a+a;


res.b=ob2.b+b;


return(res);


}


};


void main()


{


poly ob1,ob2,ob3;


cout%26lt;%26lt;"\n Enter first polynomial :";


ob1.getdata();


cout%26lt;%26lt;"\n ENter the second polynomial:";


ob2.getdata();


ob3=ob1+ob2;


ob3.putdata();


}

carnation

No comments:

Post a Comment