Step
up to the Plate.
Problem 1Itýs
not uncommon for C programmers to write code that resembles object-oriented
programming techniques. The following structure is a good example
of a C implementation of an object. Why would a C++ class be more
efficient in terms of data space than this C construct?
struct c_obj {
int x, y; void (*create)(); void (*show)(); void (*hide)(); void (*delete)(); };
Answer: The C++ equivalent, shown here, is more efficient
than the typical C equivalent because the C++ class does not actually
store a pointer for each virtual function. Instead, it stores a pointer
to an array of functions (virtual function table) so each instance
of cpp_obj is smaller than each struct c_obj.
class cpp_obj {
public:
int x, y;
void create(void);
void show(void);
void hide(void);
void delete(void);
};
4-00
|