C++专题之string和char *的简单比较使用

来源:互联网 发布:mysql安装配置 编辑:程序博客网 时间:2024/04/27 17:51

转载请注明出处:http://blog.csdn.net/feng1790291543


当编写C++类,使用string时,我们注意到,在头文件处,我们要#include<string> (不加.h,如果加了,就成了给C语言加头文件了),别忘了,还有using namespace std;不然就会出现很多错误,这可能是初学者容易犯的错误,下面附上代码。

Student.h

// Student.h: interface for the CStudent class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_STUDENT_H__96794BEB_7938_416F_A48A_1E028AE6E7D4__INCLUDED_)#define AFX_STUDENT_H__96794BEB_7938_416F_A48A_1E028AE6E7D4__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include <string>using namespace std;class CStudent  {public:int number;string name;  //避免使用* 去分配内存,此时的name的长度是无限的,克服*name的缺陷    //char *name;  CStudent();virtual ~CStudent();CStudent(int number,string name);void print_student();    CStudent(CStudent &stu);CStudent &operator=(CStudent &stu);};#endif // !defined(AFX_STUDENT_H__96794BEB_7938_416F_A48A_1E028AE6E7D4__INCLUDED_)

Student.cpp

// Student.cpp: implementation of the CStudent class.////////////////////////////////////////////////////////////////////////#include "Student.h"#include <iostream>#include <string>#include <stdlib.h>using namespace std;//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CStudent::CStudent(){}CStudent::~CStudent(){}CStudent::CStudent(int number,string name){    this->number=number;this->name=name;return ;}void CStudent::print_student(){cout<<"number is "<<this->number<<" name is "<<this->name<<endl;return ;}CStudent::CStudent(CStudent &stu)               //拷贝构造函数{this->number=stu.number;this->name=stu.name;                     //深复制   ----->string name;   本身就是相当于数组,故直接将字符串复制了    //this->name=stu.name;                       //浅拷贝   --->char *name;   只是复制指针的本身,而没有复制指针指向的目标    //this->name=new char[strlen(stu.name)+1];   //char *name;  ---->解决浅拷贝,将指针的目标复制了//strcpy(this->name,stu.name);                return ;}CStudent &CStudent::operator=(CStudent &stu)   //当string name;的时候的情况分析------->赋值函数,运算符重载 {    if(&stu==this){        return *this;      }this->name=stu.name;                       //明显不用分配内存,而直接进行深复制  this->number=stu.number;                   return *this;}/*CStudent &CStudent::operator=(CStudent &stu)   //当char *name;的时候的情况分许------->赋值函数,运算符重载 {    if(&stu==this){        return *this;}this->number=stu.number;         char *name_bak=new char[strlen(stu.name)+1]; //分配内存,还要备份name的值,避免空间分配失败    if(name_bak==NULL){return *this;}name_bak=this->name;free(this->name);                           //释放原有的空间,使其指向新的内存区域this->name=name_bak;strcpy(this->name,stu.name);                //深拷贝,解决复制问题,return *this;}*//*********************************************    *  比较上述的string 和char *,可以知道       **  string 比较方便实用,减少了很多繁琐的问题 **                                            * *********************************************/

main.cpp

#include "Student.h"   #include <iostream>#include <string>#include <stdlib.h>//带.h的是C语言中的头文件using namespace std;int main(){    string nu="快跑,草泥马!";cout<<nu<<endl;CStudent stu(1001,"孙权");stu.print_student();CStudent stu2(1002,"草泥马");        stu2.print_student();stu.name=stu.name+"——"+stu2.name;//字符串连接stu.print_student(); CStudent  stu3(stu2);             //拷贝构造函数使用stu3.print_student();CStudent stu4;        stu4.operator =(stu);            //赋值构造函数使用stu4.print_student();return 0;}


运行效果如下:



0 0