2

I have started learning C++ and I think the language is great, but few things are baffling me while I am on my path learning it. In this example:

cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint);

In this example why do we type the whole setiosflags(ios::...) when the program still does the same if I only type showpoint without setiosflags?

Second question I have is simple. If we have the following:

int x=0;
cin>>x;

Why do we define a value for int if we later change it to something different than 0?

Craig W.
  • 17,838
  • 6
  • 49
  • 82
Nikolay
  • 205
  • 1
  • 3
  • 12
  • 1
    You don't need to initialize `x` if you're going to change it to something else before using it. Some people just like to initialize all variables out of habit. – Barmar May 18 '15 at 19:29
  • 1
    Question #2 depends on whether you're pre-C++11 or post. – Kerrek SB May 18 '15 at 19:30
  • Regarding the first question, it's because [`std::showpoint` is a standard manipulator](http://en.cppreference.com/w/cpp/io/manip). As for the second question, see e.g. [this old answer of mine](http://stackoverflow.com/questions/13378989/why-does-stringstream-change-value-of-target-on-failure/13379073#13379073). – Some programmer dude May 18 '15 at 19:30

3 Answers3

1

why do we type the wholesetiosflags(ios::...)when the program still does the same if I only type showpoint without setiosflags?

We don't, unless we want the program to be more verbose than necessary. As you say, streaming setioflags with a single flag is equivalent to streaming the flag itself. You might use setioflags if you have a pre-computed set of flags you want to set.

Why do we define a value for int if we later change it to something different than 0?

Again, we don't, unless we like unnecessary verbiage. But it's a good habit to initialise variables, to avoid undefined behaviour if you later change the code to assume it has been initialised.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

Its optional and flexibility language provide, so either you can set manipulators using setiosflgas or as showing below:

float y= 1.45;
std::cout << std::fixed<<std::showpoint<<y;

Why insisting to initialize variables is because before C++11 these uninitialized variables can hold garbage value until you set value for them. And it may create unwanted issues and bugs. So better practice always initialize variables when you define it.

Since C++11 all fundamental data types are initialized to zero if you use explicit constructor as follows:

int i2 = int(); // initialized with zero 
int i3{}; // initialized with zero (since C + + 11)
Steephen
  • 14,645
  • 7
  • 40
  • 47
-1
  1. The stream manipulator std::setiosflags(ios_base::fmtflags mask)- is a function that sets the format flags specified by parameter mask. It can be used for multiple flags simultaneously, by using binary AND : &. It probably exists to provide full/complete functionality of the class that belongs to. Now regarding your question:

If you can access a flag(member) directly, why bother using a function(setter)?

I can't think of any reason why you shouldn't. However have in mind that manipulators are global functions and these constants, ios_base::fmtflags, are member constants. For more information on manipulators check this.

  1. Regarding the second question: you initialize a variable when you define it to avoid undefined behaviour in case you use it, by mistake, before assigning it any value. Local variables need initialization, global variables are initialized by default.
Ziezi
  • 6,375
  • 3
  • 39
  • 49