001. cpp primer快速入门

最简单的c++程序:

#include<iostream>
using namespace std;
int main()
{
    std::cout << "hello,world!" << std::endl;
    system("pause"); 
    return 0;
}

注解:

  1. cout定义在头文件中iostream中。cout相当于计算机的screen,相当于把“hello,world!”丢给计算机的屏幕。
  2. cout在名称空间std里面,就想一个文件在某个文件夹里。std相当于是一个文件夹的名字,cout相当于是一个文件的名字。cout是名称空间std里面的一个对象。
  3. std::cout,std后面4个点叫做作用域操作符。
  4. endl就是endline的缩写。

运行结果:

#include<iostream>
using namespace std;
int main()
{
    //cout << "hello,world!" << endl;
    system("pause"); 
    return 0;
}

 0返回给谁呢?返回给windows,告诉windows,我的程序正常运行,正常结束了。

 下面输出一个变量v1:

#include<iostream>
using namespace std;
int main()
{
    int v1;
    v1 = 12;
    cout <<"v1="<< v1 << endl;
    system("pause"); 
    return 0;
}

下面在main.cpp中利用键盘动态输入变量v1:

#include<iostream>
using namespace std;
int main()
{
    int v1;
    cout << "Enter a integer number:" ;
    cin >> v1;
    cout << "v1=" << v1 << endl;
    system("pause");
    return 0;
}

 下面main.cpp中利用键盘动态输入2个数:

#include<iostream>
using namespace std;
int main()
{
    int v1,v2;
    cout << "Enter two integer number:" ;
    cin >> v1;
    cin >> v2;
    cout << "v1=" << v1 << endl<< "v2=" << v2 << endl;
    system("pause");
    return 0;
}

 注解:

cin>>中的“>>”叫做输入操作符。

 下面开始学习“类”。

类可以写在main文件中,也可以写在头文件中,一般写在头文件中。

包含自己写的头文件的范式:

#include"dog.h"

包含库的头文件的范式:

#include<iostream>

即:不用写后缀.h了,且用尖括号。

原文地址:https://www.cnblogs.com/yibeimingyue/p/13379919.html