Complex number

Plz I need the solution, I know I'm not supposed to be getting the solution with no effort but the due date of the assignment is tomorrow and i need help, and I am not that good.

1) Create a class Complex defining a complex number with different operations as follows:
 A complex number is defined of a real number, an imaginary number a name and an
ID.
The class should provide methods for the following operations:
 adding a complex number to itself
 subtracting a complex number from itself
 dividing itself with a complex number
 multiplying a complex number to itself
 printing out the complex number in the form of “Name(ID)=Real + iImaginary”
The attributes of the class should not be accessible outside the class except for the name which
should be available to everyone. The name of the object cannot be changed once initialized. As
for the rest of the data members, we should be able to read them and change their values. The ID
is unique, so 2 complex numbers cannot have the same id, it is an incremented number as shown
in the example below.
At all times during the main program, we should maintain a count of the number of objects
created.
Ex: Complex c1; //id=1
Complex c2; //id=2
Complex c3; //id=3
 The name is a character
 The mathematical operations should be defined as cascaded functions
 All the getter functions should be constants
 The printout member function should be defined outside the class
 A complex number by default should be “name=1+I”
 There should be only one constructor
 When an object is destroyed, a message should be printed out
 The number of instances created should be held within a private static variable which can
be read through a static getter member functionvariable which can
be read through a static getter member function
Check this out:
http://cplusplus.com/reference/complex/complex/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Complex
{
private: // members                        // The attributes of the class should not be accessible outside the class
  double real;                             // Real number
  double imag;                             // Imaginary number
  int id;                                  // id
  static int created = 0;                  // At all times during the main program, we should maintain a count o the number of objects
public:
  const char name;                         // except for the name which should be available to everyone
public: // Constructors
  Complex(char Name) : name(Name)          // the name is constant
  {         
    id = ++created;                        // it is an incremented number as shown in the example below
  }
  ~Complex()
  {
    std::cout << "Complex destroyed";
  }
public: // getters/setters                 // for the rest of the data members
  const double GetReal() { return real; }  // we should be able to read them and change their values
  const double GetImag() { return imag; }  // 
  const double GetID()   { return id;   }
  void SetReal (double r) { real = r; }
  void SetImag (double i) { imag = i; }
  void SetID (int ID) { id = ID; }
public: // methods
  void operator+=(const Complex& c)        //  adding a complex number to itself
  {
    real+=c.real;
    imag+=c.imag;
  }
  void operator-=(const Complex& c)        //  subtracting a complex number from itself
  {
    real-=c.real;
    imag-=c.imag;
  }
  void operator/=(const Complex& c)        //  dividing itself with a complex number
  {
    real/=c.real;
    imag/=c.imag;
  }
  void operator*=(const Complex& c)        //  multiplying a complex number to itself
  {
    real*=c.real;
    imag*=c.imag;
  }
  friend std::ostream& operator<< (std::ostream& o, const Complex& c);
};

std::ostream& operator<< (std::ostream& o, const Complex& c) // the printout function shall be declared outside of the class
{//  printing out the complex number in the form of “Name(ID)=Real + iImaginary”
  return o<< name << '(' << id << ")="<<real<<" + i"<<imag;
}


There were some ill-defined requirements here by the way.
Topic archived. No new replies allowed.