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 4 1509
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
On 10 Apr 2006 07:49:34 -0700, "markpark" <ma*******@veri zon.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**********@Ho me.com
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();
}
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 (???). This is what it seems to me. I have this declarations:
class wxFoldWindowItem
{
private:
wxWindow *_wnd;
|
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 kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
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
|
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.
|
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 then I would consider this a serious error in the language
implementation,
not to mention a pain in the backside :(
Also, is it to much to ask that the method Type.GetTypeFromCLSID be
documented
| |
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 that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.
|
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 work right,
although I quite don't see what is does in addition to the 2nd constructor.
Also, the example works fine without message calls in either constructor
(the numerical answer is still there and correct!).
I exected it to no longer work...
|
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 permutations
for constructors (none, default, two argument) and method call in body of
constructor (none and one).
Maybe this example is not representative, but for this example I found the
following:
1. Without any constructors, the program works fine...
|
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 have made
lots of assumptions.
Things I (think) know, please correct as necessary:
Constructors are called on instantiation.
Static constructors are called the first time a class is referenced.
There can be many constructors and you can chain...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |