C AND C++ TESTING (INTERMEDIATE)
1. Explain the following:
(1) struct str *(*block())[8];
(2) double (*(*func)())[2][6];
2. What's object ID? What's persistent object?
How to create persistent object?
3. What’s the type (span) of a pointer? In the following code:
class A : public B { … };
A a; B *pb = &a; A *pa = &a;
What’s difference between pb and pa ?
4. Why in the iostream library the designer defined “operator void*()” for “cin” ?
(1) class C1 { public: int j; };
void C1_ini() { C1 c1; … }
class C3 {public: C2 c2; int k; };
void C3_ini() { C3 c3; … }
S global;
S s_ini() { S local; S *ps = new S; *ps = local; delete ps; return local; }
private: int j, k; };
S global;
S s_ini() { S local the; S *ps = new S; *ps = local; delete ps; return local; }
What’s wrong in the following code:
A *pa = new ( buffer ) A;
// do something
delete pa; // want to destroy the object but preserve the storage for reuse
pa = new ( buffer ) A;
class A { public: float x, y, z; … };
A a;
What’s difference between “& A::z” and “& a.z”?
Assume we have a code:
float A::*p1 = 0; float A::*p2 = & A::x;
What’s difference between p1 and p2 ?
class A { public: virtual ~A();
int a();
int b();
virtual int c(); };
What are the following addresses:
&A::~A; &A::a; &A::b; &A::c;
Why?
class B { public: B(); virtual ~B(); … };
class A : public B { public: A(); ~A(); … };
void main() { B *pb = new A[10]; delete [ ] pb; … }
extern double fun (double); // first fun()
template < class type >
class A { public:
void fun1() { t = fun (j); }
type fun2() { return fun(t) }
private: int j; type t; };
In the example.cpp file we have:
extern int fun (int); // second fun()
A <int> a;
a.fun1();
a.fun2();
The question is which fun() is called when a.fun1() and a.fun2() are called?
void sh_use ( void* p ) {
A *pa = new A;
Sh_lock ( p );
// ….
Sh_unlock (p );
delete pa;
}
How to add Exception Handling (EH) in above code? How to use auto_ptr object to
improve EH coding?
rules used to figure out objects when designing a programming system?
Data Type (ADT) Model (Object-Based Model) and (3) Object-Oriented (OO) Model
(Polymorphism). What are differences between them?
void Stack::push(char);
How to declare a pointer to the member function? How to initialize the pointer?
How to call the member function through the pointer?
C AND C++ TESTING (INTERMEDIATE) WITH ANSWERS
1. Explain the following:
(1) struct str *(*block())[8];
(2) double (*(*func)())[2][6];
2. What's object ID? What's persistent object?
How to create persistent object?
3. What’s the type (span) of a pointer? In the following code:
class A : public B { … };
A a; B *pb = &a; A *pa = &a;
What’s difference between pb and pa ?
A: The type (span) of a pointer is the different type of object being addressed. The type of a pointer instructs the compiler as to how to interpret the memory found at a particular address.
Pb and pa addresses the same first byte of A object. The difference is that address span of pa encompasses the entire A object, while the span of pb encompasses only the B subobject of A. pb cannot directly access any members other than those present within the B subobject, except through the virtual mechanism.
4. Why in the iostream library the designer defined “operator void*()” for “cin” ?
A: The code if (cin) should evaluate to a true/false scalar value. So operator int() was defined. But the programmer error: cin << intVal // meant cout, not cin, will produce: int temp = cin.operator int(); temp << intVal; To avoid this unexpected behavior, replace the operator int() with operator void*().
(1) class C1 { public: int j; };
void C1_ini() { C1 c1; … }
class C3 {public: C2 c2; int k; };
void C3_ini() { C3 c3; … }
S global;
S s_ini() { S local; S *ps = new S; *ps = local; delete ps; return local; }
private: int j, k; };
S global;
S s_ini() { S local; S *ps = new S; *ps = local; delete ps; return local; }
A: (1) and (3).
A: In A.h, we have
Class A {
Public:
Void* operator new(size_t) {
Return pool.alloc();
}
Void operator delete(void* p) {
Pool.free(p);
}
Private:
Static Pool pool;
};
In A.c, we have
Pool A::pool(sizeof(A));
At global scope, we have
A* pa = new A;
A::pool and pa are nonsimple, nonlocal, static object. A::pool might or might not be constructed before pa is initialized. This is the static initialization problem. We can use Init function, Init check, and Init object to deal with the static initialization problem.
What’s wrong in the following code:
A *pa = new ( buffer ) A;
// do something
delete pa; // want to destroy the object but preserve the storage for reuse
pa = new ( buffer ) A;
A: The placement operator new is a predefined overloaded instance of operator new. Delete pa applies the destructor, but it also frees the memory addressed by pa. So, you should use pa->~A instead.
A: NRV is named return value optimization. It is the compiler level optimization. Usually a class has well designed copy constructor will turn on NRV optimization.
A: static_cast <type> (expr) performs well-behaved casts. It is safer than using C-style cast. Dynamic_cast <type> (expr) enable you to use safe, standardized, and simple downcasts in runtime.
class A { public: float x, y, z; … };
A a;
What’s difference between “& A::z” and “& a.z”?
Assume we have a code:
float A::*p1 = 0; float A::*p2 = & A::x;
What’s difference between p1 and p2 ?
A: & A::z is its offset within the class, whereas a.z is its actual address in memory which is the offset of z (minus 1) plus the beginning address of a. p2 is the physical offset of x plus 1.
class A { public: virtual ~A();
int a();
int b();
virtual int c(); };
What are the following addresses:
&A::~A; &A::a; &A::b; &A::c;
Why?
A: &A::~A; yields 1. &A::c; yields 2. Because the address of virtual function is unknown at compile time. What is known is the function’s associated index into the virtual table. &A::a; and &A::b; yield their actual memory locations since they are not virtual.
A: The use of p as a pointer to an address in storage is still well defined if not setting p = 0.
class B { public: B(); virtual ~B(); … };
class A : public B { public: A(); ~A(); … };
void main() { B *pb = new A[10]; delete [ ] pb; … }
A: delete [ ] pb; is a problem. In most case, only ~B() is called when delete [ ] pb; instead of delete [ ] pb; the following code should be added:
For (int I = 0; I < 10; I ++) {
B *p = & ((A*) pb) [I] ;
Delete p;
}
extern double fun (double); // first fun()
template < class type >
class A { public:
void fun1() { t = fun (j); }
type fun2() { return fun(t) }
private: int j; type t; };
In the example.cpp file we have:
extern int fun (int); // second fun()
A <int> a;
a.fun1();
a.fun2();
The question is which fun() is called when a.fun1() and a.fun2() are called?
A: When fun1() is called, inside fun1() the first fun() is called because the invocation of the first fun() is not dependent on the template argument and the invocation must be resolved from the template declaration. When fun2() is called the second fun() is called because this instance clearly is dependent on the template argument that determines the actual type t.
void sh_use ( void* p ) {
A *pa = new A;
Sh_lock ( p );
// ….
Sh_unlock (p );
delete pa;
}
How to add Exception Handling (EH) in above code? How to use auto_ptr object to
improve EH coding?
A: void sh_use ( void* p ) {
A *pa = new A;
Try{
Sh_lock ( p );
// ….
}
Catch (…) {
Sh_unlock (p );
delete pa;
throw;
}
Sh_unlock (p );
delete pa;
}
Using the class template auto_ptr can avoid memory leakage in the case of exceptions.
void sh_use ( void* p ) {
auto_ptr < A > pa ( new A);
Sh_lock shl(p);
}
rules used to figure out objects when designing a programming system?
Data Type (ADT) Model (Object-Based Model) and (3) Object-Oriented (OO) Model
(Polymorphism). What are differences between them?
A: When we use the const, we should understand how the const should be interpreted and how it should be applied. The abstract const promises that a function will not change the abstract value of the object , whereas the bit const promises that a function will not write to any of the bits making up the object. We should always use abstract const, and other possible interpretations also are not type safe.
A: When we design a container, the containing object can have a handle to the representation of the contained object. The handle object acts as pointer, so it is called smart pointer. The class template auto_ptr is a smart pointer. If these container class contain a reference count manipulated by the handle class, such use is called the reference counting. Using reference counting can avoid expensive copy of the instances.
void Stack::push(char);
How to declare a pointer to the member function? How to initialize the pointer?
How to call the member function through the pointer?
A: It is also called link compatibility. The changes in the classes make all related codes to recompile. These changes are binary incompatibility. It is typically involved in the design of the libraries.
A: This declares a function, data, whose return type is list<int>. The function data takes two parameters: The first parameter is named dataFile; its type is istream_iterator<int>. And the second parameter has no name; its type is pointer to function taking nothing and returning an istream_iterator<int>.