none
Copy Constructors for a Derived Class RRS feed

  • General discussion

  •  G'day,

    I was just wondering if there was a simple way of employing copy constructors in base classes for derived classes.

    If we were to consider some class say class1, with attributes int a1 that possessed two constructors and a copy constructor i.e.

    public class class1
    {
        private int a1;
        //constructors
        public class1()
        {
            a1 = 0;
        }
        public class1(int A1)
    `{
            a1 = A1;
    `}
      //copy constructor
      public class1(class1 c)
    `{
           a1 = c.a1; 
    `}
    }

    And now include a derived class class2 with additional attribute a2  with constructors for each case and a copy constructori.e.

    public class class2:class1
    {
        private int a2

        //constructors
        //.......
       //copy constructor
       public class2(class2 c2)
       {
            a1 = c2.a1 //inherited property from class1
            a2 = c2.a2 //additional property for derived class class2
       }
    }
    Here we observe that the copy constructor is simple enough as the base class has only one attribute a1, however what is there was a large number of attributes (as im dealing with at the moment (12)) is there anyway we can use the copy constructor of the base class for the copy constructor in the derived class??

    Any help would be greatly appreciated,

    David
    Tuesday, June 3, 2008 10:20 AM