* 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?
|