You are here:

C++/How to output read info.


Hi Sam

I am beginner to C++. I used to do java programming, which doesn't deal with pointers (just references). This is where I am confused.

I made some C++ classes for defining different shapes (circle, rectangle and triangle).

I implemented a function that reads shapes from the user's input, and then recognises the shape entered by the user.

It's clear that this is cannot be achieved until my program have read at least some of the input (note: ignore input errors).

I managed to make this function (below if u wanna c), but all what I need to do now is output what has been read by the program. Here I am confused with pointers and references and these stuff.

If my code will be tested using the following lines:

  cout << "Please enter a shape:" << '\n';
  shape *s = (shape *) 0;
  cin >> s;   
  cout << *s << '\n';

I need to get this part right, because this is what causing me the problems.

Since 'cin >> s;' is used then my >> overloading method must take the arguments:
istream& operator>>(istream &s, shape *shp) {...}
And then among the code should have 'shp = & variable;' to ensure that the original s in 'cin >> s;' is modified accordingly. Am I right in this part?

Then what I will need to do now to output s in 'cout << *s << '\n';' is overloading << operator, but since  *s is used then my operator<< function should have the following arguments:

/*Define the output operator for shape*/
ostream& operator<<(ostream &s, const shape &shp) {...}
Note: I used 'const shape &shp' since in outputting the value will not be changed.

I feel there's something missing. Is the changes made by the:
istream& operator>>(istream &s, shape *shp) {...)
Will be output using 'cout << *s << '\n';' through ostream& operator<<(ostream &s, const shape &shp) {...}

Please help me to understand the usage of pointers and references when overloading I/Oput operators. And inform me of any mistake or misunderstanding of the above procedure.

//<shape::circle (0,0) 1 >
//<shape::rectangle (0,0) (1,1) >
//<shape::triangle (0,0) (0,1) (1,0) >
  istream& operator>>(istream &s, shape *shp) {
     char left_tag, colon, left_bracket, comma, right_bracket, right_tag;
     string shape_name_string;
     double x1, y1, x2, y2, x3 ,y3,z;      
     if ((s >> left_tag) && (s >> shape_name_string) && (left_tag == '<')) {
        if (shape_name_string == "shape::rectangle") {
         if ((s >> left_bracket >> x1 >> comma >> y1 >> right_bracket >> left_bracket >> x2 >> comma >> y2 >> right_bracket >> right_tag) && (comma == ',' && right_bracket == ')' && right_tag == '>')) {
         rectangle rec(x1,y1,x2,y2);          
         shp = &rec;
         //shp-> draw();          
        } else
         if (shape_name_string == "shape::triangle") {
         if ((s >> left_bracket >> x1 >> comma >> y1 >> right_bracket >> left_bracket >> x2 >> comma >> y2 >> right_bracket >> left_bracket >> x3 >> comma >> y3 >> right_bracket >> right_tag) && (comma == ',' && right_bracket == ')' && right_tag == '>')) {
         triangle tri(x1,y1,x2,y2,x3,y3);         
         shp = &tri;          
         //shp-> draw();
        } else
         if (shape_name_string == "shape::circle") {
         if ((s >> left_bracket >> x1 >> comma >> y1 >> right_bracket >> z >> right_tag) && (comma == ',' && right_bracket == ')' && right_tag == '>')) {
         circle cir(x1,y1,z);          
         shp = ○
         //shp-> draw();
     return s;

1. No, when you define a function as istream& operator>>(istream &s, shape *shp), shp cannot be changed inside. If you want to do that, define it as: shape*&shp
2. Yes. You can also use "cout << s" and then define the operator overloading function as: ostream& operator<<(ostream &s, const shape *shp). This way, you ignore * before s.


All Answers

Answers by Expert:

Ask Experts




You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.


I'm a professional programer in C++ and network/internet programming.

©2016 All rights reserved.