Classes II

Classes

Destructor

Destructor should be only a private, runtime function.

class Test
{
	public:
		// Constructor - run on the object creation
		Test() { cout << "Object is created"; }

		//Destructor - run on the object destruction
		~Test() { cout << "Object was destroyed"; }
}

The destructor is run when:


Assignment operator

class FractionSet()
{
	private:
		// as previously
	public:
		FractionSet();
		~FractionSet();
		void insertElement(const Fraction& f);
		void insertE
		// something something
}
FractionSet& FractionSet::operator=(const FractionSet& x)
{
	// Self assignment
	if (this == &x)
		return *this;

	element* etr = head;
	while (etr)
	{
		etr = etr -> next;
		delete head;
		head = etr;
	}
	
	element *xtr = x.head;

	while (xtr)
	{
		Fraction copied = xtr -> data;
		element* ntr = new element;
		if (!ntr)
		{
			cerr << "FractionSet::operator=: Allocation memory failure!\n";
			break;
		}
		ntr -> data = copied;
		ntr -> next = NULL;
		if (!head)
			head = ntr;
		else
			etr -> next = ntr;
		
		etr = ntr;
		xtr = xtr -> next;

		number = x.number;
		return *this
	}
}