#include #include #include #include using namespace std; class TreeNode { public: int data; TreeNode * left; TreeNode * right; }; class BinarySearchTree { public: BinarySearchTree() { root=NULL; } void add(int value); void inorder(); private: TreeNode * root; void inorderHelper(TreeNode * node); }; void BinarySearchTree::inorder() { cout << "Start Print BST" << endl; inorderHelper(root); cout << "End Print BST" << endl; } void BinarySearchTree::inorderHelper(TreeNode * node) { if (root==NULL) { return; } inorderHelper(node->left); cout << node->data << endl; inorderHelper(node->right); } void BinarySearchTree::add(int value) { // start of solution to question 1 // end of solution to question 1 } class Exception { public: Exception(char*s) { msg=s; } virtual void print() { cout << msg << endl; } protected: char *msg; }; class UnimplementedMethodException: Exception { public: UnimplementedMethodException(char*s):Exception(s) {} virtual void print() { cout << "Not Implemented @ " << msg << endl; } }; class DivisionByZeroException: Exception { public: DivisionByZeroException(char*s):Exception(s) {} virtual void print() { cout << "Division By 0 @ " << msg << endl; } }; class Computable { public: virtual Computable operator+(const Computable& c2) const; virtual Computable operator-(const Computable& c2) const; virtual Computable operator*(const Computable& c2) const; virtual Computable operator/(const Computable& c2) const; virtual void print() const; }; Computable Computable::operator+(const Computable& c2) const { UnimplementedMethodException e("Computable::operator+()"); throw e; } Computable Computable:: operator-(const Computable& c2) const { UnimplementedMethodException e("Computable::operator+()"); throw e; } Computable Computable:: operator*(const Computable& c2) const { UnimplementedMethodException e("Computable::operator+()"); throw e; } Computable Computable:: operator /(const Computable& c2) const { UnimplementedMethodException e("Computable::operator+()"); throw e; } void Computable::print() const { UnimplementedMethodException e("Computable::print()"); throw e; } class Real : public Computable { private: double real; public: Real(double value) { real=value; } virtual Real operator+(const Real& c2) const; virtual Real operator -(const Real& c2) const; virtual Real operator*(const Real& c2) const; virtual Real operator/(const Real& c2) const; virtual void print() const; }; Real Real::operator+(const Real& c2) const { Real result(real+c2.real); return result; } Real Real::operator-(const Real& c2) const { Real result(real-c2.real); return result; } Real Real::operator*(const Real& c2) const { Real result(real*c2.real); return result; } Real Real::operator/(const Real& c2) const { if (c2.real==0.0) { DivisionByZeroException e("Real::operator/()"); throw e; } Real result(real/c2.real); return result; } void Real::print() const { cout << real << endl; } class Complex : public Computable { private: double real; double imaginary; public: Complex(double r, double i) { real=r; imaginary=i; } virtual Complex operator+(const Complex& c2) const; virtual Complex operator-(const Complex& c2) const; virtual Complex operator*(const Complex& c2) const; virtual Complex operator/(const Complex& c2) const; virtual void operatorcomplex() const; virtual void print() const; }; Complex Complex::operator+(const Complex& c2) const { Complex result(real+c2.real,imaginary+c2.imaginary); return result; } Complex Complex::operator-(const Complex& c2) const { Complex result(real-c2.real,imaginary-c2.imaginary); return result; } Complex Complex::operator*(const Complex& c2) const { Complex result(real*c2.real - imaginary*c2.imaginary, real*c2.imaginary + imaginary*c2.real); return result; } Complex Complex::operator/(const Complex& c2) const { UnimplementedMethodException e("Complex::inverse()"); throw e; } void Complex::operatorcomplex() const { // this method is defined to provide an extra test case for your regular expression } void Complex::print() const { cout << real << " + " << imaginary << "i" << endl; } void q2() { cout << "start q2" << endl; Complex c1(1.0,2.0); Complex c2(2.0,3.0); Complex c3=c1+c2; Complex c4=c1-c2; Complex c5=c1*c2; Real i1(3.0); Real i2(4.0); Real i3=i1+i2; Real i4=i1-i2; Real i5=i1*i2; c3.print(); c4.print(); c5.print(); i3.print(); i4.print(); i5.print(); cout << "end q2" << endl; } /* start of solution to question 2 Output: Computable: objects= copy constructors= non-copy constructors= destructors= Real: objects= copy constructors= non-copy constructors= destructors= Complex: objects= copy constructors= non-copy constructors= destructors= end of solution to question 2 */ void q3() { cout << "start q3" << endl; Complex x(1.0,2.0); Complex y(2.0,3.0); Complex z(0.0,0.0); Complex i1(0.0,0.0); Complex i2(0.0,0.0); Complex i3(0.0,0.0); Complex i4(0.0,0.0); try { i1=x+y; i2=x*y; i3=x/z; i4=z-y; } catch (Exception& e) { cout << "haha"<