Re: Inheirting constructor?

Discussion in 'C++' started by jbo5112, Nov 12, 2010.

  1. jbo5112

    jbo5112 Guest

    On Nov 12, 8:17 am, Steve Keller <> wrote:
    > Is is possible to inherit a constructor?  I have an abstract base
    > class with lots of derived classes.
    >
    > class Base {
    > protected:
    >         int a, b;
    > public:
    >         Base(int a, int b) : a(a), b(b) {}
    >         virtual int foo() = 0;
    >
    > };
    >
    > class A : public Base {
    > public:
    >         A(int a, int b) : Base(a, b) {}
    >         virtual int foo();
    >
    > };
    >
    > int A::foo()
    > {
    >         // do something with a and b
    >         return a + b;
    >
    > }
    >
    > Since I have lots of classes derived from Base it gets tedious to
    > write all the constructors for the derived classes.  Is is possible to
    > skip the definition of A::A(int a, int b) and have it some inherited
    > automatically from Base?
    >
    > --- news://freenews.netfront.net/ - complaints: ---


    You can probably accomplish what you want with templates. Here's an
    example program.




    #include <iostream>

    enum children_classes { c1, c2 };

    template< children_classes T >
    class Base {
    protected:
    int a, b;
    public:
    Base(int a, int b) : a(a), b(b) {}
    int foo();
    };

    template<>
    int Base<c1>::foo()
    {
    // do something with a and b
    return a + b;
    }

    template<>
    int Base<c2>::foo()
    {
    // do something with a and b
    return a * b;
    }

    typedef Base<c1> class1;
    typedef Base<c2> class2;

    int main() {
    class1 obj1(2,3);
    class2 obj2(2,3);
    std::cout << obj1.foo() << std::endl;
    std::cout << obj2.foo() << std::endl;
    }
     
    jbo5112, Nov 12, 2010
    #1
    1. Advertising

  2. On 11/12/2010 9:02 PM, jbo5112 wrote:
    >
    > You can probably accomplish what you want with templates. Here's an
    > example program.
    >
    >
    >
    >
    > #include<iostream>
    >
    > enum children_classes { c1, c2 };
    >
    > template< children_classes T>
    > class Base {
    > protected:
    > int a, b;
    > public:
    > Base(int a, int b) : a(a), b(b) {}
    > int foo();
    > };
    >
    > template<>
    > int Base<c1>::foo()
    > {
    > // do something with a and b
    > return a + b;
    > }
    >
    > template<>
    > int Base<c2>::foo()
    > {
    > // do something with a and b
    > return a * b;
    > }
    >
    > typedef Base<c1> class1;
    > typedef Base<c2> class2;
    >
    > int main() {
    > class1 obj1(2,3);
    > class2 obj2(2,3);
    > std::cout<< obj1.foo()<< std::endl;
    > std::cout<< obj2.foo()<< std::endl;
    > }


    But as far as i understand that's not true inheritance. Rather you
    defined interface Base<T> and implemented it in Base<c1>, Base<c2>. And
    I can not figure out how are you going to use non-static polymorphism.
    clarify please.
     
    Ruslan Mullakhmetov, Nov 13, 2010
    #2
    1. Advertising

  3. jbo5112

    jbo5112 Guest

    On Nov 13 2010, 2:26 am, Ruslan Mullakhmetov <>
    wrote:
    > On 11/12/2010 9:02 PM, jbo5112 wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > You can probably accomplish what you want with templates.  Here's an
    > > example program.

    >
    > > #include<iostream>

    >
    > > enum children_classes { c1, c2 };

    >
    > > template<  children_classes T>
    > > class Base {
    > > protected:
    > >          int a, b;
    > > public:
    > >          Base(int a, int b) : a(a), b(b) {}
    > >          int foo();
    > > };

    >
    > > template<>
    > > int Base<c1>::foo()
    > > {
    > >          // do something with a and b
    > >          return a + b;
    > > }

    >
    > > template<>
    > > int Base<c2>::foo()
    > > {
    > >          // do something with a and b
    > >          return a * b;
    > > }

    >
    > > typedef Base<c1>     class1;
    > > typedef Base<c2>     class2;

    >
    > > int main() {
    > >      class1      obj1(2,3);
    > >      class2      obj2(2,3);
    > >      std::cout<<  obj1.foo()<<  std::endl;
    > >      std::cout<<  obj2.foo()<<  std::endl;
    > > }

    >
    > But as far as i understand that's not true inheritance. Rather you
    > defined interface Base<T> and implemented it in Base<c1>, Base<c2>. And
    > I can not figure out how are you going to use non-static polymorphism.
    > clarify please.


    It's probably too late to be useful here anymore, but since nearly
    everything is forever archived and indexed by Googl (not that I know
    how they'd find it)e, here's my answer. No, it's not true
    inheritance, but with careful construction and creative use of
    includes I managed to simulate parent class, child classes and
    grandchild classes with such templates. Personally, I avoid run-time
    polymorphism for code that doesn't incur unnecessary overhead, and I
    find templates intriguing. If you want to use non-static
    polymorphism, you will need some actual inheritance. Here's the
    solution, complete with default functions and tested with g++.


    #include <iostream>

    class Parent {
    protected:
    int a, b;
    public:
    Parent(int a, int b) : a(a), b(b) {}
    virtual int foo()=0;
    virtual int bar(int x, int y)=0;
    };

    enum children_classes { c1, c2, c3 };
    template< children_classes T >
    class Base : public Parent {
    public:
    Base(int a, int b) : Parent(a,b) {}
    int foo() {return a/b;}
    int bar(int x, int y) {return x/y;}
    };

    template<>
    int Base<c1>::foo() {
    // do something with a and b
    return a + b;
    }

    template<>
    int Base<c2>::foo() {
    // do something with a and b
    return a * b;
    }

    template<>
    int Base<c1>::bar( int x, int y ) {
    // do something with x and y
    return x + y;
    }

    template<>
    int Base<c2>::bar( int x, int y ) {
    // do something with x and y
    return x * y;
    }

    typedef Base<c1> class1;
    typedef Base<c2> class2;
    typedef Base<c3> class3;
    int main() {
    class1 obj1(4,2);
    class2 obj2(4,2);
    class3 obj3(4,2);
    Parent *obj4;
    char input;
    std::cout << "Input 1 to point to obj1, or 2 to point to obj2\n";
    while( std::cin.get(input).good() ) {
    if( input>='1' && input<='3' ) {
    if( input=='1' ) {
    obj4=&obj1;
    std::cout << "obj4 points to obj1\n";
    }
    else if( input=='2' ){
    obj4=&obj2;
    std::cout << "obj4 points to obj2\n";
    }
    else {
    obj4=&obj3;
    std::cout << "obj4 points to obj3\n";
    }
    std::cout
    << "obj1.foo()=" << obj1.foo()
    << "\tobj2.foo()=" << obj2.foo()
    << "\tobj3.foo()=" << obj3.foo()
    << "\nobj4.foo()=" << obj4->foo()
    << "\nobj1.bar()=" << obj1.bar(4,5)
    << "\tobj2.bar()=" << obj2.bar(4,5)
    << "\tobj3.bar()=" << obj3.bar(4,5)
    << "\nobj4.bar()=" << obj4->bar(4,5) << "\n\n";
    }
    }
    }
     
    jbo5112, Jan 5, 2011
    #3
    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. Giulio
    Replies:
    9
    Views:
    1,064
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,346
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,860
    David Harmon
    Jan 23, 2004
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,323
  5. Alex

    Prevent controls from inheirting ID of parent

    Alex, Sep 29, 2004, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    163
Loading...

Share This Page