0

I need a help with understanding the following:

I overloaded << operator. I wrote a test program. I didnt include the "using namespace std" code. But the program worked out well.

#include <iostream>
#include "fraction.h"

//using namespace std;

int main(void)
{
//constructing two fractions
    Fraction a(4, 2);
    Fraction b(17, 11);
//modifying them that is entering a fractions from keyboard
    cin>>a;
    cin>>b;
//computing product and quotient and printing them using cout
    cout<<a*b<<endl;
    cout<<a/b<<endl;
}

But as you can see I used "endl" which is from standard namespace. Can you explain me the "paradox" I m getting here.

P.S I didnt include .h and .cpp file because I think they are irrelevant.

zneak
  • 134,922
  • 42
  • 253
  • 328
L.G
  • 29
  • 5
  • 5
    `fraction.h` probably has `using namespace std;` – clcto Mar 24 '14 at 21:41
  • 1
    I bet fraction.h includes `using namespace std;` – Eric Fortin Mar 24 '14 at 21:41
  • As others said, `fraction.h` probably has the line `using namespace std;`, which is bad practice. See [this SO article](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?rq=1) as to why. – Gareth Mar 24 '14 at 21:44
  • Yes it does, I checked the lihk. I am using ostream and istream to declare and overload << and >>. It doesnt work when I do std::ostream. What is an alternative if including namespace entirely is considered a bad habit? – L.G Mar 24 '14 at 21:51
  • 1
    Use it only in cpp files where you know there won't be name clash. – Eric Fortin Mar 24 '14 at 21:52

3 Answers3

1

A compiler works with translation unit. It takes a .cpp and then insert .h text inside the translation unit. If your header file includes using namespace std;, it will effectively be present in the code the compiler tries to compile.

This is a bad practice as your are shoving namespace using down your user throat and can induce name clashes if other namespace use the same name.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
  • Thank you for clear explanation.If I use it .cpp file, and remove using namespace std in .h file, I get the following problem. When declaring overloaded operator in .h I have to use ostream, but since std namespace is not included ostream "does not name a type",which gives me an error – L.G Mar 24 '14 at 21:57
  • @user3501: Write `std::ostream` instead. – Mooing Duck Mar 24 '14 at 21:58
  • What @MooingDuck said. – Eric Fortin Mar 24 '14 at 21:58
  • 1
    cool, I figured it out, I included , then std::ostream worked out. Thanks a lot for help! – L.G Mar 24 '14 at 22:03
0

If you can use cin and cout, and the problem is using endl, then your fraction.h file is most likely including those dependencies either in one of two ways

using std::cin;
using std::cout;
using std::endl;

or

using namespace std;
Claudiordgz
  • 3,023
  • 1
  • 21
  • 48
0

When you #include something, is does a straight text replacement with the file. Since you #include "fraction.h" and it probably has the line

using namespace std;

the main file also uses std.

Also note, it is not good practice to using namespace std; in header files since everywhere else that includes the header will also automatically use std which can cause conflicts if the programmer is unaware.

clcto
  • 9,530
  • 20
  • 42