C++ 16 拷贝字符串

来源:互联网 发布:软件设计方案 评审 编辑:程序博客网 时间:2024/05/20 02:24

#include <iostream.h>
#include <string.h>
class Csample
{
 char *p1,*p2;
public:
 Csample()
 {
  p1=new char[20];
  p2=new char[20];
 }
 void intit(char *s1,char *s2)
 {
  int k;

  k=strlen(s1);
  for(int i=0;i<k;i++)
  {
   p1[i]=s1[i];
  }
  p1[k]='/0';
  k=strlen(s2);
  for(i=0;i<k;i++)
  {
   p2[i]=s2[i];
  }
  p2[k]='/0';
 }

 void print()
 {
  cout<<"p1="<<p1<<" p2="<<p2<<'/n';
 }
 void copy(Csample &one)
 {
  p1[0]=one.p1[0];
  p2[0]=one.p2[0];

  int k;

  k=strlen(one.p1);
  for(int i=0;i<k;i++)
  {
   p1[i]=one.p1[i];
  }
  p1[k]='/0';
  k=strlen(one.p2);
  for(i=0;i<k;i++)
  {
   p2[i]=one.p2[i];
  }
  p2[k]='/0';


 }
 
 void free()
 {
  delete [20]p1;
  delete [20]p2;
 }
};

void main()
{
 Csample strI,str2;
 strI.intit("Hello","Word");
 strI.print();
 str2.copy(strI);
 str2.print();
 strI.free();
 str2.free();

原创粉丝点击