• 0

function overloading help?


Question

hi i'm an IT student taking an intro c++ class and my professor assigned this program for us to "Fix" but he didn't explain what any of the codes in them actually are... um... yeah. this might be the worst class i've ever taken. i've been studying the book 6 hours a night to try to teach myself... as a last ditch effort before i have to drop the class i thought i'd come here for help.

can ANYONE, ANYONE help me with this program? can you fix it for me so i can just see how it's supposed to be done? thanks for any help at all.

------------------------------------------------------

Implementation of complx class

Problem Statement

In this exercise, you will define a complex data type complx, to manipulate complex numbers: a+bi. In complx class where you can declare complex data, input/output complex data and check if two complex numbers equal each other or not using the same operators used for other built-in data types.

You need to partially implement a few functions (in bold) in class complx. This exercise is a supplement and preparation to the Assignment.

-----------------------------------------------------

// Complx.h

#include <iostream>

#include <fstream>

using namespace std;

class complx

{

double real,

imag;

public:

complx( double real = 0., double imag = 0.); // constructor

~complx(); // destrcutor

friend int operator== (const complx&, const complx&);

//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;

}

};

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

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

extern ostream &operator << ( ostream &out_file, complx &number );

--------------------------------------------------------

The following code implements two functions in the class. You need to implement the rest of the operators (in ?).

--------------------------------------------------------

// Complx.cpp

#include "complx.h"

complx::~complx() {

?

}

ostream &operator<< ( ostream &out_file, complx &number )

{

?

}

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;

}

extern istream &operator >> ( istream &in_file, complx &number )

{

?

}

// define constructor

complx::complx( double r, double i )

{

real = r; imag = i;

}

int operator== (const complx& a, const complx& b){

?

}

-------------------------------------

Submission

1. Run the following main program using the input sequence

(2,3) (6,7) (6,7) (4,5) (2,3)

----------------------------------------------

//call_complx.cpp

#include "complx.h"

int main()

{

int i=0;

complx c1, c2(3,5),in[5];

cout<< "Please input a complex number in the format of (a,b) - " << endl;

while ((cin >> in)&& (i<4)){

cout << in << endl;

i++;

}

if (c1 == in[0] )

cout << c1 << " equals to " << in[0] << endl;

else

cout << c1 << " not equal to " << in[0]<< endl;

if (in[1] == in[2] )

cout << in[1] << " equals to " << in[2] << endl;

else

cout << in[1] << " not equal to " << in[2]<< endl;

char c; cin >> c;

return 0; //successful termination

}

----------------------------------------------

Okay seriously, can anyone help? I've never had problems with any other programming classes and have a 4.0 in college so far. He's just NOT teaching.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

It doesn't actually look like you need to add too much code.

From what I can gather:

ostream &operator<< ( ostream &out_file, complx &number ) - Outputs the complex number to a file, and returns the file handle

extern istream &operator >> ( istream &in_file, complx &number ) - Inputs a a complex number from a file and returns the file handle.

int operator== (const complx& a, const complx& b) - Compares two complex numbers and returns if they are equal.

There's some more info here: http://www.csse.monash.edu.au/~jonmc/CSE23.../html/text.html It even has a complex number class near the bottom.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.