# C++ Program Code for Polynomial Addition Using Binary Operator Overloading

*Radhika has the degree of master in computer applications. She has worked as a faculty with Aptech Computer Education Center for sometime.*

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

- 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 - 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. - 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

**© 2013 Radhika Sreekanth**