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

   /2005-05-08

 

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


相关话题/

  • 领限时大额优惠券,享本站正版考研考试资料!
    大额优惠券
    优惠券领取后72小时内有效,10万种最新考研考试考证类电子打印资料任你选。涵盖全国500余所院校考研专业课、200多种职业资格考试、1100多种经典教材,产品类型包含电子书、题库、全套资料以及视频,无论您是考研复习、考证刷题,还是考前冲刺等,不同类型的产品可满足您学习上的不同需求。 ...
    本站小编 Free壹佰分学习网 2022-09-19