GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Apr-2005, 14:49
clander clander is offline
New Member
 
Join Date: Apr 2005
Posts: 11
clander is on a distinguished road

Operator overloading c++


I am trying to make a program that makes a complex number class that will add subtract multiply and divide complex numbers. I am trying to make my code easier by using operator overloading but am running into some errors that I am stuck on. If anyone can help please do.

CPP / C++ / C Code:
#pragma once
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

class Complex
{
 public:
  Complex(double r, double i); 
  Complex();
  ~Complex();
  double getReal();
  void setReal(double r);
  double getImag();
  void setImag(double i);
  friend void operator <<(const Complex& num, ostream& out); 
  friend void operator >>(const Complex& num, ostream& in);
  friend Complex operator ==(const Complex& num1, const Complex& num2);
  friend Complex operator +(const Complex& num1, const Complex& num2); 
  friend Complex operator *(const Complex& num1, const Complex& num2);  
  friend Complex operator -(const Complex& num1, const Complex& num2);
  friend Complex operator /(const Complex& num1, const Complex& num2);
 private:
  double real;
  double imag;
};

CPP / C++ / C Code:
#include ".\complex.h"

Complex::Complex(){

 real = 0;
 imag = 0;
}

Complex::Complex(double r, double i){
 
 real = r;
 imag = i;
}

double Complex::getImag(){

 return imag;
}

double Complex::getReal(){

 return real;
}

void Complex::setImag(double i){

 imag = i;
}

void Complex::setReal(double r){

 real = r;
}

void operator <<(const Complex& num, ostream& out){

 out << num.real << "+" << num.imag << "i\n";

}

void operator >>(const Complex& num, ostream& in){

 char i;

 in >> num.real >> i >> num.imag >> i;
}

Complex operator ==(const Complex& num)
{

 Complex temp;

 temp.real = num.real;
 temp.imag = num.imag;

 return temp;
}

 Complex operator *(const Complex& num1, const Complex& num2){
 
 Complex temp;
 
 temp.real = num1.real * num2.real - num1.imag * num2.imag;
 temp.imag = num1.real * num2.imag + num1.imag * num2.real;
 
 return temp;
}

 Complex operator +(const Complex& num1, const Complex& num2){

 Complex temp;

 temp.real = num1.real + num2.real;
 temp.imag = num2.imag + num2.imag;
 
 return temp;
}

Complex operator -(const Complex& num1, const Complex& num2){

 Complex temp;

 temp.real = num1.real - num2.real;
 temp.imag = num1.imag - num2.imag;

 return temp;
}

 Complex operator /(const Complex& num1, const Complex& num2){

 Complex temp;

 temp.real = (num1.real*num2.real + 
num1.imag*num2.imag)/(num2.real*num2.real + num2.imag*num2.imag);
 temp.imag = (num1.imag*num2.real - 
num1.real*num2.imag)/(num2.real*num2.real + num2.imag*num2.imag);

 return temp;
}

Complex::~Complex(){

}

Thats the header and the cpp files
Last edited by LuciWiz : 24-Apr-2005 at 14:59. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 24-Apr-2005, 14:52
clander clander is offline
New Member
 
Join Date: Apr 2005
Posts: 11
clander is on a distinguished road
CPP / C++ / C Code:
Complex operator ==(const Complex& num)
{

 Complex temp;

 temp.real = num.real;
 temp.imag = num.imag;

 return temp;
}

I am getting several errors with this part of the code
It says it cannt acces the private members, even though I used "friend"
Last edited by LuciWiz : 24-Apr-2005 at 14:59. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #3  
Old 24-Apr-2005, 21:08
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
Quote:
Originally Posted by clander
I used "friend"
no you didn't. look over the friend declarations. you are mixing up member and non-member functions. all of your functions (==, +, -, *, /) seem like they should be implemented as member functions. and in your second post (the one i quoted) you showed me a member function (although you omitted the Complex:. all the friend declarations are for non-member functions. Also, your operator == function is incorrect. what you are doing is what the operator = function should be doing (assignment). == is for testing equality:
CPP / C++ / C Code:
bool Complex::operator== (Complex &other_one)
{
if (this->real == other_one.real) && (this->imag == other_one.imag) return true;
else return false;
//or whatever. this is simple test of private member equality.
}
the above is implemented as a member function, btw.

here is a great reference on operator overloading:
http://www.csse.monash.edu.au/~jonmc...html/text.html
other references can easily be found using google.
  #4  
Old 25-Apr-2005, 10:12
clander clander is offline
New Member
 
Join Date: Apr 2005
Posts: 11
clander is on a distinguished road
I am also getting other errors with my use of the << and >> operators

It says that there is no operator found which takes a right hand operand of type Complex

I did also put in the Complex:: and it gave me errors for that when I took them out the errors went away which I thought was unusual but I looked in a book and they didn't have it in there either.
  #5  
Old 25-Apr-2005, 10:21
clander clander is offline
New Member
 
Join Date: Apr 2005
Posts: 11
clander is on a distinguished road
Also when I tried to do what you showed as an example it says that there is an error with the arrow and this it says left of ->imag must point to class/struct/union and the same with ->real

also it give me a error saying hat Complex::imag cannot access private member declared in class complex
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.