# C++ program code for polynomial addition using binary operator overloading

Even if the logic involved for polynomial additionin C++ is same as that of **C**, the main advantage with C++ is that both the polynomials can be added directly with the concept of operator overloading. In the C program that I had written first, two arrays were used to represent the two polynomials and codes common to both had to be written twice. But object oriented** **programming eliminates this redundancy by encapsulating all the attributes of a polynomial within a class. This program describes the entire procedure using the concept of operator overloading.

Define a class ‘poly’ with n, deg[] and coeff[] as the data members that represent the no. of terms and degree and coefficient of each term of the polynomial. If functions getdata() and putdata() declared can be used for reading and printing the polynomials, the friend function putdatasum(poly) prints the resultant polynomial sum.

The operator overloaded member function ‘poly operator +(poly)’ is where the entire logic of binary polynomial addition comes under. Let’s discuss each function in detail.

## 1. void getdata()

This function reads each attribute of a polynomial such as no. of terms, degree and coefficient of each term. As it is a member function, it can directly access these attributes when invoked using an object of its class type.

## 2. void putdata()

Printing a polynomial in its normal format ax^{2}+bx+c is done by this member function. By checking each condition for the degree and coefficient, the polynomial gets printed in its usual form.

## 3. void putdatasum(poly)

The resultant polynomial C is printed by using this friend function that takes the argument as the object ‘C’.

## 4. poly operator+(poly)

This operator overloaded member function adds two polynomials and stores the sum in another object of class ‘poly’ and returns it. Operator overloading concept allows us to perform computing operations with object data types like we do with basic data types. Here, the statement C=A+B same as A.operator+(B) invokes this function that has another object ‘d’ declared of class type ‘poly’. With the same logic used for performing polynomial addition using a binary operator in C, summation is performed here too. The polynomial sum obtained here is then printed by invoking the friend function putdatasum(poly).

## Output

Object Oriented Programming shortens the length of many programs written in traditional C programming language. If it provides saving of resources, faster execution and accurate results on one side, code reusability is another important aspect that enhances the scope of developing user-friendly programs with much ease.

## C++ program code

#include<iostream.h> #include<conio.h> class poly { int n; int deg[20]; int coeff[20]; public: void getdata(); void putdata(); friend void putdatasum(poly); poly operator +(poly); }; void poly::getdata() { cout<<"\n\tEnter the no. of terms:"; cin>>n; cout<<"\n\tEnter the degrees and coefficients:"; for(int i=0;i<n;i++) { cin>>deg[i]; cin>>coeff[i]; } } void poly::putdata() { for(int i=0;i<n;i++) { if (i==0) cout<<coeff[i]<<"x^"<<deg[i]; else if(deg[i]==0) cout<<"+"<<coeff[i]; else cout<<"+"<<coeff[i]<<"x^"<<deg[i]; } } void putdatasum(poly c) { for(int i=0;i<c.n;i++) { if (i==0) cout<<c.coeff[i]<<"x^"<<c.deg[i]; else cout<<"+"<<c.coeff[i]<<"x^"<<c.deg[i]; } } poly poly::operator +(poly c) { poly d; d.n=0; int i=0; int j=0; int k=0; while(n>0 && c.n>0) { if (deg[i]==c.deg[j]) { d.coeff[k]=coeff[i]+c.coeff[j]; d.deg[k]=deg[j]; d.n++; n--; c.n--; i++; j++; k++; } else if (deg[i]>c.deg[j]) { d.coeff[k]=coeff[i]; d.deg[k]=deg[i]; d.n++; n--; i++; k++; } else { d.coeff[k]=c.coeff[j]; d.deg[k]=c.deg[j]; d.n++; c.n--; j++; k++; } } while (n>0) { d.coeff[k]=coeff[i]; d.deg[k]=deg[i]; d.n++; n--; i++; k++; } while (c.n>0) { d.coeff[k]=c.coeff[j]; d.deg[k]=c.deg[j]; d.n++; c.n--; j++; k++; } return(d); } int main() { clrscr(); poly A,B,C; cout<<"\n\tPolynomial Addition\n"; cout<<"\t====================="; cout<<"\n\n\tEnter the details of 1st polynomial:"; A.getdata(); cout<<"\n\n\tEnter the details of 2nd polynomial:"; B.getdata(); cout<<"\n\n\t1st polynomial="; A.putdata(); cout<<"\n\n\t2nd polynomial="; B.putdata(); cout<<"\n\n\tSum="; C=A+B; putdatasum(C); getch(); return 0; }

## Which program code for polynomial addition is easy for you?

- C program code for addition of two polynomials using...

A polynomial is an expression of finite length constructed from variables, constants and non-negative integer exponents. C program code for polynomial addition is explained in detail here. - The concept of operator overloading in C++

Operator overloading concept gives a special meaning to an operator such as ‘+’ or ‘-‘. Addition or subtraction cannot be directly performed with objects as we do with basic data types. Like overloading functions, operators also can be overloaded for - About the object-oriented programming language C++

If the volume of data involved in a problem is more, it becomes difficult to program using conventional programming. In such case, data is organized to form a class (like structure in C) of user-defined type and programming is performed on that data - Understanding C++ Programming Language

C++ is one of the Object Oriented Languages, other examples of these languages include, jaza, visual basic and many others Below are basic definitions you need to know as you start your journey into the programming world. Here we go A class -...

## Comments

No comments yet.