On 10 Apr 2006 07:49:34 -0700, "markpark" <ma*******@verizon.net>
wrote:
Hi : I had the following question on a C++ test and I don't think I got
it
right and I was hoping someone could answer it. I am very new to C++
so I'm sorry if ths is not a good question. ( I failed the test ).
Thanks.
The question was : given the following code :
class A {
private:
int a_;
static double db_;
public :
A();
};
class B: public A {
private:
int b_;
public:
B();
B(const B& src);
};
class C {
{
private:
B* pb_;
public:
C();
C(const C& );
const C& operator=(const C& src);
~C() {delete pb_; }
};
implement the classes A, B and C. I'm not even sure I understand the
question but I think they mean, write the specific code for the
constructors and write the code ( in main ) that would create specific
instances of A, B and C. Thank you very much to whoever would be kind
enough to do this.
Mark
The assignment operator for class C should return a non-const
reference. Returning a const reference is an error. If this is what
your teacher gave you, then you need a new teacher.
There isn't enough information to say exactly how these classes should
be implemented. For example, B derives publicly from A, yet A has no
virtual destructor. If someone deletes a B instance through an A*, the
behavior is undefined.
Also, what are the reasonable initial values for the member variables?
In the case of the B* member in C, the answer should be fairly
obvious, but what about the others?
Why does B declare a copy constructor, yet no assignment operator and
no destructor (hint: look up the "rule of three" in your C++
textbook)?
Finally, it would be nice if the classes actually provided some
functionality. That would make it a lot easier to decide how they
should be implemented.
--
Bob Hairgrove
No**********@Home.com