473,725 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(i d, 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(r hs.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 14797
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(i d, 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(i d, 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(r hs.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(i d, 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(con st Car &rhs)
{
//my code
}

Can I just add it as:

Car::Car(con st Car &rhs):Vehicle(r hs.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.comwrot e,
>Can I just add it as:

Car::Car(con st Car &rhs):Vehicle(r hs.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
1753
by: Dave | last post by:
class base { public: base(const base &other) { // Init. here... } // Stuff }; class derived: public base { public:
3
2346
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 class definition. Therefore I can't specify the return type of the function, that returns a pointer to the derived class, because the derived class is not yet known at that point. Hov can I solve this problem? In a nice manner :)
6
2106
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 (presuming the base constructor took an int): Derived::Derived(int a) : Base(a)
3
3493
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; // ... more protected fields }
5
3432
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
12
5809
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. The subclass's constructor sets this variable to a real value. So first the variable is set to 0, the variable is never used before it once again gets set to a real value. It's a waste. Any ideas anyone?
8
3677
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 bool argument? Thanks for your suggestions. Mike.
9
2355
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
2231
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 modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
0
8889
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, 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...
0
8752
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,...
0
9401
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, 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...
1
9179
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,...
0
6011
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();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
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
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.