How to access the size of another class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: How to access the size of another class

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Posts
    3

    Question How to access the size of another class

    Hello, I'm having trouble with multiple parts in my polynomial program. First of all, I can't seem to find a way to access the mySize variable from the Polynomial class, also, I'm not quite sure how to combine the liketerms, and finally if you could give me some tips for the multiplication function in Polynomial class. Any help would be appreciated.

    polynomial.cpp
    Code:
    #include <cassert>
    using namespace std;
    
    #include "Polynomial.h"
    
    
    /*----------------------------------------------------------------------
     Member and friend functions for Monomial.
     -----------------------------------------------------------------------*/
    
    Monomial operator+(Monomial& a, Monomial& b) {
    
    	Monomial res;
    
    	for(int i = 0; i < mySize; i++){ // Attempting to access mySize from here and combine liketerms
    		if(a.exp == b.exp){
    			res.coef = a.coef + b.coef;
    			res.exp = a.exp;
    		}
    		else if(a.exp != b.exp){
    			res.coef = a.coef;
    			res.exp = a.exp;
    		}
    		else if(b.exp != a.exp){
    			res.coef = b.coef;
    			res.exp = b.exp;
    		}
    	}
    
    return res;
    
    }
    
    Monomial operator-(Monomial& a, Monomial& b) {
        Monomial res;
    
        for(int i = 0; i < mySize; i++){ // Attempting to access mySize from here and combine liketerms
    		if(a.exp == b.exp){
    			res.coef = a.coef - b.coef;
    			res.exp = a.exp;
    		}
    		else if(a.exp != b.exp){
    			res.coef = a.coef;
    			res.exp = a.exp;
    		}
    		else if(b.exp != a.exp){
    			res.coef = b.coef;
    			res.exp = b.exp;
    		}
        }
    return res;
    
    }
    
    Monomial operator*(Monomial& a, Monomial& b) {
        Monomial res;
    
        res.coef = a.coef * b.coef;
        res.exp  = a.exp + b.exp;
    
        return res;
    }
    
    
    ostream & operator<< (ostream & out, const Monomial & mono)
    {
    	out << mono.coef ;
    	if (mono.exp > 0) out << "x^" << mono.exp; 
    	return out;
    }
    
    
    /*----------------------------------------------------------------------
     Member and friend functions for Polinomial.
     -----------------------------------------------------------------------*/
    
    Polynomial::Polynomial()                 
    : mySize(0)
    {}
    
    
    bool Polynomial::empty() const
    {
       return mySize == 0;
    }
    
    
    void Polynomial::display(ostream & out) const
    {
    	for (int i = mySize-1; i >= 0 ; i--) {
    		if (i != mySize-1) out << " + ";
    		out << myArray[i];
    	}
    
    }
    
    
    ostream & operator<< (ostream & out, const Polynomial & aList)
    {
       aList.display(out);
       return out;
    }
    
    
    void Polynomial::insert(ElementType item, int pos)
    {
       if (mySize == CAPACITY)
       {
          cerr << "*** No space for list element -- terminating "
                  "execution ***\n";
          exit(1);
       }
       if (pos < 0 || pos > mySize)
       {
          cerr << "*** Illegal location to insert -- " << pos 
               << ".  List unchanged. ***\n";
          return;
       }
    
       // First shift array elements right to make room for item
    
       for(int i = mySize; i > pos; i--)
          myArray[i] = myArray[i - 1];
    
       // Now insert item at position pos and increase list size  
       myArray[pos] = item;
       mySize++;
    }
    
    
    void Polynomial::erase(int pos)
    {
       if (mySize == 0)
       {
          cerr << "*** List is empty ***\n";
          return;
       }
       if (pos < 0 || pos >= mySize)
       {
          cerr << "Illegal location to delete -- " << pos
               << ".  List unchanged. ***\n";
          return;
       }
    
       // Shift array elements left to close the gap
       for(int i = pos; i < mySize; i++)
           myArray[i] = myArray[i + 1];
    
       // Decrease list size
        mySize--;
    }
    
    Polynomial operator+(Polynomial &p1, Polynomial &p2){
        Polynomial res;
        int i = 0;
    
        if (p1.mySize > p2.mySize){
            res.mySize = p1.mySize;
        }else{
            res.mySize = p2.mySize;
        }
    
        for(i = 0; i < res.mySize; i++){
            res.myArray[i] = p1.myArray[i] + p2.myArray[i];
        }
    
    return res;
    
    }
    
    Polynomial operator*(Polynomial &p1, Polynomial &p2){ // Multiplication does not work properly
        Polynomial res;
        int i = 0;
    
        if (p1.mySize > p2.mySize){
            res.mySize = p1.mySize;
        }
    	else{
            res.mySize = p2.mySize;
        }
    
        /*for(i = 0; i < res.mySize; i++){
            res.myArray[i] = p1.myArray[i] * p2.myArray[i];
        }*/
    	
    	for (i = 0; i < res.mySize; i++){
    		for (int j=0; j < res.mySize; j++){
    			res.myArray[i+j] = (p1.myArray[i]*p2.myArray[j]) ;
    		 }
    	}
    return res;
    
    }
    polynomial.h
    Code:
    #include <iostream>
    
    #ifndef PNOM
    #define PNOM
    
    const int CAPACITY = 1024;
    
    
    class Monomial {
    protected:
    	int coef;
    	int exp;
    	// int size;
    public:
    	Monomial(){};
    	/*----------------------------------------------------------------------
    	 Construct a Monomial object.
    	 
    	 Precondition:  None
    	 Postcondition: An empty Monomial object has been constructed..
       -----------------------------------------------------------------------*/
    	Monomial(int c,int p) { coef = c; exp = p;};
    
    	friend Monomial operator+ (Monomial&, Monomial&);
    
    	friend Monomial operator- (Monomial&, Monomial&);
    
    	friend Monomial operator* (Monomial&, Monomial&);
    
    	friend class Polynomial;
    
    	int getExponent() {return exp;}
    	
        int getCoefficient() {return coef;}
        
    	/*----------------------------------------------------------------------
    	 Construct a Monomial object with specified coeffient and exponent.
    	 
    	 Precondition:  None
    	 Postcondition: A Monomial object has been constructed with the 
                      specified coeffient and exponent.
       -----------------------------------------------------------------------*/
    
    	friend ostream & operator<< (ostream & out, const Monomial & mono);	
    	/*----------------------------------------------------------------------
    	 Overloading the OUTPUT operator for Monomials. 
    	 
    	 Precondition:  None.
    	 Postcondition: The coefficient and exponent (if != 0) of the monomial are 
    	                displayed in the default output device.
       -----------------------------------------------------------------------*/
    };
    
    typedef Monomial ElementType;
    
    class Polynomial
    {
     public:
    
    	friend class Monomial;
    
       Polynomial();
       /*----------------------------------------------------------------------
         Construct a List object.
    
         Precondition:  None
         Postcondition: An empty List object has been constructed; mySize is 0.
       -----------------------------------------------------------------------*/
    
       /***** empty operation *****/
       bool empty() const;
       /*----------------------------------------------------------------------
         Check if a list is empty.
    
         Precondition:  None
         Postcondition: true is returned if the list is empty, false if not.
       -----------------------------------------------------------------------*/
    
       /***** insert and erase *****/
       void insert(ElementType item, int pos);
       /*----------------------------------------------------------------------
         Insert a value into the list at a given position.
    
         Precondition:  item is the value to be inserted; there is room in
             the array (mySize < CAPACITY); and the position satisfies
             0 <= pos <= mySize. 
         Postcondition: item has been inserted into the list at the position
             determined by pos (provided there is room and pos is a legal
             position).
       -----------------------------------------------------------------------*/
    
       void erase(int pos);
       /*----------------------------------------------------------------------
         Remove a value from the list at a given position.
    
         Precondition:  The list is not empty and the position satisfies
             0 <= pos < mySize.
         Postcondition: element at the position determined by pos has been
             removed (provided pos is a legal position).
       ----------------------------------------------------------------------*/ 
    
    	friend Polynomial operator+(Polynomial &, Polynomial &);
    
    	friend Polynomial operator*(Polynomial &, Polynomial &);
    
       /***** output *****/
       void display(ostream & out) const;
    
     protected:
       int mySize;                     // current size of list stored in myArray
       ElementType myArray[CAPACITY];  // array to store the Monomials
    
    }; //--- end of List class
    
    //------ Prototype of output operator
    ostream & operator<< (ostream & out, const Polynomial & p);
    
    #endif

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: How to access the size of another class

    First of all, I can't seem to find a way to access the mySize variable from the Polynomial class
    I'm not sure what your problem is, need more details.

    combine the liketerms
    Any two Monomials within a Polynomial should have different exponents. If you end up with more than one with the same exponent during a computation, just add their coefficients.

    if you could give me some tips for the multiplication function in Polynomial class
    Write down the steps for doing polynomial multiplication on paper. The programming logic will be very similar.

  3. #3
    Join Date
    Apr 2008
    Posts
    726

    Re: How to access the size of another class

    mySize is only in polynomial. So how does it make sense for monomial to access mySize from a monomial?

  4. #4
    Join Date
    Mar 2011
    Posts
    3

    Re: How to access the size of another class

    Well, for now I suppose the mySize can be ignored; however, for multiplication I implemented this piece of code but the result is not correct:

    Code:
     for (i = 0; i < p1.mySize; i++){
    		for (int j=0; j < p2.mySize; j++){
    			res.myArray[i+j] = (p1.myArray[i]*p2.myArray[j]) ;
    		 }
    }
    I tried using += but I get an error stating that 'ElementType' does not define this operator. I appreciate your help

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: How to access the size of another class

    Well, you could define operator+= for Monomial. In fact, that's usually a more useful thing to define than operator+.

    Now that I look more closely at your code, I'm wondering why you're trying to do Polynomial operations in a Monomial operator. This:
    Code:
    Monomial operator+(Monomial& a, Monomial& b)
    should have no need to know anything about other Monomials, only those two that are passed.

    The point of confusion may be----what to do if the two monomials have different exponents? Well, you can't handle that situation, so just throw an exception or an assertion failure or something like that. This will help make sure you never pass invalid arguments to the function.

  6. #6
    Join Date
    Mar 2011
    Posts
    3

    Re: How to access the size of another class

    Allright, well I was able to make the multiplication function work by adding a cout inside the second for and taking out the one in the main function. Now my problem is the adding liketerms and sorting the polynomial. I understand that all I'd have to do is compare the exponents and add the coefficients, but I am unsure of where to put it and how to access the seperate terms after each operation.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: How to access the size of another class

    If your multiplication function is truly "working", then it shouldn't matter where you do the output....

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center