Need help(complex no. class)

This is a discussion on Need help(complex no. class) within the C++ Programming forums, part of the General Programming Boards category; Hello,, I have made a class of complex numbers with functionality that it can add and subtract. But i get ...

  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 08: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, 09: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, 03:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21