Alternative to Abtract Class?

Discussion in 'C++' started by Immortal Nephi, Apr 16, 2009.

  1. I want to know. Is first version of abtract class the same as second
    version of class with protected constructor() and destructor()? Here
    is an example. You can't use class A so class B is used to derive
    from class A. Also, you can't use class A2 with protected constructor
    () so class B2 is used to derive from class A2.

    #include <iostream>
    using std::cout;
    using std::endl;

    class A
    {
    public:
    A() : m_x(0) { cout << "Constructor A" << endl; }
    ~A() { cout << "Destructor A" << endl; }

    void set(int x) { m_x = x; }
    int get() { return m_x; }

    virtual void print() = 0;

    private:
    int m_x;
    };

    class B : public A
    {
    public:
    B() : A() { cout << "Constructor B" << endl; }
    ~B() { cout << "Constructor B" << endl; }

    void print() { cout << "m_x: " << get() << endl; }
    };

    class A2
    {
    protected:
    A2() : m_x(0) { cout << "Constructor A2" << endl; }
    ~A2() { cout << "Destructor A2" << endl; }

    int m_x;
    };

    class B2 : public A2
    {
    public:
    B2() : A2() { cout << "Constructor B2" << endl; }
    ~B2() { cout << "Destructor B2" << endl; }

    void set(int x) { A2::m_x = x; }
    int get() { return m_x; }

    void print() { cout << "m_x: " << get() << endl; }
    };

    int main5()
    {
    // Compilation Error because of abtract class
    // A a;
    // a.set( 5 );
    // a.print();

    B b;
    b.set( 5 );
    b.print();

    // Compilation Error because of protected constructor
    // A2 a2;

    B2 b2;
    b2.set( 10 );
    b2.print();

    return 0;
    }

    Thanks....
     
    Immortal Nephi, Apr 16, 2009
    #1
    1. Advertising

  2. Immortal Nephi wrote:
    > I want to know. Is first version of abtract class the same as second
    > version of class with protected constructor() and destructor()?


    Of course not. An abstract class cannot be instantiated, no matter
    where you are. A class with protected constructor/destructor can still
    be instantiated (perhaps by mistake) in a member function of itself or
    any of its descendants.

    > Here
    > is an example. You can't use class A so class B is used to derive
    > from class A. Also, you can't use class A2 with protected constructor
    > () so class B2 is used to derive from class A2.
    >
    > #include <iostream>
    > using std::cout;
    > using std::endl;
    >
    > class A
    > {
    > public:
    > A() : m_x(0) { cout << "Constructor A" << endl; }
    > ~A() { cout << "Destructor A" << endl; }
    >
    > void set(int x) { m_x = x; }
    > int get() { return m_x; }
    >
    > virtual void print() = 0;
    >
    > private:
    > int m_x;
    > };
    >
    > class B : public A
    > {
    > public:
    > B() : A() { cout << "Constructor B" << endl; }
    > ~B() { cout << "Constructor B" << endl; }
    >
    > void print() { cout << "m_x: " << get() << endl; }
    > };
    >
    > class A2
    > {
    > protected:
    > A2() : m_x(0) { cout << "Constructor A2" << endl; }
    > ~A2() { cout << "Destructor A2" << endl; }
    >
    > int m_x;
    > };
    >
    > class B2 : public A2
    > {
    > public:
    > B2() : A2() { cout << "Constructor B2" << endl; }
    > ~B2() { cout << "Destructor B2" << endl; }
    >
    > void set(int x) { A2::m_x = x; }
    > int get() { return m_x; }
    >
    > void print() { cout << "m_x: " << get() << endl; }
    > };
    >
    > int main5()
    > {
    > // Compilation Error because of abtract class
    > // A a;
    > // a.set( 5 );
    > // a.print();
    >
    > B b;
    > b.set( 5 );
    > b.print();
    >
    > // Compilation Error because of protected constructor
    > // A2 a2;
    >
    > B2 b2;
    > b2.set( 10 );
    > b2.print();
    >
    > return 0;
    > }
    >
    > Thanks....


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 16, 2009
    #2
    1. Advertising

  3. On Apr 16, 2:49 pm, Victor Bazarov <> wrote:
    > Immortal Nephi wrote:
    > > I want to know.  Is first version of abtract class the same as second
    > > version of class with protected constructor() and destructor()?

    >
    > Of course not.  An abstract class cannot be instantiated, no matter
    > where you are.  A class with protected constructor/destructor can still
    > be instantiated (perhaps by mistake) in a member function of itself or
    > any of its descendants.


    What do word "instantiated" mean? Class A becomes abtract class. It
    contains data members and function members inside class A. You can't
    define "A a;" and "a.set( 5);". Abtract class does not allow you to
    initialize data members through function members. It is like a name
    without existing object. You must derive subclass from abtract class
    if you want subclass to become existing object. Correct?

    Thanks...


    >
    >  >  Here
    >
    >
    >
    >
    >
    > > is an example.  You can't use class A so class B is used to derive
    > > from class A.  Also, you can't use class A2 with protected constructor
    > > () so class B2 is used to derive from class A2.

    >
    > > #include <iostream>
    > > using std::cout;
    > > using std::endl;

    >
    > > class A
    > > {
    > > public:
    > >    A() : m_x(0) { cout << "Constructor A" << endl; }
    > >    ~A() { cout << "Destructor A" << endl; }

    >
    > >    void set(int x) { m_x = x; }
    > >    int get() { return m_x; }

    >
    > >    virtual void print() = 0;

    >
    > > private:
    > >    int m_x;
    > > };

    >
    > > class B : public A
    > > {
    > > public:
    > >    B() : A() { cout << "Constructor B" << endl; }
    > >    ~B() { cout << "Constructor B" << endl; }

    >
    > >    void print() { cout << "m_x: " << get() << endl; }
    > > };

    >
    > > class A2
    > > {
    > > protected:
    > >    A2() : m_x(0) { cout << "Constructor A2" << endl; }
    > >    ~A2() { cout << "Destructor A2" << endl; }

    >
    > >    int m_x;
    > > };

    >
    > > class B2 : public A2
    > > {
    > > public:
    > >    B2() : A2() { cout << "Constructor B2" << endl; }
    > >    ~B2() { cout << "Destructor B2" << endl; }

    >
    > >    void set(int x) { A2::m_x = x; }
    > >    int get() { return m_x; }

    >
    > >    void print() { cout << "m_x: " << get() << endl; }
    > > };

    >
    > > int main5()
    > > {
    > > // Compilation Error because of abtract class
    > > // A a;
    > > // a.set( 5 );
    > > // a.print();

    >
    > >    B b;
    > >    b.set( 5 );
    > >    b.print();

    >
    > > // Compilation Error because of protected constructor
    > > // A2 a2;

    >
    > >    B2 b2;
    > >    b2.set( 10 );
    > >    b2.print();

    >
    > >    return 0;
    > > }

    >
    > > Thanks....

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >
    > - Show quoted text -
     
    Immortal Nephi, Apr 16, 2009
    #3
  4. Immortal Nephi wrote:
    > On Apr 16, 2:49 pm, Victor Bazarov <> wrote:
    >> Immortal Nephi wrote:
    >>> I want to know. Is first version of abtract class the same as second
    >>> version of class with protected constructor() and destructor()?

    >> Of course not. An abstract class cannot be instantiated, no matter
    >> where you are. A class with protected constructor/destructor can still
    >> be instantiated (perhaps by mistake) in a member function of itself or
    >> any of its descendants.

    >
    > What do word "instantiated" mean?


    It means that an object (instance) of that class is created.

    > Class A becomes abtract class.


    It doesn't *become* abstract. It *is* abstract. 'A2', however, isn't.

    > It
    > contains data members and function members inside class A. You can't
    > define "A a;" and "a.set( 5);". Abtract class does not allow you to
    > initialize data members through function members. It is like a name
    > without existing object. You must derive subclass from abtract class
    > if you want subclass to become existing object. Correct?


    Yes, correct. So, let's go back to your question, shall we? "Is first
    version of abtract class the same as second version...?" (sic) And the
    answer is "no". The "first version" is an abstract class. The "second
    version" is NOT. The true abstract class cannot be used to create a
    stand-alone object, no matter how you try doing it. The "second
    version" can still be used to create an object in the scope where the
    constructor and destructor are accessible, for example in a member
    function of a derived class.

    >
    > Thanks...


    You're welcome.

    >
    >
    >> > Here

    >>
    >>
    >>
    >>
    >>
    >>> is an example. You can't use class A so class B is used to derive
    >>> from class A. Also, you can't use class A2 with protected constructor
    >>> () so class B2 is used to derive from class A2.
    >>> #include <iostream>
    >>> using std::cout;
    >>> using std::endl;
    >>> class A
    >>> {
    >>> public:
    >>> A() : m_x(0) { cout << "Constructor A" << endl; }
    >>> ~A() { cout << "Destructor A" << endl; }
    >>> void set(int x) { m_x = x; }
    >>> int get() { return m_x; }
    >>> virtual void print() = 0;
    >>> private:
    >>> int m_x;
    >>> };
    >>> class B : public A
    >>> {
    >>> public:
    >>> B() : A() { cout << "Constructor B" << endl; }
    >>> ~B() { cout << "Constructor B" << endl; }
    >>> void print() { cout << "m_x: " << get() << endl; }
    >>> };
    >>> class A2
    >>> {
    >>> protected:
    >>> A2() : m_x(0) { cout << "Constructor A2" << endl; }
    >>> ~A2() { cout << "Destructor A2" << endl; }
    >>> int m_x;
    >>> };
    >>> class B2 : public A2
    >>> {
    >>> public:
    >>> B2() : A2() { cout << "Constructor B2" << endl; }
    >>> ~B2() { cout << "Destructor B2" << endl; }
    >>> void set(int x) { A2::m_x = x; }
    >>> int get() { return m_x; }
    >>> void print() { cout << "m_x: " << get() << endl; }
    >>> };
    >>> int main5()
    >>> {
    >>> // Compilation Error because of abtract class
    >>> // A a;
    >>> // a.set( 5 );
    >>> // a.print();
    >>> B b;
    >>> b.set( 5 );
    >>> b.print();
    >>> // Compilation Error because of protected constructor
    >>> // A2 a2;
    >>> B2 b2;
    >>> b2.set( 10 );
    >>> b2.print();
    >>> return 0;
    >>> }
    >>> Thanks....

    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >>
    >> - Show quoted text -

    >


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 17, 2009
    #4
    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. Bernd
    Replies:
    8
    Views:
    440
    Chris Uppal
    Nov 11, 2003
  2. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    784
    Pete Becker
    May 4, 2005
  4. Gary Coutts

    Alternative to WebClient Class

    Gary Coutts, Oct 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    772
    Gary Coutts
    Oct 4, 2006
  5. Joseph Turian
    Replies:
    5
    Views:
    627
Loading...

Share This Page