钱能C++教材习题答案十



文件信息
文件来源  
文件作者  
更新时间 2005-5-8 10:48:14 
添加编辑 viewsnake 

辅助信息
打印功能 打印本文
背景颜色 杏黄 秋褐 胭红 芥绿 天蓝 雪青 炭灰 奶白
字体大小 特大号字 大号字 中号字 小号字
免责声明 本网站所有文章均来自网络,仅提供预览形式,不提供纸张形式,若涉及到版权的文章,请购买正版,毕竟在电脑上看也不舒服啊,呵呵,这是viewsnake个人网站,纯粹交流学习资料的地方。无商业行为。
选择更多免费考研资料:
阅读正文内容
 

第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();
}



<<<返回上一页 <<<返回网站首页
<<<您的位置:首页>考研经验>考研笔记>计算机工程笔记>正文