发信人: skysnow (王生洪), 信区: Job
标 题: SAP笔试题
发信站: 日月光华 (2004年03月18日19:33:15 星期四), 站内信件
1.Below is usual way we find one element in an array:
const int *find1(const int* array, int n, int x)
{
const int* p = array;
for(int i = 0; i < n; i++)
{
if(*p == x)
{
return p;
}
++p;
}
return 0;
}
In this case we have to bear the knowledge of value type "int", the size of array, even the existence of an array. Would you re-write it using template to eliminate all these dependencies?
//指针的引用用来确定一个数组的存在
Template<class type, int n>const type *find1(const type *& array, const type & x)
{
const type * p = array;
for(int i = 0; i < n; i++)
{
if(*p == x)
{
return p;
}
++p;
}
return 0;
}
2. Assume you have a class like
class erp
{
HR* m_hr;
FI* m_fi;
public:
erp()
{
m_hr = new HR();
m_fi = new FI();
}
~erp()
{
}
};
if "new FI()" failed in the constructor, how can you detect this problem and release the properly allocated member pointer m_hr?
C++里面不到万不得已别用TRY-CATCH,用
erp()
{
auto_ptr<HR> auto_hr (new HR());
auto_ptr<FI> auto_fi (new FI());
m_hr = auto_hr.release();
m_fi = auto_fi.release();
}
3. Check the class and variable definition below:
#include <iostream>
#include <complex>
using namespace std;
class Base
{
public:
Base() { cout<<"Base-ctor"<<endl; }
~Base() { cout<<"Base-dtor"<<endl; }
virtual void f(int) { cout<<"Base::f(int)"<<endl; }
virtual void f(double) {cout<<"Base::f(double)"<<endl; }
virtual void g(int i = 10) {cout<<"Base::g()"<<i<<endl; }
};
class Derived: public Base
{
public:
Derived() { cout<<"Derived-ctor"<<endl; }
~Derived() { cout<<"Derived-dtor"<<endl; }
void f(complex<double>) { cout<<"Derived::f(complex)"<<endl; }
virtual void g(int i = 20) {cout<<"Derived::g()"<<i<<endl; }
};
Base b;
Derived d;
Base* pb = new Derived;
Select the correct one from the four choices:
Cout<<sizeof(Base)<<endl;
A. 4 B.32 C.20 D.Platform-dependent
A?d?
Cout<<sizeof(Derived)<<endl;