How do you get input from a user as an object?

I'm trying to write this program from Deitel and Deitel HW9.5 . It's not required in the HW to get input from the user but I am curious how it is done? I didn't find an example in the tutorial on classes on how to deal with inputting variables.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>


#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H

class ComplexNumber {
private:
    double real;
    double img;
public:
    ComplexNumber(double, double); // two parameter constructor
    ~ComplexNumber(); // destructor
    void addum(ComplexNumber &, ComplexNumber);
    void subum(ComplexNumber &, ComplexNumber);
    void showem(ComplexNumber);
};

#endif 






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using namespace std;
#include "ComplexNumber.h"
#include <iostream>



   

    ComplexNumber::ComplexNumber(double realPart, double imgPart) {
          real = realPart;
          img = imgPart;
       
   }

  ComplexNumber::~ComplexNumber() {


   }

 void ComplexNumber::addum(ComplexNumber & lhs, ComplexNumber rhs) {
	 
	 lhs.real = lhs.real + rhs.real;
     lhs.img = lhs.img + rhs.img;
  }


 void ComplexNumber::subum(ComplexNumber & lhs, ComplexNumber rhs) {

      lhs.real = lhs.real - rhs.real;
      lhs.img = lhs.img - rhs.img;
 }
		  
 void ComplexNumber::showem(ComplexNumber daThing) {
      cout << daThing.real << " " << daThing.img << "i" << endl;
  }


   


    int main() {
	ComplexNumber A(1,2), B(0,0), C(2,3);

	cout << "Enter the first complex number in the form (real,imaginary) sans the i" ;

	cin >> ComplexNumber::ComplexNumber ;

      
      ComplexNumber A(1,2), B(0,0), C(2,3);
      A.showem(A);
      A.addum(A,C);
      A.showem(A);
      A.subum(A,C);
      A.showem(A);
     return 0;

   }



Line 46 is my dumb attempt at it. Please take into consideration this is my first program dealing with classes and objects.
You'll need to provide an overloaded function for
istream operator >> (istream,ComplexNumber);

In your header you'd need to declare it as a friend function to the class and as a function itself. The friend keyword lets the function use the private members of the ComplexNumber class.
1
2
3
4
5
6
7
8
9
class ComplexNumber {
    // Your code

    // We declare it is a friend function
    friend istream& operator >> (istream& in, ComplexNumber& c);
}

// Now we declare the function
istream& operator >> (istream& in, ComplexNumber& c);


Then in your source file, you'd write the code for this. Returning an istream object lets you chain this with other cins.
1
2
3
4
5
istream& operator >> (istream& in, ComplexNumber& c)
{
    in >> c.real >> c.imag;
    return &in; 
}


The above code assumes you enter it with a space between numbers. If you want to enter it as a string, just parse the string in this function.

Now your code at line 46 will work!
Last edited on
Thank you! I will give that a try tonight
Topic archived. No new replies allowed.