Lesson1
新的开始!
the first C++ code
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}
cout流对象,<<把要输出的内容扔给cout对象。
cin就是输入流对象,>>把输入的内容扔给cin对象。
C语言是贴近底层逻辑的,而C++则是向下兼容C语言的,并且增加了面向对象编程。"a better C"。
the class
接下来就是贴近类的讨论内容了,例如string类,我们#include <string>后,就可以去string str;了。
C的string类有很多不方便的地方,因为它本质就是字符数组,所以我们不能直接cstr2 = cstr1;,但是C++可以。
看下面的代码:
...
string str1 = "hello ";
string str2 = "world";
string str3 = str1 + str2;
str3 = "hello, china";
string str4("hello zju");
string str5(str3,7,5); // china
str3 = "hello china";与string str4("hello zju");,一个是赋值,一个是初始化,有本质区别的。
对于对象加.调用了其中的成员函数,如string str6 = str3.substr(7.5)