basic understanding of `Std:: cout <<”hello world”;` in c++

Han sang yeob·2021년 9월 20일
0

C++

목록 보기
1/1
post-thumbnail

Std::

In short,

  • Example of namespace
  • Namespace is grouping of code

    Let say we have 2 people in the room, Tony and Sarah.Tony made a sweet function for all of people in the room.We have Sarah and she say “my function is better then yours.” And Tony, unfortunatly, named his function exacully same as Sarah. but Tony still want make his function to be used.
    So, we have naming conficit in thie room. What happenes if other people try to import these functions to their project? How should they call Tony’s function and Sarah’s function?

The solution of the is name spaces. So,

1) Tony:: cout 

2) Sarah:: count

This is purpose of nameing spaces. It allows others to

  1. Group and organize our code
  2. Prevents naming conflicts

All of Tony’s code can be in his container and All Sarah’s code can be in her container. So back to the code,

Std:: cout<< “Hello world”;

This cout is coming from std(standard) namespace.

But if we put,
using namespace std; on the top,
it will allows us to delete std::: from the state. This will make our life more easy but it is best practice not to use using namespace std; in a huge application due to naming conflicts.

cout

This is object. It is tool to reach the console.
But we have to know that for example,
Class is a cookie cutter and object is cookie made from the cookie cutter.
Each of cookie(in here object) will be delivered to us and we will eat them.

So cout is the cookie, then where is cookie cutter(class)?

It is from ostream.

<<

<< is operator. Operator work in operand.In here cout and hello world! is operand.

profile
Learning IT in Vietnam

0개의 댓글