C++ primer review (1, input and output)

1.1, hello world program

#include <iostream>
#include <windows.h>
int main()
{
    
    
    std::cout<<"hello world!";
    system("pause");
    return 0;
}

1.2, first acquaintance with input and output

The iostream library contains two basic types, istream (input stream) and ostrean (output stream).
istream:cin
ostream:cout, cerr (output error and warning information), clog (output general information about program operation).
Note that I only wrote examples, but cerr and clog are not used to print general information.

#include <iostream>
#include <windows.h>
int main()
{
    
    
    int a, b;
    std::cout<<"请输入a,b其中a需要比b大";
    std::cin>>a>>b;
    if(a <= b) std::cerr<<"a小于等于b";
    else std::cout<<"a大于b";
    std::clog<<"程序运行完成";
    system("pause");
    return 0;
}

The special value endl of the operator: printing endl looks like a new line, but actually ends the current line and flushes the contents of the buffer (temporary data storage area) associated with the device to the device. (endl is also in the std namespace) As
you can see, I use a std:: for the input and output stream operations above, which can help us explain that the cin and cout we use are defined in the std namespace, and can be used using namespace std; means that all the names we use come from the std namespace.

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    cout<<"今天是周日"<<endl;
    cout<<"明天要上健美操"<<endl;
    system("pause");
    return 0;
}

1.3 Introduction to annotations

Comment on one line: //Content
Comment on multiple lines: /* Content*/

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    cout<<"今天是周日"<<endl;//可以多睡一会
    cout<<"明天要上健美操"<<endl;
    /*
    太难了
    */
    system("pause");
    return 0;
}

1.4, control flow

Use of while

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    int a = 0;
    string answer1,answer2;
    while(a < 10){
    
    
        cout<<"今天还喜欢她吗"<<endl;
        cin>>answer1;
        if(answer1 == "不") break;//结束了
        cout<<"是否过了一天"<<endl;
        cin>>answer1;
        if(answer1 == "不") continue;//还继续爱她但是今天没过
        a++;
        cout <<"这是爱你的第"<<a<<"天"<<endl;
    }
    system("pause");
    return 0;
}

Use of for loop

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    string answer1,answer2;
    for(int i = 0;i < 10;){
    
     //这里可以写成i = 0;i < 10;i++但是如果逻辑不适合在这里写,空出来就行。
        cout<<"今天还喜欢她吗"<<endl;
        cin>>answer1;
        if(answer1 == "不") break;//结束了
        cout<<"是否过了一天"<<endl;
        cin>>answer1;
        if(answer1 == "不") continue;//还继续爱她但是今天没过
        i++;
        cout <<"这是爱你的第"<<i<<"天"<<endl;
    }
    system("pause");
    return 0;
}

Read an indefinite amount of input data (can be ended with ctrl+z)

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    int a;
    while(cin>>a){
    
    
        cout<<"输入的是"<<a<<endl;
    }
    system("pause");
    return 0;
}

if statement

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    int a;
    cin>>a;
    if(a > 0) cout<<"a大于0";
    else cout<<"a不大于0";
    system("pause");
    return 0;
}

1.5, class introduction

For example, people are a class, and they have several attributes such as height, weight, etc., people can eat, study, and so on. Behaviors can be stored in the class as functions, and attributes can be stored in the class as variables.
I don't like the C++ primer example very much. I didn't understand it when I first saw it in my sophomore year. I wrote an example of Apple myself.

#include <iostream>
#include <windows.h>
using namespace std;
class apple{
    
    
public:
int wight;
string name;
apple(int w,string n){
    
    
    wight = w;
    name = n;
}
void can(){
    
    
    cout<<"eat"<<"重量"<<wight<<"品种"<<name<<endl;
}
};
int main()
{
    
       apple a(1,"红富士");
    a.can();
    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45743162/article/details/115048609