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

   /2005-05-08

 

第13章

//*******
//*13.1**
//*******
物质->有机物、无机物
有机物->生物、非生物
生物->植物、动物
动物->低级动物、高级动物
高级动物->人、其他高级动物

//*****************
//******13.2未完成*
//*****************
# include <iostream.h>

class Student 
{
public:
        void setname(char * newname);
        char * getname();
protected:
        char * name;
        int numofcourse;//实际选修课程数
};

class Course 
{
public:
        void setxuefen(float x);
        float getxuefen();
protected:
        char * kecheng;
        float xuefen;
        int numofstuent;//实际选修人数
};

void main()
{
}

第14章

//*****************
//******14.1*******
//*****************
//Multi[0] is:0
//Multi[0] is:1
//Multi[0] is:4
//Multi[0] is:9
//Multi[0] is:16
//Multi[0] is:25
//Multi[0] is:36
//Multi[0] is:49
//Multi[0] is:64
//Multi[0] is:81
//Destroying..9
//Destroying..8
//Destroying..7
//Destroying..6
//Destroying..5
//Destroying..4
//Destroying..3
//Destroying..2
//Destroying..1
//Destroying..0

//*****************
//******14.2*******
//*****************
1
2
3
4
5
6
7
8
9
10
Null pointer assignment
增加的拷贝构造函数为:
Vector::Vector(Vector & x)
{
        size=x.size;
        int * buffer=new int[x.size];
        for(int k=0;k<x.size;k++)
        {
                buffer[k]=x.buffer[k];
        }
};

//*****************
//******14.3*******
//*****************
# include <iostream.h>
class X
{
public:
        X(int a)
        {
                cout<<"construct a class:"<<a<<endl;
                i=a;
        };
        X(X& b)
        {
                cout<<"copy a class of:"<<b.i<<endl;
        };
        ~X()
        {
                cout<<"distroy a class:"<<i<<endl;
        };
protected:
        int i;
};

X f(X b)
{
       
        cout<<" come in f()"<<endl;
        cout<<" come out f()"<<endl;
        return b;
       
};

void main()
{
        X a(1);//construct a class:1
cout<<"     "<<endl;
               
        X b=f(X(2));//construct a class:2
       
        //come in f()
        //come out f()
        //copy a class of:2
        //distroy a class临时class
cout<<"     "<<endl;
        a=f(a);
        //copy a class of:1
        //come in f()
        //come out f()
        //copy a class of:1
        //distroy a class临时class
        //distroy a class: ???
cout<<"     "<<endl;
}
       
        //distroy a class 2
        //distroy a class 1

//*****************
//******14.4*******
//*****************
CAT::CAT(const CAT & A)
{
        int * itsAge=new int;
         * itsAge=A.itsAge;
}

第15章

//*****************
//******15.1(1)****
//*****************

class example
{
public:
        example(int);
        ~example();
protected:
        int i;
        static int j;
};

static int j=0;

example::example(int a)
{
        j=a;
        j++;
};
example::~example()
{
        j--;
};


//*****************
//******15.1(2)****
//*****************
# include <iostream.h>
class example
{
public:
        example(int);
        ~example();
        void getij()
        {
                cout<<"i:"<<i<<" "<<"j:"<<j<<endl;
        };
protected:
        int i;
        static int j;
};

int example::j=0;//注意:非static int example::j=0;

example::example(int a)
{
        i=a;
        j++;
        cout<<"construct a class: "<<j<<endl;
};
example::~example()
{
        j--;
        cout<<"distroy a class "<<"and j="<<j<<endl;
};

void main()
{
        example x(1);
        x.getij();
        cout<<endl;
        example y(2);
        y.getij();
        cout<<endl;
        example z(3);
        z.getij();
        cout<<endl;
        x.getij();
        y.getij();
        z.getij();
        cout<<endl;
}

//*****************
//******15.1(3)****
//*****************
# include <iostream.h>
class example
{
public:
        example(int);
        ~example();
        void getij()
        {
                cout<<"i:"<<i<<" "<<"j:"<<j<<endl;
        };
        static void getj()
        {
                cout<<"j:"<<j<<endl;
        };
protected:
        int i;
        static int j;
};

int example::j=0;//注意:非static int example::j=0;

example::example(int a)
{
        i=a;
        j++;
        cout<<"construct a class: "<<j<<endl;
};
example::~example()
{
        j--;
        cout<<"distroy a class "<<"and j="<<j<<endl;
};

void main()
{
        example x(1);
        x.getij();
        cout<<endl;
        example y(2);
        y.getij();
        cout<<endl;
        example z(3);
        z.getij();
        cout<<endl;

       x.getj();
        cout<<endl;
        y.getj();
        cout<<endl;
        z.getj();
        cout<<endl;

        x.getij();
        y.getij();
        z.getij();
        cout<<endl;
}

//******15.2(1)****
//*****************
void SetValue(Animal&,int,int);不是class Animal的友元,不能直接访问其保护数据成员。
应修改void SetValue(Animal&,int,int)为class Animal的友元。
//*****************
//******15.2(2)****
//*****************
类的成员函数定义为:
void Animal::SetWeight(int tw)
{
        itsWeight=tw;
};
void Animal::SetAge(int tn)
{
        itsAge=tn;
};
普通函数定义为:
void SetValue(Animal& ta,int tw)
{
        ta.Weight(tw);
};
void SetValue(Animal& ta,int tw,int tn)
{
        ta.Weight(tw);
        ta.SetAge(tn);
};


//*****************
//******15.3*******
//*****************
# include <iostream.h>
# include <math.h>
class Boat;

class Car
{
public:
        Car(int j){size=j;}
        int get(){return size;}
        int Leisure(int time)
{
        return int(sqrt(time))*size;
}
protected:
        int size;
};

class Boat
{
public:
        Boat(int j){size=j;}
        int get(){return size;}
        int Leisure(int time)
        {
                return int(sqrt(time))*size;
        }
protected:
        int size;
};

void main()
{
        Car c1(2);
        Boat b1(3);
        int time=4;
        cout<<c1.Leisure(time)*b1.Leisure(time);
}


相关话题/

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