以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  求助!!C++的3题改错的和10题给出程序写结果的。急。。。。。在线等。麻烦高手了!  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=64055)


--  作者:a188a100
--  发布时间:6/25/2008 10:44:00 AM

--  求助!!C++的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;
}


--  作者:chenqm0454
--  发布时间:8/4/2008 6:01:00 PM

--  
还没有学到C++
--  作者:lee_ruin
--  发布时间:8/4/2008 7:49:00 PM

--  
b.p有东西吗?
--  作者:lee_ruin
--  发布时间:8/4/2008 7:52:00 PM

--  
d.base::b;
d的什么???
--  作者:lee_ruin
--  发布时间:8/4/2008 7:53:00 PM

--  
101
--  作者:lee_ruin
--  发布时间:8/4/2008 7:54:00 PM

--  
先构父再构子,析正好相反
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms