Calling base class constructor from derived class Copy constructor

Discussion in 'C++' started by ali, Mar 5, 2007.

  1. ali

    ali Guest

    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
    ali, Mar 5, 2007
    #1
    1. Advertising

  2. ali

    Ian Collins Guest

    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.
    Ian Collins, Mar 5, 2007
    #2
    1. Advertising

  3. * 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?
    Alf P. Steinbach, Mar 5, 2007
    #3
  4. * 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?
    Alf P. Steinbach, Mar 5, 2007
    #4
  5. ali

    David Harmon Guest

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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrstephengross
    Replies:
    5
    Views:
    593
    Larry I Smith
    May 18, 2005
  2. Replies:
    1
    Views:
    393
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
  4. Rahul
    Replies:
    16
    Views:
    1,196
    terminator
    Nov 7, 2007
  5. Replies:
    2
    Views:
    706
Loading...

Share This Page