day11am

专门名字的构造函数,系统自动产生

重载构造函数

构造函数的重载 、缺省构造函数 、自定义构造函数、 拷贝构造函数、

#include<iostream>
using namespace std;

class A{
  int data;
 public:
  A():data(100){cout<<"A()"<<endl;}
  A(const A& o)/*:data(o.data)*/{cout<<"A(const A&)"<<endl;}

//A(const A& o):data(o.data){cout<<"A(const A&)"<<endl;}  不传进去data就是垃圾数据

void show(){
  cout<<"data="<<data<<endl;
}
virtual ~A(){cout<<"~A()"<<endl;}

};

void show(A obj)
{
  obj.show();
}

int main(){
  A a1;
  a1.show();
  cout<<"/////////////////////////////"<<endl;
  A a5(a1);
  a5.show();
}

  

 

 

自己定义拷贝构造函数之后,就没有默认拷贝构造函数了。要在自己定义的拷贝构造函数中处理所有需要处理的数据

2

class Array{
 char *p;
 int len;
 public:
 Array(int n):len(0),p(NULL){
 resize(n);
}
void resize(int n){
  char* q=new char[n];///new 新长度 数组
  int min=(n<len?n:len);
if (p!=NULL)
{
 for(int i=0;i<min;i++)
 q[i]=p[i];
 delete[] p;
 p=q;
 for(int i=min;i<n;i++)
 p[i]='\0';
}
len=n;
}

void set(int index,char value){
 p[index]=value;
}
char get(int index){
 return p[index];
}
~Array(){
 if(p!=NULL){delete[] p;
 p=NULL;}
}
};

  3 

*例子函数头的表示方法
@函数名称:  strcpy 
函数原型:  char* strcpy(char* str1,char* str2); 
函数功能:  把str2指向的字符串拷贝到str1中去 
函数返回:  返回str1,即指向str1的指针 
参数说明: */

  

 

posted @ 2014-03-25 21:32  Flyzhcong  阅读(138)  评论(0编辑  收藏  举报