where to implement these lines

This is a discussion on where to implement these lines within the C++ Programming forums, part of the General Programming Boards category; hello all, i have almost finished a C++ program but am not sure where to place these lines and was ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    where to implement these lines

    hello all,
    i have almost finished a C++ program but am not sure where to place these lines and was hoping for some direction. i know i need to implement:

    complx &operator + (double, complx);

    complx &operator - (double, complx);

    complx &operator * (double, complx);

    but i dont exactly know where to place them. Im pretty sure they will go respectively in the complx.cpp page. Any help would be appreciated. and sorry about the length of this post. i've underlined a line of code towards the very bottom where i am getting my compiling error and im pretty sure it has to do with not including complx &operator (double, complx); in the complx.cpp file. I no longer have my notes on alot of this after formating my hard drive and forgetting to save them. so again, thanks for any help given

    Code:
    // Complx.cpp
    
    #include "complx.h"
    
    // define I/O stream
    
     
    
    extern ifstream &operator >> ( ifstream &in_file, complx &number )
    
    
    {
    
         double re, is;
    
         char ch;
    
         if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','
    
              && in_file >> is >> ch && ch == ')')
    
                  number.Set(re,is);
    
         else cerr << "Finish the input"<<endl;
    
         return in_file;
    
    }
    
    ostream& operator << ( ostream& out_file, complx number ); 
    
    
    // define constructor
    
    complx::complx( double r, double i )
    
    {
    
          real = r; imag = i;
    
    }
    
    
    // define overloaded + (plus) operator
    
    complx complx::operator+ (complx c)
    
    
    {
             
          complx result;
    
          result.real = (this->real + c.real);
    
          result.imag = (this->imag + c.imag);
    
          return result;
    
    }
    
    // define overloaded - (minus) operator
    
    complx complx::operator- (complx c)
    
    
    {
           complx result;
           
           result.real = (this->real - c.real);
           
           result.real = (this->imag - c.imag);
           
           return result;
           
    }
    
    // define overloaded * (multiplication) operator
    
    complx complx::operator* (complx c)
    
    
    {
           complx result;
           
           result.real = (this->real * c.real);
           
           result.real = (this->imag * c.imag);
           
           return result;
           
    }

    header file
    Code:
    // Complx.h
    
    #include <iostream>
    
    #include <fstream>
    
    using namespace std;
    
    
    class complx
    
    {
    
          double real,
                 imag;
    
    public:
    
          complx( double real = 0., double imag = 0.); // constructor
    
          complx operator+(complx);       // operator+()
    
          complx operator+(double);       // operator+()with double
    
          complx operator-(complx);       // operator-()
    
          complx operator-(double);       //operator-()with double
    
          complx operator*(complx);       // operator*()
    
          complx operator*(double);       //operator*() with double
    
     //Sets private data members.
    
         void Set(double new_real, double new_imaginary) {
    
         real = new_real;
    
         imag = new_imaginary;
    
         }
    
    
         //Returns the real part of the complex number.
    
              double Real() {
    
              return real;
    
         }
    
     
         //Returns the imaginary part of the complex number.
    
         double Imaginary() {
    
         return imag;
    
         }
    
    };
    
    
    ostream& operator << ( ostream& out_file, complx number );
    
    extern ifstream &operator >> ( ifstream& in_file, complx &number );
    
    complx &operator + (double, complx);
    
    complx &operator - (double, complx);
    
    complx &operator * (double, complx);
    call_complx
    Code:
    //call_complx.cpp
    
    #include "complx.h"
    
    ifstream infile ("in.dat");
    
    int main()
    
    {
    
              int i=0;
    
              complx in[5];
    
              double d = 4.5; 
    
              cout<< "The input numbers are: " << endl;
    
             while (infile  >> in[i]){
    
                  cout << in[i] << endl;
    
                  i++;
    
            }      
    
              complx s1 =  in[0] + in[1]; // calls complx::operator+()
    
              complx s2 =  d + in[2]; // overload operator+()
    
              complx s3 =  in[3] + d; // overload operator+()
    
              complx a1 = in[4]  in[5]; // calls complx::operator-()
    
              complx a2 = d - in[6]; // overload operator-()
              
              complx a3 = in[7] - d; // overload operator-()
              
              complx mm1 = in[3] * in[4]; //calls complx::operator*()
              
              complx mm2 = d * in[5]; // overload operator*()
              
              complx mm3 = in[6] * d; //overload operator*()
              
              complx dm=d*in[4] ;
    
              complx b=d-in[0] ;
    
              cout << "The sum is a complex number " << s1 <<endl;
    
              cout << "The sum is a complex number " << s2 <<endl;
    
              cout << "The sum is a complex number " << s3 <<endl;
    
              cout << "The subtract is a complex number " << a1 <<endl;
              
              cout << "The subtract is a complex number " << a2 <<endl;
              
              cout << "The subtract is a complex number " << a3 <<endl;
    
              cout << "The product is a complex number " << mm1 <<endl;   
              
              cout << "The product is a complex number " << mm2 <<endl;
              
              cout << "The product is a complex number " << mm3 <<endl;  
    
              cout << "The subtract is a complex number " << b <<endl;
    
              cout << "The product is a complex number " << dm <<endl;
    
              return 0;   //successful termination
    
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well one thing I noticed is that your header file should have header #include guards.

    BTW, you do know there already is a <complex> header that's part of C++ right?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    i didnt know there was a <complex> header and what will the #include gaurds do?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by danf004 View Post
    i didnt know there was a <complex> header and what will the #include gaurds do?
    #include guards prevent the header from being included multiple times in the same module, which prevents warnings about "previous declaration of function ..."
    The header guard looks like this:

    Code:
    #ifndef SOME_LONG_UNIQUE_NAME__USUALLY_THE_FILENAME
    #define SOME_LONG_UNIQUE_NAME__USUALLY_THE_FILENAME
    
    // Put the rest of your header here.
    
    #endif  // SOME_LONG_UNIQUE_NAME__USUALLY_THE_FILENAME

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 10:51 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-20-2008, 11:58 PM
  4. Reading lines from a file
    By blackswan in forum C Programming
    Replies: 9
    Last Post: 04-26-2005, 04:29 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM

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