[Solved] What is the function of std::cout ? Is it similar to simple cout? [closed]


Oh wow, you are just starting with C++.
So lets start with the std:: part. This is called a namespace. Namespaces are sometimes confusing people, but they are very useful.
A namespace is just little container that can hold classes, enums, sturcts and all the other things in one place.

Why are they useful?
Well, for example, if you are writing a painting program and that program is interacting with graphics, both may contain classes named color and point. If you have no namespace, it would be impossible to do so!

Then comes the cout part. cout is an actual class in the std namespace, kind of like the Color class in above example. So as an example, here is a simplified example of paint and grahpics:

namespace paint {
    class Point {
        private:
           Color color;
           int x, y;
        public:
           Point(Color c, int xx, int yy) : x(xx), y(yy), color(c) {
           }
           // Continued code...
    };

    class Color {
        private :
            int red, green, blue;
        public:
            Color(int r, int g, int b) : red (r), green (g), blue (b) {
            }
            // Continued code...
    };
}

and GUI:

namespace gui {
    class Window {
        private :
            Point position;
            int width, height;
            Color background_color;

        public:
            Window(Point p, int w, int h, Color b) : position (p), width (w), height (h), background_color (b) {
             }
    };

    class Color {
        private:
            Color_type c;
            Point point;

        public
            enum Color_type {
                red=0xff0000, green=0xff00, blue=0xff, white=~0x0, black=0x0
            };  

            Color(Color_type cc, Point p) :c(cc), point(p) { } 
            // Continued code...
    };

    class Point {
        private:
            int x, y;

        public:
            Point(int xx, int yy) : x(xx), y(yy) {
            }
            // Continued code...
    };
}

So, as you can see, you can have namespaces to encapsulate different classes with the same name.
How do you use them is easy:
You just add the namespace’s name in front of it (e.g paint::color or gui::color). Here is an example:

int main (void) {
    paint::Point paint_p(Color(2,2,2), 55, 33);
 p(55, 33);

    // or

    paint::Color paint_c(0, 0, 0); // Produces black;
    gui::Color gui_c(gui::Color_type::black);
}

To sum it up <something> is class name or variable name, or perhaps a nested namespace (a namespace inside a namespace), but in case of cout, it is the name of a class.

I hope this explains it.

2

solved What is the function of std::cout ? Is it similar to simple cout? [closed]