課程名稱︰計算機程式
課程性質︰系定必修
課程教師︰王勝得
開課系所︰電機系
考試時間︰2006 1 11
試題 :
1. (22%) True or False
(a) A derived object may be used where a base object is expected. T
(b) The parameter of a copy constructor should be passed by value. F
(c) A virtual member function in a base class cannot be overridden
in the derived class. F
(d) If at least one of member functions in a class is pure virtual,
the class is abstract. T
(e) If we have declared const char* p; then the statement p=0;
is an error. F
(f) A base class should always have virtual constructors. F
(g) The private inheritance can be seen as a form a composition. T
(h) Base-class constructors are not inherited by derived classes. T
(i) Inheritance encourages the reuse of proven high-quality
software. T
(j) Polymorphic programming can reduce the need for switch
statement. T
(k) Referring to a derived-class object with base-class pointer
is dangerous. F
2.(6%) If you we want print out the ASCII code value of character 'c'
in integer, how can we do that? Write down the cout statement
to do the job.
Ans: cout << static_cast<int> ('c') ;
3.(6%) (a) Write down the statement that allocates a 10-element integer
array using new operatorand assign the array starting address
to integer pointer intArray.
(6%) (b) Write down the statement that deallocates the array that is
allocated in the part (a).
Ans: (a) int *intArray = new int[10]; or intArray = new int[10];
(b) delete [] intArray;
4.(6%) What is the output generated by the following program?
#include <iostream>
using namespace std;
class B { public: virtual void print(){};};
class D : public B {};
class E : public B {};
class F : public B {};
class G : public E {};
int main(){
B *b[4];
b[ 0 ] = new D();
b[ 1 ] = new E();
b[ 2 ] = new F();
b[ 3 ] = new G();
for ( int i = 0; i < 4; i++ ) {
E *d = dynamic_cast < E * > ( b[ i ] );
if ( d != 0 ) {
cout << "i= " << i << endl;
}
}
return 0;
}
Ans:
i= 1
i= 3
5.(6%) We have the following class template definition:
template< class T >
class Stack {
public:
Stack( int = 10 ); // default constructor (stack size 10)
// destructor
~Stack() { delete [] stackPtr; } // end ~Stack destructor
bool push( const T& ); // push an element onto the stack
bool pop( T& ); // pop an element off the stack
// determine whether Stack is empty
bool isEmpty() const { return top == -1; }
// determine whether Stack is full
bool isFull() const { return top == size - 1; }
private:
int size; // # of elements in the stack
int top; // location of the top element
T *stackPtr; // pointer to the stack
}; // end class Stack
Write the statement that use the template class to declare
one 100-element integer Stack named intStack and one 50-element
double Stack named doubleStack.
Ans:
Stack<int> intStack(100);
Stack<double> doubleStack(50);
6.Consider the following class definition:
#include <iostream>
using namespace std;
class Point{
public:
Point(){cout << "Point constructors" << endl ;}
~Point() { cout << "Point destructors" << endl ;}
void set(int a=0, int b=0);
virtual void print(){ cout << x << y << endl;}
private:
int x, y;
};
class Circle : public Point {
public :
Circle(int r=3):radius(3){cout << "Circle constructors" << endl ;}
~Circle() { cout << "Circle destructors" << endl ;}
void set(int r=2) ;
virtual void print() { cout << radius << endl;}
private :
int radius ;
};
void Point::set(int a, int b){
// assignment statements here
cout << "Point set" << endl;
}
void Circle::set(int r){
// assignment statement here
cout << "Circle set" << endl;
}
(a) (6% )Complete the methods set for both the Point and Circle class.
(b) (9%)What are the output generated by the following main program?
int main() {
Point p, *pPtr;
Circle c, *cPtr;
{
Circle c2;
}
pPtr = &c;
pPtr->set();
pPtr->print();
return 0;
}
Ans:
(a)
void Point::set(int a, int b){
x=a;
y=b;
cout << "Point set" << endl;
}
void Circle::set(int r){
radius = r;
cout << "Circle set" << endl;
}
(b)
Point constructors
Point constructors
Circle constructors
Point constructors
Circle constructors
Circle destructors
Point destructors
Point set
3
Circle destructors
Point destructors
Point destructors
7.(9%) Correct the errors (if any) of the following program:
class Prob3 {
public:
Prob3(int y=10) : data (y) { }
Prob3() { };
int getIncrData() const { return data++ ;}
static int getCount() {
cout << "Data is " << data << endl;
return count;
}
private:
int data;
static int count;
};
Ans: three errors:
(a) cannot have two default constructors
(b) getCount() is not a static member function
(c) getIncrData() is not a const member function
8. (9%) We have the following three class definitions
class Shape {
public:
virtual void draw() const;
virtual void rotate(int degrees);
private:
float x, y;
int color;
};
class Circle : public Shape {
public:
void draw() const;
void rotate(int) { }
protected:
float radius;
};
class Triangle : public Shape {
public:
void draw() const;
void rotate(int);
protected:
float xa, ya, xb, yb, xc, yc;
};
In class Triangle we found that the member data float x, y that was
inherited from Shape is useless, so please rewrite the above three
class definitions in such a way that class Shape is an abstract class.
Ans:
class Shape {
public:
virtual void draw() const =0;
virtual void rotate(int degrees) =0;
};
class Circle : public Shape {
public:
void draw() const;
void rotate(int) { }
protected:
float cx, xy;
int color
float radius;
};
class Triangle : public Shape {
public:
void draw() const;
void rotate(int);
protected:
int color;
float xa, ya, xb, yb, xc, yc;
};
9. (15%) Consider the following code.
class B {
public:
B() {cout << "B() ";}
~B() {cout << "~B() ";}
virtual void f() const {cout << "B::f() ";}
};
class D: public B {
public:
D() {cout << "D() ";}
~D() {cout << "~D() ";}
void f() const {cout << "D::f() ";}
};
B g(const B& x) {return x;}
void h(B x) {x.f();}
What do each of the statements below print?
(1) B b;
(2) D d;
(3) b.f();
(4) d.f();
(5) B* p = new D;
(6) p->f();
(7) delete p;
(8) B& r=d; r.f();
(9) g(b);
(10)g(d);
(11)h(b);
(12)h(d);
(13)b=d; b.f();
Ans:
(1)B()
(2)B() D()
(3)B::f()
(4)D::f()
(5)B() D()
(6)D::f()
(7)~B()
(8)D::f()
(9)~B()
note: f(b) return a temporary B object using copy constructor,
which print nothing, but when the return object is destroyed
it prints ~B()
(10)~B()
(11)B::f() ~B()
note: h(b) passes a B object as parameter using copy constructor,
which print nothing, but when the object is destroyed as
function returns it prints ~B()
(12)B::f() ~B()
(13)B::f()
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.58.163.194