473,396 Members | 2,147 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,396 software developers and data experts.

derived class constructors

Hi,
I have a problem, any help would be much appreciated.

To simplify it for you I have tried to isolate some code. There is a
base class (class_a) and a derived class (class_b). Not very
intelligent names I know, but bear with me.. My problem is that I would
like to write a constructor for class_b based on an instance of
class_a. I have added the declaration for it, but how do I define
class_b(class_a&) ?????

Cheers
BOR

#define CLASS_A_N 8

class class_a
{
private:
int a;
int* g;
public:
class_a(void): a=0, g(NULL) {}
virtual ~class_a(void)
{
if ( g != NULL )
delete[] g;
}
class_a(class_a& rhs): g(NULL)
{
a = rhs.a;
g = new int[CLASS_A_N];
for ( int i = 0; i < CLASS_A_N; i++)
g[i] = rhs.g[i];
}
}

class class_b : public class_a
{
private:
int b, c;
public:
class_b(int local_b=0, int local_c=0): b(local_b), c(local_c) {}
~class_b(){};
class_b(class_a&);
}

Jul 23 '05 #1
4 1015
BlimeyOReilly wrote:
Hi,
I have a problem, any help would be much appreciated.

To simplify it for you I have tried to isolate some code. There is a
base class (class_a) and a derived class (class_b). Not very
intelligent names I know, but bear with me.. My problem is that I would
like to write a constructor for class_b based on an instance of
class_a. I have added the declaration for it, but how do I define
class_b(class_a&) ?????
Just like any other constructor. Which problem did you get?
#define CLASS_A_N 8
I'd make that a static const member variable of class_a instead of a macro.

class class_a
{
private:
int a;
int* g;
public:
class_a(void): a=0, g(NULL) {}
virtual ~class_a(void)
{
if ( g != NULL )
That check isn't necessary. delete[] on a null pointer is a no-op.
delete[] g;
}
class_a(class_a& rhs): g(NULL)
That constructor doesn't modify rhs, so it should take it as a const
reference.
{
a = rhs.a;
g = new int[CLASS_A_N];
for ( int i = 0; i < CLASS_A_N; i++)
g[i] = rhs.g[i];
}
}
;
class class_b : public class_a
{
private:
int b, c;
public:
class_b(int local_b=0, int local_c=0): b(local_b), c(local_c) {}
~class_b(){};
class_b(class_a&);
}


;

So what's the problem now? You have implemented other constructors. This on
is no different in that respect.

Jul 23 '05 #2
> So what's the problem now? You have implemented other constructors. This on
is no different in that respect.

Well I can write the outside ok. I can initialise the derived class
private members, but I can't seem to find the way to copy the base
class members across from &rhs (which yes should be const too).

BOR

Jul 23 '05 #3
BlimeyOReilly wrote:
So what's the problem now? You have implemented other constructors. This
on

is no different in that respect.

Well I can write the outside ok. I can initialise the derived class
private members, but I can't seem to find the way to copy the base
class members across from &rhs (which yes should be const too).


Ah, I think now I see the problem. Your class_a has a copy constructor, so
you can just use that. Try something like:

class_b::class_b(const class_a& rhs)
: class_a(rhs),
... initialization of class_b members
{
}

You cannot directly access rhs's members from class_b's constructor, because
they are private.

Jul 23 '05 #4
Perfect!!
I hadn't realised you were allowed to specify base class parts by name
in the initialisation list. I knew there had to be a simple solution
to the problem - thank you so much!
BOR

(go RedHotPawn.com)

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
3
by: zlf | last post by:
I am asked to complete a COM+ component, there is a class A derived from ServicedComponent. However, when executing , exception is thrown. Messaged: Unhandled Exception:...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
4
by: nozyrev | last post by:
Hi, I apologize in advance if this is a very dumb question. I've been struggling with this problem for some time: I have an abstract base class called Base and n derived classes D1, D2, ....Dn. I...
2
by: Ethan Strauss | last post by:
Hi, I want to be able to make a Master constructor for a class which all overloads of the class constructor would call and thus if I have minor changes to something I only need to make them in the...
6
by: Peng Yu | last post by:
Hi, I want B has all the constructors that A has. Obviously, the code below would not work. I could define a corresponding B's constructor for each A's constructor. But if A has many...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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
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...
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,...

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.