c ++ learning the basics of learning day01

A code sample analysis:

1 #include <iostream>
2 int main()
3 {
4     using namespace std;
5     cout << "come up and C++ me some time.";
6     cout << endl;
7     cout << "you are right!"<<endl;
8     return 0;
9 }

operation result:

come up and C++ me some time.
you are right!

Description: Here is using namespace std namespace, which is to allow write large programs and combine multiple vendors with existing code, but also for unified management. But if we do not use the name space, then the above code is usually something like this:

#include <iostream>
int main()
{
    //using namespace std;
    std :: cout<<"come up and C++ me some time.";
    std ::cout << std::endl;
    std ::cout << "you are right!"<<std::endl;
    return 0;
}

Operating results and is the same as the first example, generally when doing large-scale projects will use this style of writing code, we usually use more learning is the use of using compiler directives, it can be used inside a namespace has been defined name, and would not write code, is the first example of such code style a bit lazy, I do not recommend such an approach. cout << "you are right!", where "<<" represents this string is sent to cout; endl is a control character, meaning it represents a new line, and newline "\ n" type action, but he also located in the header file iostream,

Note: Just above the "<<" represents the insertion operator, and as this little bit left shift operator, but their role is different, this is the concept of operator overloading, is a transport operator, by overloading, containing different the functional description.

Next we look at an example:

#include <iostream>
int main()
{
    using namespace std;
    int carrots;
    
    cout << "how  many carrots do you have ?"<< endl;
    cin >> carrots;
    cout<< "here you are two more.";
    carrots=carrots + 2;
    cout << "now you have "<<carrots<<" carrots."<<endl;
    return 0;
}

operation result:

how  many carrots do you have ?
4
here you are two more.now you have 6 carrots.

Description: Here we mainly to learn about the use of cin, cin >> carrots, this statement is mainly the value we assign to the input variables carrots,

 

Guess you like

Origin www.cnblogs.com/1121518wo/p/11077004.html