Docsity

OOPs - Assignment operation overloading - Develop class Polynomial., Assignments of Object Oriented Programming

Toggle sidebar
bg1
Q1.Develop classPolynomial. The internal representation of a Polynomial is an
array of terms. Each term contains acoefficientand an exponent. The term
2x4has the coefficient 2 and the exponent 4. Theindex numberof an array
represents theexponent.
Develop a complete class containing
properconstructoranddestructorfunctions as well assetandgetfunctions.
The class should also provide the following overloaded operator capabilities:
oOverload the addition operator (+) to add two Polynomials.
oOverload the subtraction operator (-) to subtract two Polynomials.
oOverload the equal operator (==) to compare two Polynomials.
oOverload the assignment operator to assign one Polynomial to another.
oOverload the multiplication operator (*) to multiply two Polynomials.
oOverload the addition assignment operator (+=), subtraction assignment
operator (-=), and multiplication assignment operator (*=).
#include<iostream>
#include<conio.h>
#include<string.h>
#include<math.h>
usingnamespacestd;
#defineSIZE 10
classpolynomial
{
private:
intco[SIZE] ;
public:
polynomial(){
for(inti=0 ; i<SIZE ; i++)
co[i]=0;
}
voidgetvalues (){
for(inti=4 ; i>=0 ; i--){
cout <<"Enter value of cofficient
x^"<<i<<": ";
cin>>co [i] ;
}
}
polynomialoperator+ (polynomial);
polynomialoperator- (polynomial);
polynomialoperator* (polynomial);
polynomialoperator+=(polynomial);
polynomialoperator-=(polynomial);
polynomialoperator*=(polynomial);
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download OOPs - Assignment operation overloading - Develop class Polynomial. and more Assignments Object Oriented Programming in PDF only on Docsity!

Q1. Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term 2x^4 has the coefficient 2 and the exponent 4. The index number of an array represents the exponent.  Develop a complete class containing proper constructor and destructor functions as well as set and get functions.  The class should also provide the following overloaded operator capabilities: o Overload the addition operator ( + ) to add two Polynomials. o Overload the subtraction operator ( - ) to subtract two Polynomials. o Overload the equal operator ( == ) to compare two Polynomials. o Overload the assignment operator to assign one Polynomial to another. o Overload the multiplication operator ( ***** ) to multiply two Polynomials. o Overload the addition assignment operator ( += ), subtraction assignment operator ( -= ), and multiplication assignment operator ( *= ). #include #include<conio.h> #include<string.h> #include<math.h> using namespace std; #define SIZE 10 class polynomial { private: int co[SIZE] ; public: polynomial(){ for (int i=0 ; i<SIZE ; i++) co[i]=0; } void getvalues (){ for(int i=4 ; i>=0 ; i--){ cout << "Enter value of cofficient x^" << i << ": " ; cin >> co [i] ; } } polynomial operator + (polynomial); polynomial operator - (polynomial); polynomial operator * (polynomial); polynomial operator +=(polynomial); polynomial operator -=(polynomial); polynomial operator *=(polynomial);

bool operator ==(polynomial); void print(int i = 4){ for ( ; i >= 0 ; i-- ){ if ( i == 0 && co[i] > 0 ) { cout << " +"; cout << co[i] ;} else if ( i == 0 && co[i] < 0 ) { cout << " "; cout << co[i] ;} else if ( co[i] > 0) { cout << " +"; cout << co[i] << "x^" << i ;} else if ( co[i] < 0) { cout << " "; cout << co[i] << "x^" << i ;} } cout << endl; } }; polynomial polynomial :: operator + (polynomial P2) { polynomial P3; for ( int i = 0 ; i <= 4 ; i++ ){ P3.co [i] = co [i] + P2.co [i] ; } return P3; } polynomial polynomial :: operator - (polynomial P2) { polynomial P3; for ( int i = 0 ; i <= 4 ; i++ ){ P3.co [i] = co [i] - P2.co [i] ; } return P3; } polynomial polynomial :: operator *(polynomial P2){ polynomial P3; for( int i = 0 ; i <= 4 ; i++ ){ for( int j = 0 ; j <= 4 ; j++ ){ P3.co [i+j] += co [i] * P2.co [j] ; } } return P3; } bool polynomial :: operator ==(polynomial P2 ) { for (int i=0 ; i<=4 ; i++){

P3 = P1 + P2;

cout <<"Addition of two Polynomial is \n"; P3.print (); P3 = P1 - P2; cout <<"Subtraction of two polynomial is \n"; P3.print (); P3= P1 * P2; cout <<endl << "Multiplication of two polynomial is
n"; P3.print (9); system ("PAUSE"); return 0; } Q2. Create a class Polar that represents the points on the plain as polar coordinates ( radius and angle ).  Create an overloaded + operator for addition of two Polar quantities.  “Adding” two points on the plain can be accomplished by adding their X coordinates and then adding their Y coordinates.  This gives the X and Y coordinates of the “answer.”  Thus you’ll need to convert two sets of polar coordinates to rectangular coordinates, add them, then convert the resulting rectangular representation back to polar. #include #include #include <math.h> const double Pi =3.141593; using namespace std; class polar { private : double radius; double angle; // theeta in degrees public : polar () : radius(0.0),angle(0.0) { } polar (double r , double t){

radius = r; angle = t; } void show_polar () { cout << "Radius = "<< radius <<" , Theeta "<< angle <<" Degrees"<< endl; } polar operator + (polar a) { double r,t; double x1,x2, y1,y2, x3,y3; x1 = radius * cos( Piangle / 180); y1 = radius * sin( Piangle / 180); x2 = a.radius * cos(Pia.angle / 180); y2 = a.radius * sin(Pia.angle / 180); x3 = x1 + x2; y3 = y1 + y2; r = sqrt( x3x3 + y3y3 ); t = atan(y3/x3); t = t*180 / Pi; // convert to degrees return polar (r,t); } }; int main() { polar P1(3,0); polar P2(4,90); cout <<"For P1: "; P1.show_polar(); cout <<"For P2: "; P2.show_polar(); polar P3 = P1 + P2; cout <<"P1+P2 = : ";P3.show_polar(); system("PAUSE"); return 0; }

return (sqrt ((ii) + (jj) + (kk))); } }; Vector3D Vector3D :: operator + (Vector3D v2) { Vector3D v3; v3.i = i + v2.i; v3.j = j + v2.j; v3.k = k + v2.k; return v3; } Vector3D Vector3D :: operator - (Vector3D v2) { Vector3D v3; v3.i = i - v2.i; v3.j = j - v2.j; v3.k = k - v2.k; return v3; } double Vector3D :: operator * (Vector3D v2) { // Dot Product return ( iv2.i + jv2.j + kv2.k ); } Vector3D Vector3D :: operator / (Vector3D v2) { // Cross Product Vector3D v3; v3.i = (j * v2.k) - (k * v2.j); v3.j = (k * v2.i) - (i * v2.k); v3.k = (i * v2.j) - (j * v2.i); return v3; } Vector3D Vector3D :: operator += (Vector3D v2) { i += v2.i; j += v2.j; k += v2.k; return Vector3D(i,j,k); } Vector3D Vector3D :: operator -= (Vector3D v2) { i -= v2.i; j -= v2.j; k -= v2.k; return Vector3D(i,j,k); } Vector3D Vector3D :: operator /= (Vector3D v2) { Vector3D v3; v3.i = j * v2.k - k * v2.j; v3.j = k * v2.i - i * v2.k; v3.k = i * v2.j - j * v2.i;

i = v3.i; j = v3.j; k = v3.k; return v3; } int main(){ Vector3D v1,v2, v3; cout << "Enter Values of 1st Vector: "<< endl; v1.getvalues(); cout << "\nEnter Values of 2nd Vector: "<< endl; v2.getvalues(); cout << "\n1st vector is "; v1.print(); cout <<"2nd vector is "; v2.print(); cout << "\nFor 1st vector, Length = " << v1.length()<<endl; cout << "For 2nd vector, Length = " << v2.length()<<endl; v3= v1 + v2; cout <<"\nAnswer of Addition is = "; v3.print (); v3= v1 - v2; cout <<"Answer of Subtraction is = "; v3.print (); cout << "\nDot product is = " << v1*v2 <<endl; v3 = v1 / v2; cout <<"Cross product is = "; v3.print (); v1 /= v2; cout <<"Cross product is = "; v1.print (); system ("PAUSE"); return 0; } Q4. Modify the Program at slide number 48 for 3D Polar to 3D Cartesian Coordinates conversion.

void Show() const{ cout << "X = " << X <<", Y = " << Y << ", Z = " << Z <<endl; } }; int main(){ Polar Pol(5,53.13, 45); // R = 5, Theeta = 53. Degrees, Psi = 45 Degrees Cartesian Cat = Pol; // Convert Polar to Cartesian Pol.Show(); // Show in Polar Cat.Show(); // Show in Cartesian system("PAUSE"); return 0; }