C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 20th, 2012, 12:24 AM
BXisAWOL BXisAWOL is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 1 BXisAWOL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 25 sec
Reputation Power: 0
Implementation of complx class?

Basically I need to implement a class, complx, to manipulate complex numbers: a+bi.


Here's what I have for the 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* (complx); // operator*()

bool operator== (complx); // operator==()


//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 istream &operator >> ( istream &in_file, complx &number );

extern ifstream &operator >> ( ifstream &in_file, complx &number );

complx &operator + (double, complx);
complx &operator - (double, complx);
complx &operator * (double, complx);






And then here's what I have for the Complx.cpp file:


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 )
{
out_file << '(' << number.Real() << ',' << number.Imaginary() << ')';
return out_file;
}

// define constructor
complx::complx( double r, double i )
{
real = r; imag = i;
}

// define overloaded + (plus) operator
complx complx:perator+ (complx c)
{
complx result;
result.real = (this->real + c.real);
result.imag = (this->imag + c.imag);
return result;
}






Lastly this is what I have to call the class:

Code:
//call_complx.cpp
#include "complx.h"
ifstream infile ("in.dat");
int main()
{
int i=0;
complx in[7];
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:perator+()
complx s2 = d + in[2]; // overload operator+()
complx s3 = in[3] + d; // overload operator+()
complx a = in[4] - in[5];
complx mm=in[3]*in[4];
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 " << a <<endl;
cout << "The product is a complex number " << mm <<endl; 
cout << "The subtract is a complex number " << b <<endl;
cout << "The product is a complex number " << dm <<endl;
if (in[4] == in[5]) cout << "in[4] and in[5] are the same " << endl;
system("pause");
return 0; //successful termination
}




Can anyone please help me complete this?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Implementation of complx class?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap