一、通讯类
#include<iostream>
using namespace std;
#include<string.h>
class connector
{
char name[20];
char phone[20];
char *addr;
public:
connector(char a[],char b[],char *c)
{
strcpy(name,a);
strcpy(phone,b);
if(c)
{
addr=new char[strlen(c)+1];
strcpy(addr,c);
}
else
addr=NULL;
cout<<"constructor"<<endl;
}
~connector()
{
if(addr)
delete []addr;
cout<<"destructor"<<endl;
}
connector(connector & a)
{
if(a.addr)
{
addr=new char[strlen(a.addr)+1];
strcpy(addr,a.addr);
}
else
addr=NULL;
strcpy(name,a.name);
strcpy(phone,a.phone);
cout<<"copy constructor"<<endl;
}
void display();
};
void connector::display()
{
cout<<"姓名"<<'\t'<<"电话号码"<<'\t'<<"地址"<<endl;
cout<<name<<'\t'<<phone<<'\t'<<addr<<endl;
}
void main()
{
connector a("张三","15011112222","江苏省XXX");
connector b(a);
cout<<"输出a"<<endl;
a.display();
cout<<"输出b"<<endl;
b.display();
}
二、线性表
#include<iostream>
using namespace std;
#include<string>
class line
{
float * list;
int max;
int num;
public:
line(float *a, int b,int c)
{
if(a)
{
list=new float[b];
for(int i=0;i<c;i++)
*(list+i)=*(a+i);
}
else
list=NULL;
max = b;
num = c;
cout<<"constructor"<<endl;
}
~line()
{
delete list;
cout<<"destructor"<<endl;
}
line(line &a)
{
if(a.list)
{
list=new float[a.max];
for(int i=0;i<a.num;i++)
*(list+i)=*(a.list+i);
}
max=a.max;
num=a.num;
cout<<"copy constructor"<<endl;
}
void display();
void addele(float a);
};
void line::display()
{
for(int i=0;i<num;i++)
cout<<*(list+i)<<'\t';
cout<<endl;
}
void line::addele(float a)
{
if(num<max)
{
*(list+num)=a;
num++;
}
else
cout<<"Array full"<<endl;
}
void main()
{
float a[5]={1,2,3};
line b(a,5,3);
b.display();
b.addele(4);
b.display();
b.addele(5);
b.display();
b.addele(6);
b.display();
}