string
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main(void)
{
string name;
cout << "please input uername:";
getline(cin,name);
if(name.empty())
{
cout << "input is empty"<<endl;
return 0;
}
if(name == "admin")
{
cout << "you are an adminstrator"<< endl;
}
cout << "hello " +name << endl;
cout << "username length is "<< name.size()<<endl;
cout << "The first words of username is "<< name[0] << endl;
return 0;
}
属性封装
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
private:
string m_strName;
};
int main()
{
Student *str = new Student;
str->setName("learn c++");
cout << str->getName() ;
delete str;
str = NULL;
return 0;
}