-2

i have named this as next.h and included in main.cpp snippet

i have created two libraries one for the main function and the other just for having classes i wanted to practise for OOPS so i suddenly thought of using namespaces to know their full potential but i am getting confused between why it isnt working as intended

#include<bits/stdc++.h>
namespace custom
{
class aries
{
    public:
    int data;
    
};
  }

 namespace custom2
 {
class aries
{
    public:
    double data1;  
    
};

 }


#include<bits/stdc++.h>
#include "next.h"
using namespace std;
using namespace custom;
using namespace custom2;

 int main()
{
custom2::aries a;
a.data1=5.000;
cout<<a.data1;

return 0;
}

The output for the following program is as follows:

5

...Program finished with exit code 0
Press ENTER to exit console.

My question is it should have been 5.000 but why it is int type and not a double type ?

sweenish
  • 4,793
  • 3
  • 12
  • 23
John Mellow
  • 116
  • 5
  • 4
    do not: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – RoQuOTriX Sep 30 '21 at 13:03
  • 3
    This has nothing to do with namespaces or classes. Remove all of that code. `double a = 5.000; std::cout << a << '\n';` will do exactly the same thing. The default formatting suppresses trailing zeros. Change the value to 5.1 and you'll see more digits. – Pete Becker Sep 30 '21 at 13:04
  • 3
    First of all, please [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). That something you never should have learnt about from the beginning. – Some programmer dude Sep 30 '21 at 13:04
  • 1
    Secondly, [`using namespace std;` is a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), and depending on your design so is `using` any other namespace as well. – Some programmer dude Sep 30 '21 at 13:05
  • ah got it , thanks alot my mistake but why not it has alot of containers i can use for my OOPS project like for a taxi booking application i can use hashmap to map the car with a particular user...right ? – John Mellow Sep 30 '21 at 13:07
  • so how namespaces are a big no no to use ? but in what other way can i have similar functionality as of such ? – John Mellow Sep 30 '21 at 13:08
  • "Why not?" - Read the (multiply) linked question. – StoryTeller - Unslander Monica Sep 30 '21 at 13:08
  • 1
    5.0000000000 = 5 – L. Scott Johnson Sep 30 '21 at 13:09
  • *Use* namespaces, that's alright, but `using namespace ...;` needs thought. And as I already linked, `using namespace std;` is usually not very good at all. – Some programmer dude Sep 30 '21 at 13:15
  • namespaces are excellent! But "using namespace" in code is less so because you easily can get nameclashes (like you have with aries). Just remove those lines and get used to explicitly type namespaces. That way you will also not run into problems when you add an include of someone elses code, even if they also have an aries class. – Pepijn Kramer Sep 30 '21 at 13:15
  • i don't understand "use namespaces explicitly" is it like , namespaces within same file ? – John Mellow Sep 30 '21 at 13:20
  • #define print(a) std::cout< – John Mellow Sep 30 '21 at 13:29
  • 1
    `using std::cout;` is okay (in a `*.cpp` source file, not in a `*.h` header file). `using namespace std;` is not recommended. – Eljay Sep 30 '21 at 13:51

1 Answers1

0

Okay, just summing up the comments:

  • don't use the #include <bits/stdc++.h>. Instead, just include every header you need to use in your code separately. Reason: bad code readability and not every compiler supports this directive. More: Why should I not #include <bits/stdc++.h>?
  • also don't use the using namespace *name* command. For clarity: you can use namespaces, just don't use using, use *namespace name*:: instead, like std::cout. Reason: bad code readability, also you can mess up with names (like when you have function_name function and the namespace also has a function with the same name, then the compiler will not know which to use (or you get an error).
  • answer to your question: std::cout omits zeros while printing numbers so 5.00 will be just 5, but 5.278 will be 5.278.
esperor
  • 47
  • 10