Thread: need someone to check my coding

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    need someone to check my coding

    hi.
    i am doing a question from the book on c++ and would like all of you to check the errors..
    this is about adding complex numbers

    but i am getting many errors can some check what's wrong
    Code:
    #include<iostream>
    using namespace std;
    
    class complexNumber
    {
    
    private:
    
    float realPart;
    float imgPart;
    static int count;
    
    public:
    complexNumber ( float real=0,float img=0);
    void show();
    
    static int getcount() { return count;};
    void incr();
    
    friend int add( complexNumber, complexNumber):complexNumber
    };
    
    complexNumber :: complexNumber( float real , float img)
    {
    
    realPart = real; 
    realPart = real;
    
    count ++;
    }
    void complexNumber :: show()
    {
               
    cout<< realPart<< "+"<< imgPart<<"i"<<endl;
    
    }
    
    int add( complexNumber &a, complexNumber &b): complexNumber &c
    {
    c.realPart = a.realPart + b.realPart;
    c.imgPart = a.realPart + b.realPart;
    return c;
    }
     void complexNumber :: incr()
    {
    complexNumber a;
    a.realPart++;
    return 0;
    
    }
    
    int main()
    
    {
    
      complexNumber a(2.0, 1.0);        
      a.incr();
      a.show();
    
     return 0;   
    }
    i can't get it actually how to define <<friend>> add( complexNumber , complexNumber) :
    complexNumber
    and my incr() function is wrong..it is a member method that increments the complex object by 1 and effect the real part and return nothing

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    what error is your compiler reporting? and indent your code, or noone is likely to bother reading it.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Looks like there's lots of crazy stuff going on.
    1.) incr() is declared as void yet returns a zero?
    2.) add() is declared as returning an INT yet really returns a complexNumber
    3.) Reread about using static variables

    Try this:
    Code:
    #include<iostream>
    using namespace std;
    
    class complexNumber
    {
    
           private:
                    float realPart;
                    float imgPart;
                    static int count;
    
           public:
           complexNumber ( float real=0,float img=0);
           
           void show();
    
           int getcount();
           int incr();
           friend complexNumber add(complexNumber, complexNumber);
         
    };
    
    int complexNumber::count=0;
    
    int complexNumber::getcount()
    {
        return complexNumber::count;
    }
    
    complexNumber::complexNumber(float real, float img)
    {
                   realPart = real; 
                   imgPart = img;
    
                   count++;
    }
    
    complexNumber add(complexNumber a, complexNumber b)
    {
                  complexNumber c;
                  c.realPart=a.realPart+b.realPart;
                  c.imgPart=a.imgPart+b.imgPart;
                  return c;
    }
    
    void complexNumber :: show()
    {
               cout<< realPart<< "+"<< imgPart<<"i"<<endl;
    }
    
    int complexNumber :: incr()
    {
         complexNumber a;
         a.realPart++;
         return 0;
    }
    
    int main()
    {
    
        complexNumber a(2.0, 1.0);        
        a.incr();
        a.show();
    
        return 0;   
    }
    I still don't think this is really going to do everything you think it is, your incr() for example, but this might get you going.
    Last edited by Dragoon_42; 11-16-2009 at 03:33 PM. Reason: forgot the last line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. Replies: 3
    Last Post: 07-24-2007, 05:25 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2002, 12:21 AM
  5. modulus check digit
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-18-2002, 04:04 PM
Website Security Test