473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

implementing constructors given class declarations

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
4 1482
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrea_gavana | last post by:
Hello NG, I am trying to port a useful class from wxWidgets (C++) to a pure Python/wxPython implementation. In the C++ source code, a unique class is initialized with 2 different methods (???)....
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Thomas Matthews | last post by:
Hi, Is placing the keyword "virtual" in front of a constructor allowed as in the sample below? class TTable { virtual TTable(); }; My compiler, Borland Builder 5.2, has system libraries
7
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
9
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
6
by: DeveloperX | last post by:
In an attempt to solve a problem further down I suggested the problem might be caused by a missing constructor. Now, this led me to the conclusion that I don't fully understand constructors, or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.