a188a100 发表于 2008-6-25 10:37

求助!!3题改错的和10题给出程序写结果的。急。。。。。在线等。麻烦高手了!

6.        找出下面程序中的错误并说明理由。
class base{
        protected:
                int p;};
void fun()
{       
base b;
        int x=b.p;
}


7.        分析下列程序,指出错误的地方并予以改正。
class base{
        public:
                int b;};
class base1:public base{}
class base2:public base{}
class derived:public base1:public base2{
        public:
                int f();
};
main()
{
derived d;
d.b();
d.base::b;
}


8.        如果希望下列程序的运行结果如下,请将程序补充完整。
Base¢s cons.
Derived¢s cons.
Derived¢s des.
Base¢s des.
class Base{
public:
        Base(){cout<< “Base&cent;s cons.”<<endl;}
                          {cout<< “Base&cent;s des.”<<endl;}
};
class Derived:public Base{
public:
        Derived(){cout<< “Derived&cent;s cons.”<<endl;}
        ~Derived(){cout<< “Derived&cent;s des.”<<endl;}
};
void main()
{        Base *ptr=                       
        delete ptr;}

1.        写出下列程序的运行结果
#include <iostream.h>
int i=15;
void main()
{
int i;
i=100;
::i=i+1;
cout<<::i<<endl;
}





2.        写出下列程序的运行结果
class Person{
public:
          Person(){cout<< “Constructor of Person”<<endl;}
~Person(){cout<< “Destructor of Person”<<endl;}
};
class Student:public Person{
public:
          Student(){cout<< “Constructor of Student”<<endl;}
          ~Student(){cout<< “Destructor of Student”<<endl;}
};
class Teacher:public Person{
public:
Teacher(){cout<< “Constructor of Teacher”<<endl;}
~Teacher(){cout<< “Destructor of Teacher”<<endl;}
};
void main()
{
Student s;
Teacher t;
}






3.        写出下列程序的运行结果。
#include <iostream.h>
int square(int i)                        {return i*i;}
float square(float i)                {return i*i;}
double square(double i)        {return i*i;}
int main()
{        int i=12;
        float f=3.0;
        double d=5.0;
        cout<<i<<'*'<<i<<'='<<square(i)<<'\n';
        cout<<f<<'*'<<f<<'='<<square(f)<<'\n';
        cout<<d<<'*'<<d<<'='<<square(d)<<'\n';
        return 0;
}


4.        写出下面程序运行的结果。
#include <iostream.h>
class B{
public:
        B(){cout<< “class B”<<endl;}
};
class X:virtual public B
{
public:
        X(){cout<< “class X”<<endl;}
};
class Y:virtual public B
{
public:
        Y(){cout<< “class Y”<<endl;}
};
class D:public X,public Y
{
public:
        D(){cout<< “class D”<<endl;}
};
void main()
{  D obj;   }



5.        写出下面程序运行的结果。
#include <iostream.h>
int main()
{
int *p;
p=new int(99);
cout<<*p++;
delete --p;
return 0;
}



6.        写出下面程序运行的结果。
#include <iostream.h>
class Base{
public:
        virtual int func() {return 0;}
};
class Derived:public Base{
public:
        int func() {return 100;}
};
void main()
{
        Derived d;
        Base &b=d;
        cout<<b.func()<<endl;
        cout<<b.Base::func()<<endl;
}



7.        写出下面程序运行的结果。
class timer{
        int seconds;
   public:
         timer()                                   {seconds=0;}
         timer(char *t)                        {seconds=atoi(t);}
         timer(int t)                           {seconds=t;}
     timer(int min,int sec)         {seconds=min*60+sec;}
         int gettime()                                {return seconds;}
};
main()
{
  timer a,b(10),c(“20”),d(1,10);
cout<<“seconds1=“<<a.gettime()<endl;
cout<<“seconds2=“<<b.gettime()<endl;
cout<<“seconds3=“<<c.gettime()<endl;
cout<<“seconds4=“<<d.gettime()<endl;
return 0;
}








8.        写出下面程序运行的结果。
class point{
  int x,y;
public:
  point(int a,int b)                                   {x=a; y=b;}
  point(const point &p)                          {x=2*p.x; y=2*p.y;}
void print()                                                  {cout<<x<<“ “<<y<<endl;}
};
main()
{
point  p1(30,40);
point p2(p1);
p1.print();
p2.print();
return 0;
}




9.        写出下面程序运行的结果。
class ab{
private:
        int a;
public:
        ab( );
};
ab::ab()
{   
cout<<“initialized \n”;
a=10;
}
void main()
{   ab s;  }



10.        写出下面程序运行的结果。
#include <iostream.h>
class test{
        private:
                int num;
        public:
                test();
                int getint()                        {return num;}
                ~test();
};
test::test()                                        {num=0;}
test::~test()                                        {cout<<″Destructor is active″<<endl;}
void main()
{       
test x[3];
        cout<<″Exiting main″<<endl;
}

johenny 发表于 2008-6-25 10:59

顶,期待高手的答案!

lyd253261362 发表于 2008-6-25 17:43

解析第10题

运行的结果是:
Exiting main
Destructor is active
Destructor is active
Destructor is active
Press any key to continue
解释:test x[3];是对象数组,实际上是三个对象。
因此,在程序退出时,会有三个“Destructor is active”。
为什么是"Exiting main"在前面那。
由于main主函数没有结束的话,对象还在生存的范围内==main()中。
当main结束时,对象就要调用相应的析构函数。

lyd253261362 发表于 2008-6-25 17:49

9结果。

initialized

lyd253261362 发表于 2008-6-25 17:54

8题结果。

30   40
60   80
拷贝构造函数。
point(const point &p)

页: [1]

编程论坛