Calling base class constructor from derived class Copy constructor

A

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
 
I

Ian Collins

ali said:
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.
 
A

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.

So you don't have to do anything.
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* ali:

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 )
{}
 
D

David Harmon

On 5 Mar 2007 00:26:02 -0800 in comp.lang.c++, "ali"
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)
{ }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top