[ale] [OT] C++ Question

Denny Chambers bugfix4u2 at bellsouth.net
Mon Dec 2 11:54:59 EST 2002


Here was an assignment I had back in college that may help as an 
example. As far as I remember this code worked.

Jonathan Glass wrote:

>I'm working on my final project, and am having the hardest time with the
>overloading the "+" operator for a custom class.  Any thoughts?  I'll be
>happy to attach the full source file on request.  
>
>TIA
>
>-----
>
>CMoney CMoney::operator+ ( CMoney& t)
>{
>        // money3 = money1 + money2
>        CMoney temp;
>
>        temp.m_cents= this->m_cents + t.m_cents;
>        return temp;
>}
>
>int main(int argc, char* argv[])
>{
>        CMoney money1;
>        money1.AddMoney(0,101);
>        CMoney money2;
>        money2.AddMoney(0,100);
>        CMoney money3;
>        money3.ShowMoney();
>        money3=money1+money2;
>        cout << "MONEY1: ";
>        money1.ShowMoney();
>        cout << endl << "MONEY2: ";
>        money2.ShowMoney();
>        cout << endl;
>        cout << "MONEY3: ";
>        money3.ShowMoney();
>        return 0;
>}
>
>----- 
>Jonathan Glass
>Systems Support Specialist II
>Institute for Bioengineering & Bioscience
>Georgia Institute of Technology
>404.385.0127
>
>_______________________________________________
>Ale mailing list
>Ale at ale.org
>http://www.ale.org/mailman/listinfo/ale
>
>  
>


// Denny Chambers  //
// Asignment 5	   //
// August 1, 1997  //
// Polynomial.cpp  //


#include <iostream.h>
#include "term.h"
#include "polynomial.h"


		//Default Constructor
		Polynomial::Polynomial()
		{
			list = NULL;
		}

		//Copy constructor for Polynomial
		Polynomial::Polynomial(Polynomial & p)
		{
			if(this != &p)
			{
				return;
			}
			delete list;
			list = new Term;
			*list = *p.list;
		}


		//Destructor
		Polynomial::~Polynomial()
		{
			delete list;
		}


		//Overloaded "+" operator to add Polynomials
		Polynomial & Polynomial::operator +(Polynomial & p)
		{
			Term *temp1 = p.list;

			
			while(temp1 != NULL)
			{
			    this.Add(*temp1);
			    temp1 = temp1->next;
			}
		}



		//Overloaded "<<" to print out Polynomials
		ostream & operator <<(ostream & os, const Polynomial * p)
		{
			Term *temp = p->list;
			
			cout << endl;
			if(temp->coef < 0)
			{
			    cout << "-"; 
			}

			while(temp != NULL)
			{
			    cout << temp;
			    temp = temp->next;
			}
		    return os;
		}
			    


		//Overloaded "==" to compare Polynomials
		int Polynomial::operator ==(const Polynomial & p)
		{

			Term *temp = this->list;
			Term *temp1 = p.list;
			while(temp != NULL && temp1 != NULL)
			{
			    if(temp == temp1)
			    {
				temp = temp->next;
				temp1 = temp1->next;
			    } 
			    else
			    {
				return 0;
			    }
			}
			if(temp == NULL && temp1 == NULL)
			{
			    return 1;
			}
			else
			{
			    return 0;
			}
		}
			    



		//A member function that will add a term to the list.
		Polynomial * Polynomial::Add(Term & t)
		{
			Term *temp = this->list;
			Term *temp2 = this->list;
			if(this->list == NULL)
			{
				this->list = &t;
				return this;
			}
			else
			{
				while(temp != NULL)
				{
					if(t.exp == temp->exp)
					{
						temp->coef = *temp->coef + *t.coef;
						return this;
					}
					else
					{
					    if(temp->exp < t.exp)
					    {
						t.next = temp;
						t.prev = temp->prev;
						temp->prev = &t;
						temp2->next = &t;
						return this;
					    }
					    else
					    {
						
						temp2 = temp;
						temp = temp->next;
					    }
					}
				}

				temp = &t;
				t.prev = temp2;
				return this;
			}
		}


		//A member function that will delete a specific term.
		Polynomial * Polynomial::Delete(Term & t)
		{

			Term *temp = this->list;
			Term *temp3, *temp4;

			if(temp = NULL)
			{
			    cout << endl << "The list is empty. Nothing was deleted." << endl;
			    return this;
			}
			else
			{
			    while(temp != NULL)
			    {
			    	if(temp->coef == t.coef && temp->exp == t.exp)
				{
				    temp3 = temp->next;
				    temp4 = temp->prev;
				    delete temp;
				    temp3->prev = temp4;
				    temp4->next = temp3;
				    return this;
				}
				else
				{
				    temp = temp->next;
				}
			    }
			}
			cout << endl << "This term does not exist in this polynomial" << endl;
			return this;
		}
			
		





More information about the Ale mailing list