Question about a code

Hi everyone , i have a few questions about the code that i post below.
1.Well i have to use the overload of the << operator but i can't change some parts of the code :

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
58
59
60
61
62
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Flacon
{
private:
  string nom;
  double volume;
  double pH;
public:
  /*****************************************************
    Complete the code from here
  *******************************************************/
Flacon (string n,double v , double p):nom(n), volume(v), pH(p){}

ostream& print(ostream& out)const;


ostream& etiquette(ostream& sortie) const{
	
	sortie<<nom << " : " << volume << " ml, pH "<< pH ;
	
	return sortie;
	
	}

};
ostream& Flacon::print(ostream& cout)const 
{
	 cout << cout;
     return cout;
	}
	
const ostream& operator<<(ostream& out, Flacon c )
{
     c.print(out);
     return out;

	}

/*******************************************
 * No change the code after this line.
 *******************************************/
void afficher_melange(Flacon const& f1, Flacon const& f2)
{
  cout << "Si je mélange " << endl;
  cout << "\t\"" << f1 << "\"" << endl;
  cout << "avec" << endl;
  cout << "\t\"" << f2 << "\"" << endl;
  cout << "j'obtiens :" << endl;
  //cout << "\t\"" << (f1 + f2) << "\"" << endl;
}
int main()
{
  Flacon flacon1("Eau", 600.0, 7.0);
  Flacon flacon2("Acide chlorhydrique", 500.0, 2.0);
  Flacon flacon3("Acide perchlorique",  800.0, 1.5);
  afficher_melange(flacon1, flacon2);
  afficher_melange(flacon2, flacon3);
  return 0;
}


My questions are:

1 how i use the << operator without call the method etiquette , for exemple if want to print an object of Flacon1 f1
like
cout<<f1;
without pass f1.etiquette(cout);

2. I know that my changes have to been in the function print() , but i don't know how to associate the parametres.

Thanks foryour help.
What should the print() print out?

If you have main():
1
2
3
4
5
6
int main()
{
  Flacon flacon1("Eau", 600.0, 7.0);
  flacon1.print( std::cout );
  return 0;
}

What should you see?
If i do that , i have an infiniteloop , normaly the method print cout is an instance of oestream so, the overload of << operator (todo the printer ) is an external overload , so for that reason we need print / etiquette to have an cces to the atributs of the instance afficher(), but i don't know how to implement this situation.
Your code:
1
2
3
4
5
ostream& Flacon::print( ostream& cout ) const 
{
  cout << cout;
  return cout;
}

has a very big problem. You name the function parameter "cout". Global scope has name "std::cout". The "using namespace std;" has created shortcut "cout". When you then use in your function body "cout", which "cout" do you think that you are referring to and which "cout" do you think that the compiler will refer to? (Unless it resorts to Error: unambiguous.)

Would you write:
1
2
int x = 7;
x = x;


No, Flacon::print has nothing to do with std::cout. It does receive a reference to some output stream (could very well be a file).

I wrote:
What should the print() print out?
should

I did not ask what error your current erroneous code produces. I did ask what that program should print.

Here is an another program:
1
2
3
4
5
6
int main()
{
  Flacon flacon1("Eau", 600.0, 7.0);
  flacon1.etiquette( std::cout );
  return 0;
}

It does print something, doesn't it? Why does it work?
Now, in what way the output of print() should differ from the output of etiquette()?
well i have the fonction etiquetteto show something like this:


ostream& etiquette(ostream& sortie) const
{
sortie<<nom << " : " << volume << " ml, pH "<< pH;
return sortie;
}

If i do what you propose i should see something like this:

Eau :600 ml,pH 7.0

And it's fine but what i need is that i should be capable to see the same think just calling f1

like this for example:
cout << f1;

So i just resolved using the fonction etiquette() in the declaration of the << operator:

ostream& operator<<(ostream& out, Flacon c )
{
c.print(out);
c.etiquette(out);
return out;
}
So i changed the value or return there is not a constant anymore , and i used the method etiquette ().

With this implementation i solved the problem ,now about the use of cout like a parameter well honestly i use this name but i could use another one i mean i don't have any problem with this.
Now , Why i use print() and not only etiquette()?, well because etiquette is a chaine of characters specific but print one by one the values of the members of the class.

But now i have a problem when i printed , i see this :

Si je mélange
"0x6020a8Eau : 600 ml, pH 7"
avec
"0x6020a8Acide chlorhydrique : 500 ml, pH 2"
j'obtiens :
Si je mélange
"0x6020a8Acide chlorhydrique : 500 ml, pH 2"
avec
"0x6020a8Acide perchlorique : 800 ml, pH 1.5"
j'obtiens :


Even if i do only:

cout <<f1;

0x6020a8Eau : 600 ml, pH 7

I don't understand from wher i have the
0x6020a8

???
Please use code tags and output tags to make your post readable:

http://www.cplusplus.com/articles/z13hAqkS/
I just saw my error , it's true that i don't need the method print , only if i use etiquette , i solve the problem
Topic archived. No new replies allowed.