如何从面向过程到面向对象中的第一个问题就是从一个数组或者结构引申出类,告诉大家使用类这个新的概念有什么用,
明天就用char []到string作一次引申。例子如下:
#include <iostream>
#include <numeric>
#include <cstring>
using namespace std;
void PrintAttribute(string &str)
{
cout<<"size: "<<str.size()<<endl;
cout<<"length: "<<str.length()<<endl;
cout<<"capacity: "<<str.capacity()<<endl;
cout<<"max size: "<<str.max_size()<<endl;
}
int main()
{
char cs1[]="test cs1";
char cs2[]="test cs2";
char *cs3=new char[strlen(cs1)+1];
strcpy(cs3,cs1);
cs1+=cs2;
cout<<cs1<<endl;
cout<<cs2<<endl;
string s1;
PrintAttribute(s1);
cout<<"================================"<<endl;
s1="My String object";
cout<<s1<<endl;
string s2;
s2="My String2 object";
cout<<s2<<endl;
s2+=s1;
cout<<s2<<endl;
PrintAttribute(s1);
}
//通过比较cs1+=cs2和s2+=s1这两种形式的区别,看出有了类这个概念之后,代码的编写方式更加的灵活。