Thread: Need help(complex no. class)

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Need help(complex no. class)

    Hello,,
    I have made a class of complex numbers with functionality that it can add and subtract. But i get loads of errors in it . Can anybudy help me.

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<math.h>
    
    class complex
    { int r;
      int i;
    
      public:
    
      complex (int a, int b)
      {
      r=a;
      i=b;
      }
      void input ()
      {
      cin>>r>>i;
      }
      complex add(complex k)
      {
      complex add;
    	add.r=r+k.r;
    	add.i=i+k.i;
      return add;
      }
      complex sub(complex q)
      {
      complex sub;
    	sub.r=r-q.r;
    	sub.i=i-q.r;
      return sub;
      }
      void print()
      {
      cout<<r;
      cout<<i;
      }
    
    int main()
    {  class d,e;
       complex c(7,8);
       d.input();
       e=c.add(d);
       e.print();
       e=c.sub(d);
       e.print();
       return 0;
    }
    Last edited by Fatima Rizwan; 10-09-2009 at 07:31 AM.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    #include<iostream>
    #include<math.h>
    
    
    class complex { 
      int r;
      int i;
    
    public:
      complex() : r(0), i(0) {}
    
      complex(int a, int b) {
        r = a;
        i = b;
      }
    
      void input() {
        std::cin>>r>>i;
      }
    
      complex add(complex k) {
        complex add;
        add.r = r+k.r;
        add.i = i+k.i;
        return add;
      }
    
      complex sub(complex q) {
        complex sub;
        sub.r=r-q.r;
        sub.i=i-q.r;
        return sub;
      }
    
      void print() {
        std::cout<<r;
        std::cout<<i;
      }
    };
      int main()
      {  
        complex d,e;
        complex c(7,8);
        d.input();
        e=c.add(d);
        e.print();
        e=c.sub(d);
        e.print();
        return 0;
      }

    ur code which is working

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    And if u r getting error please post also it will be easy to detect

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    thnk yew so vey much RockyMarrone,, now its working =))

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Enjoy C++ Programming And u r most Welcome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM