By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,463 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,463 IT Pros & Developers. It's quick & easy.

implementing constructors given class declarations

P: n/a
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

Apr 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
markpark 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


Show us what you think it should be. Then we'll try to help.

Cheers! --M

Apr 10 '06 #2

P: n/a
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
Apr 10 '06 #3

P: n/a
I made a serious effort to implement the constructors by trying t
build a default constructor, a copy constructor and an operator =
constructor and a destructor for classes
B and C. I have included the twelve.C file and the twelve.h file that
do this. The problem is that I get some really strange ( without lines
and non decipherable by me )
errors when I compile twelve.C. If anyone would be kind enough to take
these two files and compile them in unix or linux and try to understand
them, i would
really aprpeciate it. I am new at C++ and i find it interesting but i'm
nto good at it at all.
this is twelve.h

#ifndef TWELVEH
#define TWELVEH

#include <iostream>
#include <cstdlib>
using namespace std;

class A

{
private:
int a_;
static double db_;

public:
A(int initial_a_ = 0) { a_ = initial_a_; }
};

//---------------------------------------------------------------------

class B: public A
{

private:
int b_;

public:
B(int initial_a_ = 0, int initial_b_ = 0):
A(initial_a_), b_(initial_b_) {}

B(const int b): b_(b) {}

B& operator=(const B& another) { b_ = another.b_; return *this; }

~B() { cout << "B destructor " << b_ << "\n"; }

};

//------------------------------------------------------------------------

class C
{
private:
B* pb_;

public:
C() { pb_ = NULL; }

C(const C& another): pb_(new(B)) { pb_ = another.pb_; }

const C& operator=(const C& another)
{
if (&another != this) {
delete(pb_);
pb_ = new B();
*pb_ = *(another.pb_);
return *this;
}
}

~C() { delete pb_; cout << "C destructor " << "\n"; }
};
//---------------------------------------------------------------------------------

#endif
this is twelve.C.

#include "twelve.h"

double A::db_ = 0.0;

int main()
{

A a();
B b();
C c();

}

Apr 13 '06 #4

P: n/a
i posted my code solution a couple of days ago but it gives me some
strange errors ( without line numbers ) that i don't understand.
if someone has time to put this code in unix/linux and see if they can
understand the errors, i would really appreciate it. thanks. mark

Apr 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.