第三章对象、类型和值

对象:用来保存一个指定类型值的一些内存单元。

类型:定义一组可能的值与一组运算(对于一个对象)。

值:根据一个类型来解释的内存中的一组比特。

#include <iostream>
using namespace std;
int main()
{
        cout << "Please enter your first name and age\n"<< endl;

        string first_name;
        int age;
            
        cin >> first_name; 
        cin >> age;
        cout << "Hello,"<< first_name << "(age" << age <<")\n"<< endl;

        return 0;
}

这里我曾犯了个错误。我是这么写的(想当然了),结果g++了一堆错误提示。

cin >> first_name >> endl;
cin >> age >> endl;

 “字符串+”意味着“连接”,也就是说:

当S1 和 S2是字符串时, S1+S2也是字符串,包含来自S1的字符后接来自S2的多个字符。

例如:S1的值为“Hello",S2的值为”World”,那么S1+S2的值为“Hello World”

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6         cout << "Please enter your first ande second names:\n"<< endl;
 7     
 8         string s1;
 9         string s2;
10 
11         cin >> s1 >> s2;     //read two strings
12 
13         string name = s1 +''+ s2; //concatenate strings
14 
15         cout << "Look,"<< name <<'\n';
16 
17         return 0;
18 
19 }

g++报错提示:13: error: empty character constant

string name = s1 +' '+ s2; //单引号中间应有空格

 

posted on 2019-07-11 17:37  charons  阅读(354)  评论(0编辑  收藏  举报