Polynomial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# include <iostream>
using namespace std; 

class Polynomial
{
      private :
                int *array ;
                int x ;
      public  :
                Polynomial(int y)
                {
                  x = y ;
                }
                void  setPolynomial() ;
                int getPolynomial() ;

     	        void operator =  (const Polynomial& ) ;
     	        Polynomial operator +  (const Polynomial& ) ;
     	        Polynomial operator -  (const Polynomial& ) ;
     	        Polynomial operator *  (const Polynomial& ) ;
     	        Polynomial operator += (const Polynomial& ) ;
     	        Polynomial operator -= (const Polynomial& ) ;
     	        Polynomial operator *= (const Polynomial& ) ;
};

void Polynomial::setPolynomial()
{
     array = new int [x] ;
     int arr[x] ;
     for ( int i = 0 ; i < x+1 ; i++ )
     {
         cout << "Enter Cofficinet x^( " << i <<") : " ;
         cin  >> arr[i] ;
         array[i] =  arr[i] ;
     }
}

int Polynomial::getPolynomial()
{
//    array = new int [x] ;
    for (int i = 0 ; i < x+1 ; i++)
    {
      cout <<"Cofficinet x^( " << i <<") : " << array[i] << endl;
    }
}


Polynomial Polynomial::operator +  (const Polynomial& p ) 
{
   array = new int [x] ;
   int arr [x] ;
   
   for ( int i = 0 ; i < x+1 ; i++ )
   {
       arr[i] = array[i] + p.array[i] ;
       
       cout <<"Cofficinet x^( " << i <<") : " << arr[i] << endl ;
   }
   
}



int main ()
{
    int power ;
    char choice ;
    cout << "The Polynomial Function to the Power : " ;
    cin >> power ;
    
    Polynomial p1(power) ;
    Polynomial p2(power) ;

    p1.setPolynomial() ;

    cout << "Enter Operator ( + , - , * , * , += , -= , *= ) : " ;
    cin  >> choice ;

    switch (choice)
    {
      case '+' :
                 p2.setPolynomial() ;
                 Polynomial p3(power) ;

                 p3 = p1 + p2 ;
                 p3.getPolynomial() ;
                 break ;
    }

    system("pause");
    return 0 ;
}
lines 85 and 86 doesn't run the program and cause linker error
what can i do ?
because line 85 states: class_object3 = class_object1 + class_object2 ?

I wouldn't think you can add objects together like this, but I might be wrong.
i have made an operator function to add them see line 48
Tht first thing I see when I look here is that you havn't declaired p3 as an object of polynomial yet. See how you did with p1 and p2. So when you get to p3 the compiler doesn't know what those objects are from unless you declair them.

Yes you can add objects together but you need to have declaired the object to get the value too. IE. Polynomial p3; Hope that helps some.
Topic archived. No new replies allowed.