第12章
//*******
//*12.1**
//*******
Constructing normally
Constructing with a number:20
Display a number:0
Display a number:20
Destructing
Distructing
//******
//*12.2*
//******
# include <iostream.h>
class Employee
{
public:
Employee(char * name,char * street,char * shi,char * sheng,char * post)
{
data[0]=name;
data[1]=street;
data[2]=shi;
data[3]=sheng;
data[4]=post;
}
ChangeName(char * name)
{
data[0]= name;
}
void Display()
{
cout<<"name:"<<data[0]<<"street:"<<data[1]
<<"shi:"<<data[2]<<"sheng:"<<data[3]
<<"post:"<<data[4]<<endl;
}
protected:
char * data[5];
}
//******
//*12.3*
//******
# include <iostream.h>
class Name
{
public:
void Display();
Name(char * a);
void ChangeName(char * e);
protected:
char * xing;
char * ming;
};
void Name:isplay()
{
cout<<"xing:"<<xing<<" "<<"ming:"<<ming<<endl;
};
Name::Name(char * a)
{
char * name1=NULL;
char * name2=NULL;
int i=0;//姓名总字节数
for(;a != ' ';i++)//姓的字节数
{
;
}
name1=new char[i+1];
for(int k=0;k<i;k++)
{
name1[k]=a[k];
}
name1='/0';
int j=0;//名的字节数
for(;a!= '/0' ;j++)
{
i++;
}
name2=new char[j];
for(int l=0;l<j;l++)
{
name2[l]=a[i-j+l];
}
name2[j]='/0';
xing=name1;
ming=name2;
};
void Name::ChangeName(char * e)
{
char * name3=NULL;
char * name4=NULL;
int x=0;
for(;e[x] != ' ' ;x++)
{
;
}
name3=new char[x+1];
for(int m=0;m<x;m++)
{
name3[m]=e[m];
}
name3[x]='/0';
int y=0;
for(;e[x]!= '/0' ;y++)
{
x++;
}
name4=new char[y];
for(int n=0;n<y;n++)
{
name4[n]=e[x-y+n];
}
name4[y]='/0';
xing=name3;
ming=name4;
};
class Employee
{
public:
Employee(char * b,char * street,char * shi,char * sheng,char * post);
void ChangeName(char * c);
void Display();
protected:
char * data[4];
Name d;
};
Employee::Employee(char * b,char * street,char * shi,char * sheng,char * post):d(b)//注意类成员的初始化
{
data[0]=street;
data[1]=shi;
data[2]=sheng;
data[3]=post;
};
void Employee::ChangeName(char * c)
{
d.ChangeName(c);
};
void Employee:isplay()
{
d.Display();//注意()不能缺少
cout<<"street:"<<data[0]<<endl
<<"shi:"<<data[1]<<endl<<"sheng:"<<data[2]<<endl
<<"post:"<<data[3]<<endl;
};
//*************
//*12.4-Name.h*
//*************
class Name
{
public:
void Display();
Name(char * a);
void ChangeName(char * e);
protected:
char * xing;
char * ming;
};
//*****************
//*12.4-Employee.h*
//*****************
# include "Name.h"
class Employee
{
public:
Employee(char * b,char * street,char * shi,char * sheng,char * post);
void ChangeName(char * c);
void Display();
protected:
char * data[4];
Name d;
};
//***************
//*12.4-Name.cpp*
//***************
# include <iostream.h>
# include "Name.h"
void Name:isplay()
{
cout<<"xing:"<<xing<<" "<<"ming:"<<ming<<endl;
};
Name::Name(char * a)
{
char * name1=NULL;
char * name2=NULL;
int i=0;//姓名总字节数
for(;a != ' ';i++)//姓的字节数
{
;
}
name1=new char[i+1];
for(int k=0;k<i;k++)
{
name1[k]=a[k];
}
name1='/0';
int j=0;//名的字节数
for(;a!= '/0' ;j++)
{
i++;
}
name2=new char[j];
for(int l=0;l<j;l++)
{
name2[l]=a[i-j+l];
}
name2[j]='/0';
xing=name1;
ming=name2; };
void Name::ChangeName(char * e)
{
char * name3=NULL;
char * name4=NULL;
int x=0;
for(;e[x] != ' ' ;x++)
{
;
}
name3=new char[x+1];
for(int m=0;m<x;m++)
{
name3[m]=e[m];
}
name3[x]='/0';
int y=0;
for(;e[x]!= '/0' ;y++)
{
x++;
}
name4=new char[y];
for(int n=0;n<y;n++)
{
name4[n]=e[x-y+n];
}
name4[y]='/0';
xing=name3;
ming=name4;
};
//*******************
//*12.4-Employee.cpp*
//*******************
# include <iostream.h>
# include "Employee.h"
Employee::Employee(char * b,char * street,char * shi,char * sheng,char * post):d(b)//注意类成员的初始化
{
data[0]=street;
data[1]=shi;
data[2]=sheng;
data[3]=post;
};
void Employee::ChangeName(char * c)
{
d.ChangeName(c);
};
void Employee:isplay()
{
d.Display();//注意()不能缺少
cout<<"street:"<<data[0]<<endl
<<"shi:"<<data[1]<<endl<<"sheng:"<<data[2]<<endl
<<"post:"<<data[3]<<endl;
};
//***************
//*12.4-main.cpp*
//***************
# include "Employee.h"
void main()
{
Employee em("Mark Brooks","5 West St.","Revere","CA","12290";
/* char buffer[255];//所其作用不明显*/
em.Display();
em.ChangeName("Richard Voss";
em.Display();
}