Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)

 nelsonc 17Oct2009 12:46

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.

-----------------------------------------------------
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();                      // 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 …).

--------------------------------------------------------
Code:

```// 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)

----------------------------------------------
Code:

```//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])&& (i<4)){               cout << in[i] << 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.

 xpi0t0s 17Oct2009 15:27

Maybe there was a pre-requisite to the class, are you already supposed to be able to do this?
Or is it just a "let's see what people can do already"-type assignment. Maybe not having a clue is a valid answer.

Best solution really is to discuss this with the teacher to find out why he's set you an impossible task.

Because if we solve this for you then your teacher will assume you (a) cheated or (b) fixed it yourself, so for the first case he won't give a toss, and for the second then you won't get the teaching you need because he'll think you won't need it, and the next assignment will be harder, and there will be a dependency cycle created because you also won't be able to do that one, and will post it here expecting us to do it for you again.

So: no, I think fixing this for you is probably about the worst thing anyone can do for you right now. Sorry if that seems harsh. Talk to your teacher, or if you really think he's incompetent (and that the problem really isn't caused by you goofing around) then raise a formal complaint against him.

 nelsonc 17Oct2009 23:38

well when it's an online class and emails get unanswered as well as discussion on the forum. and no one else in the class has any idea, i don't think it's me goofing around, but thanks. there are no prereqs either. this is our 4th program ever. first being hello world. yeah. and filing a complaint isn't really going to help at this point in the semester. i don't need the answer, but maybe a few pointers.

 xpi0t0s 18Oct2009 03:19

OK. Well, I don't think you need anything in the destructor, but can you work out why? operator<< should be easy enough, you just need to output (real,imag) and work out what to return so that multiple << operators can be chained (hint: look at the existing operator>>).

operator >> ( istream &in_file, complx &number ) will I imagine be more or less identical to operator >> ( ifstream &in_file, complx &number ) although you should check if any of the istream operators used differ from the equivalent ifstream operators: if not, then you can more or less lift the code directly.

What do you think operator== should do, based on the name? (This question assumes you know what == does.) Can you work out how to achieve that, and what to return? Look at the return type for a hint, and think of what values might be appropriate in what circumstances.

 nelsonc 18Oct2009 03:45

before i go to the school guns blazing, is it safe to say this should be expected of us this soon? he said we didnt need a book because it's intuitive enough to figure out. well, i bought the book the school says we need anyway and the first few pages of the book state chapters 1-7 must be completed in order because they build on eachother, THEN move on to chapters 9 - 14 in any order... from what i've gathered so far this lesson alone has used 7 chapters worth of work...

 nelsonc 18Oct2009 03:48

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

what is this saying? we haven't been taught any of these codes.

 xpi0t0s 18Oct2009 16:02

It's declaring an overloaded operator >>, which is done in the form of a function that takes two parameters. In the C++ idiom >> is often used for input and << for output.

Have a close look at extern ifstream &operator >> ( ifstream &in_file, complx &number ) and see what differences you can spot between:
extern ifstream &operator >> ( ifstream &in_file, complx &number )
and
extern istream &operator >> ( istream &in_file, complx &number )

He's already given you the code for the first, and the code for the second will be VERY SIMILAR (hint).

Question, if I gave you the following code, would you be able to complete it?
Code:

```int main() {   int re=2, im=3;   // the next line displays re and im (but is incomplete)   cout  // ... then what?   return 0; }```
If you're not sure, have another look at the "hello world" program.

What book, out of interest?

 nelsonc 19Oct2009 05:17