troubleshooting Question

Complex math class assignment

Avatar of dreaminbinary
dreaminbinary asked on
C++
4 Comments2 Solutions366 ViewsLast Modified:
My assignment is

Implement a complex number object having multiply,divide add and subtract operations via the definitions :

(a+b)+(x+y)=(a+x)+(b+y) add

(a+b)-(x+y)=(a-x)+(b-y) sub

(a+b)*(x+y)=(a*x+b*y)+(a*y+b*x) mult

(a+b)/(x+y)=((a+b)*(x-y)) / (xsquared+ ysquared) divide

Here is what I did

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

class Operand{
   protected:
      float a,b,x,y;
   public:
      Operand(void){a=0,b=0,x=0,y=0;}
      Operand(float n1, float n2,float n3,float n4){a=n1,b=n2,x=n3,y=n4;}
      void setNum1(const float n1){a=n1;}
      void setNum2(const float n2){b=n2;}
      void setNum3(const float n3){x=n3;}
      void setNum4(const float n4){y=n4;}
      const float getNum1(void) {return this->a;} // read state services
      const float getNum2(void) {return this->b;} // read state services
      const float getNum3(void) {return this->x;} // read state services  
      const float getNum4(void) {return this->y;} // read state services

};

class ComplexOperation: public Operand{
   private:
     float result;
   public:
      ComplexOperation(void) {result=0;}
      void add(void)  {result = (a+x)+(b+y);}
      void sub(void)  {result = (a-x)+(b-y);}
      void div(void)  {result = ( (a+b)*(x-y)  / (x+y)+(x+y) );}
      void mul(void)  {result = ( (a*x + b*y) + (a*y+b*x));}
      float showResult() {return result;}          
 };



int main (void)
{
  srand(time(0)); // Initialize random number generator.

  float r(((rand() % 10) + 1));
  float r2(((rand() % 10) + 1));
  float r3(((rand() % 10) + 1));
  float r4(((rand() % 10) + 1));

  ComplexOperation math1;
 
  math1.setNum1(r);
  math1.setNum2(r2);
  math1.setNum3(r3);
  math1.setNum4(r4);

  math1.add();
  cout<<"\n ("<<math1.getNum1()<<"+"<<math1.getNum2()<<") + ("<<math1.getNum3()<<"+"<<math1.getNum4()<<")="<<math1.showResult()<<"\n";
  math1.sub();
  cout<<"\n ("<<math1.getNum1()<<"+"<<math1.getNum2()<<") - ("<<math1.getNum3()<<"+"<<math1.getNum4()<<")="<<math1.showResult()<<"\n";
  math1.mul();
  cout<<"\n ("<<math1.getNum1()<<"+"<<math1.getNum2()<<") * ("<<math1.getNum3()<<"+"<<math1.getNum4()<<")="<<math1.showResult()<<"\n";
  math1.div();
  cout<<"\n ("<<math1.getNum1()<<"+"<<math1.getNum2()<<") / ("<<math1.getNum3()<<"+"<<math1.getNum4()<<")="<<math1.showResult()<<"\n";

  return 0;
}


1) I know the divide operations doesn't work
2) Is this code right and effecient? ( im taking an intro to OOP class)
3) I need help with a copy constructor
4) is this code reusable ( whole reason for OOP right ?)


ASKER CERTIFIED SOLUTION
itsmeandnobodyelse

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros