need someone to check my coding

This is a discussion on need someone to check my coding within the C++ Programming forums, part of the General Programming Boards category; hi. i am doing a question from the book on c++ and would like all of you to check the ...

  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,189
    what error is your compiler reporting? and indent your code, or noone is likely to bother reading it.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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 02: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-05-2010, 11:42 PM
  2. Replies: 3
    Last Post: 07-24-2007, 04:25 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12: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-24-2002, 11:21 PM
  5. modulus check digit
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-18-2002, 03:04 PM

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