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

Calling base class constructor from derived class Copy constructor

ali
Hi,

I am new to C++ and trying to understand how to work on Inheritance
and Operator overloading. I understand that the derived class can pass
the base class constructor in its constructor definition as following
code:

Car::Car(int id, int colorID, int type):Vehicle(id, colorID)
{
this->type = type;
}

What I am having difficulty with, is, how do I call the base class
constructor when writing the copy constructor for the derived class?

Example:

Car::Car(const Car &rhs)
{
//my code
}

Can I just add it as:

Car::Car(const Car &rhs):Vehicle(rhs.getID, rhs.getColor)
{
this->type = rhs.type;
}

I'm not sure of the above code copy constructor code accuracy, but
would appreciate some guidance.

Thank you!

Ali

Mar 5 '07 #1
4 14783
ali wrote:
Hi,

I am new to C++ and trying to understand how to work on Inheritance
and Operator overloading. I understand that the derived class can pass
the base class constructor in its constructor definition as following
code:

Car::Car(int id, int colorID, int type):Vehicle(id, colorID)
{
this->type = type;
}
Better to use an initialisation list:

ar::Car(int id, int colorID, int type)
: Vehicle(id, colorID), type(type) {}
What I am having difficulty with, is, how do I call the base class
constructor when writing the copy constructor for the derived class?
You can't. If you have some initialisation code you want to share
between constructors, and an initialise method and call it as required.

--
Ian Collins.
Mar 5 '07 #2
* ali:
Hi,

I am new to C++ and trying to understand how to work on Inheritance
and Operator overloading. I understand that the derived class can pass
the base class constructor in its constructor definition as following
code:

Car::Car(int id, int colorID, int type):Vehicle(id, colorID)
{
this->type = type;
}

What I am having difficulty with, is, how do I call the base class
constructor when writing the copy constructor for the derived class?

Example:

Car::Car(const Car &rhs)
{
//my code
}

Can I just add it as:

Car::Car(const Car &rhs):Vehicle(rhs.getID, rhs.getColor)
{
this->type = rhs.type;
}

I'm not sure of the above code copy constructor code accuracy, but
would appreciate some guidance.
You can just add
Car::Car( Car const& other ): Vehicle( other )
{}

But that's what the compiler does for you if you don't declare a copy
constructor.

So you don't have to do anything.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 5 '07 #3
* Alf P. Steinbach:
* ali:
>Hi,

I am new to C++ and trying to understand how to work on Inheritance
and Operator overloading. I understand that the derived class can pass
the base class constructor in its constructor definition as following
code:

Car::Car(int id, int colorID, int type):Vehicle(id, colorID)
{
this->type = type;
}

What I am having difficulty with, is, how do I call the base class
constructor when writing the copy constructor for the derived class?

Example:

Car::Car(const Car &rhs)
{
//my code
}

Can I just add it as:

Car::Car(const Car &rhs):Vehicle(rhs.getID, rhs.getColor)
{
this->type = rhs.type;
}

I'm not sure of the above code copy constructor code accuracy, but
would appreciate some guidance.

You can just add
Car::Car( Car const& other ): Vehicle( other )
{}

But that's what the compiler does for you if you don't declare a copy
constructor.
Uh, more members in Car, yes, didn't see that, should be

Car::Car( Car const& other ): Vehicle( other ), type( other.type )
{}

>
So you don't have to do anything.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 5 '07 #4
On 5 Mar 2007 00:26:02 -0800 in comp.lang.c++, "ali"
<al*************@gmail.comwrote,
>Can I just add it as:

Car::Car(const Car &rhs):Vehicle(rhs.getID, rhs.getColor)
{
this->type = rhs.type;
}
Preferable, if Vehicle has suitable copy constructor already

Car::Car(const Car &rhs)
: Vehicle(rhs),
type(rhs.type)
{ }

Mar 5 '07 #5

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

Similar topics

8
by: Dave | last post by:
class base { public: base(const base &other) { // Init. here... } // Stuff }; class derived: public base { public:
3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
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; // ......
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
8
by: Mike - EMAIL IGNORED | last post by:
I have a class that may or may not be inherited. Its constructor calls a function that should be called only if the class is not inherited. Is there a way to tell, other than simply passing a...
9
by: cindypwl | last post by:
This is what I have class base{public:virtual ~base();}; class derived1 : public base {}; class derived2 : public base {}; class A { public: A(base *b_):b(b_) {
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
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?
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
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
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
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
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.