习题
、选择题(20分)
1. 关键字 ____________ 说明对象或变量初始化后不会被修改。
a. static  b. public  c. const  d. inline
2. 如果调用带有默认参数的函数时缺少一个参数,则______________参数就作为这一参数。
a. 第一个    b. 最后一个    c.  中间一个    d. 以上都不是
3. 成员函数可声明为静态的,条件是它不访问 __________类成员。
a. 静态    b. 常数  c.  非静态    d. 公共
4. 内联函数执行起来比标准函数_______________
a. 更慢    b. 更快  c.  次数更多    d. 以上都不是
5. 默认参数的值由___________________提供。
a.  该函数    b. 调用程序  c. 上述二者    d. 以上都不是
6. C++ 中,混合类型表达式_____________
a.  允许存在  b.  为一错误  c.  从 int 到 float  d. 从 float 到 int
7. 表达式 long(intVar) 也可表示为 _______________
a.  intvar = long;  b.  intVar(long)  c.  (long)intVar    d. 以上都不是
杭州电子科技大学
8. 静态数据成员的生存期_________________
a.  与整个程序相同    b.  不长于类的生存期 
c.  取决于创建的对象数  d. 以上都不是
9. 要让一个类中的所有对象具有共同的数据,请使用__________________
a. 常数成员变量      b. 私有数据成员
    c.  静态数据成员    d. 以上都是
10. 设置虚基类的目的是:
a.简化程序    b.消除二义性    c.提高运行效率    d.减少目标代码
、写出下面程序的执行结果:(15分)
1#include <iostream>
using namespace std;
class A
{friend double count(A&);
public:
A(double t, double r):total(t),rate(r){}
private:
    double total;
double rate;
};
double count(A& a)
{
a.total+=a.al;
al;
}
int main(void)
{
    A x(80,0.5),y(100,0.2);
    cout<<count(x)<<','<<count(y)<<'\n';
    cout<<count(x)<<'\n';
    return 0;
}
执行结果:
2)
#include<iostream>
using namespace std;
class Count{
  private:
    static int counter;
    int obj_id;
  public:
    Count(); //constructor
    static void display_total(); //static function
    void display();
    ~Count(); //destructor
};
int Count::counter; //definition of static data member
Count::Count() //constructor
{
  counter++;
  obj_id = counter;
}
Count::~Count() //destructor
{
  counter--;
  cout<<"Object number "<<obj_id<<" being destroyed\n";
}
void Count::display_total() //static function
{
  cout <<"Number of objects created is = "<<counter<<endl;
}
void Count::display()
{
  cout << "Object ID is "<<obj_id<<endl;
}
int main(void)
{
  Count a1;   
  Count::display_total();
  Count a2, a3,a4;
  Count::display_total();
  a2.display();
  a4.display();
  return 0;
}
3)#include <iostream >
using namespace std;
class BASE
  char c; 
  public: 
  BASE(char n):c(n){}
  virtual ~BASE(){cout<<c;} 
}; 
class DERIVED:public BASE{ 
  char c; 
  public: 
    DERIVED(char n):BASE(n+1),c(n){} 
    ~DERIVED(){cout<<c;} 
}; 
int main(void) 
{  DERIVED('X'); 
    return 0; 
五、 程序填空:(10分)
    #include <iostream>
using namespace std;
    class A
    {
    ______1______
        char name[80];
    public:
        A(  ____2______  ) {____3______ }
    };
    class B_____4_______
    {
    public:
        B(const char*n)_____5_______{}
        void PrintName( ) {cout<<”name:”<<name<<endl;};
    };
    void main( )
    {
        B b1(“Ling Li”);
        b1.PrintName( ) ;
    }  //    执行结果:    name: Ling Li
六、编程题(20分)
1.编写程序:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、               
Rectangle( 长方形)、Trapezoid (梯形)和Triangle (三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。 
注:主函数中定义如下对象
Circle circle(12.6);                                           
Square square(3.5);               
Rectangle rectangle(4.5,8.4); 
Trapezoid trapezoid(2.0,4.5,3.2);   
Triangle triangle(4.5,8.4);     
一、判断题(15分)
1)  2)  3)  4)  5)  6)  7)  8)  9)  10)
二、选择题(20分)
1) c  2)b  3) c  4)b  5)c  6) a  7)c  8)a  9)c    10)b
、指出下列程序片段中的错误,并解释错在何处。(20分)
1)*ptr=555;    ptr是指向整数常量的指针
ntptr=&another;  ntptr是常量指针,不能指向别的的变量
2)int  arrp;  应改为:int *arrp;
delete arrp;  应改为:delete [ ]arrp;
3return basedata;// border_and_menu中引用basedata时产生二义性,应使用虚基类
应改为:
class border:virtual public window
{ };
class menu: virtual public window
{ };
4)整个正确的程序段(参考):
#include <iostream.h>  //加本句
template<typename T>  //加typename
void print(T *a)
{    cout<<a<<'\n';}
void main( )
{  int y=10;  //y应先声明后使用,并给初值
    const int x=0;
    cout<<y<<'\n';
    //x=5;    x为const,去掉该句
    int* p;
    p=&y;
    print(p);
//    return 0;  main返回为void ,去掉该句
}
、写出下面程序的执行结果:(15分)
1
2)
3XY
五、程序填空:(10分)
(1)protected:或public    (2)const char *n  (3)strcpy(name,n);
(4):public A或:protected A  (5):A(n)
六、编程题(20分)
1.
#include <iostream>
using namespace std;
class Shape
{public:
virtual double area() const =0;                             
};
class Circle:public Shape
{public:
Circle(double r):radius(r){}