derived class incresing the sopce of base class methods and explicit call to constructor

Discussion in 'C++' started by Taran, Apr 19, 2006.

  1. Taran

    Taran Guest

    Hi All,

    I tried something with the C++ I know and some things just seem
    strange.

    consider:

    #include <iostream>

    using namespace std;

    class base
    {
    public:

    base();
    void some_func_2();
    private:
    void some_func();
    };

    ///////////////////////////
    base::base()
    {
    cout<<"base constructor"<<endl;
    }
    ///////////////////////////

    void base::some_func()
    {
    cout<<"base some func"<<endl;
    }
    ///////////////////////////

    void base::some_func_2()
    {
    cout<<"base some_func_2"<<endl;
    }
    ///////////////////////////

    class derived: public base
    {
    public:
    void some_func();
    static void some_func_2();

    };


    void derived::some_func()
    {
    cout<<"derived some func"<<endl;
    }
    ///////////////////////////

    void derived::some_func_2()
    {
    cout<<"derived some_func_2"<<endl;
    }
    ///////////////////////////

    void main()
    {
    base b;
    b.some_func_2();
    base::base();
    derived d;
    d.some_func();
    derived::some_func_2();
    d.some_func_2();

    derived *pd;
    pd = reinterpret_cast<derived*>(new base());

    // derived *pd;
    // pd = new base(); /// This will cause errors if
    uncommented.

    }

    Here's the output:

    base constructor
    base some_func_2
    base constructor
    base constructor
    derived some func
    derived some_func_2
    derived some_func_2
    base constructor


    My problems here are that

    1. I understood that derived class cannot increase the scope of the
    base class methods. In this case 'derived' increased the scope of the
    base class private method 'some_func' by making it public and the scope
    of base class 'some_func_2' by making it static. Is my understading
    incorrect or there's something wrong?

    2. The constructor for the base class can be called explicitly, even
    though I cannot do anything more with that. i fail to understand why
    should it be allowed. Are there any special circumstances where this is
    used?

    3. Base class handle can take derived class objects but not the other
    way around. Casting will remove this error but why doesn't implicit
    cast like the previous one work?


    I was using Microsoft Visual Studio 6.0.

    Thanks in advance,

    Regards,
    -- Taran
     
    Taran, Apr 19, 2006
    #1
    1. Advertising

  2. Taran

    Bo Persson Guest

    "Taran" <> skrev i meddelandet
    news:...
    > Hi All,
    >
    > I tried something with the C++ I know and some things just seem
    > strange.
    >
    > consider:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class base
    > {
    > public:
    >
    > base();
    > void some_func_2();
    > private:
    > void some_func();
    > };
    >
    > ///////////////////////////
    > base::base()
    > {
    > cout<<"base constructor"<<endl;
    > }
    > ///////////////////////////
    >
    > void base::some_func()
    > {
    > cout<<"base some func"<<endl;
    > }
    > ///////////////////////////
    >
    > void base::some_func_2()
    > {
    > cout<<"base some_func_2"<<endl;
    > }
    > ///////////////////////////
    >
    > class derived: public base
    > {
    > public:
    > void some_func();
    > static void some_func_2();
    >
    > };
    >
    >
    > void derived::some_func()
    > {
    > cout<<"derived some func"<<endl;
    > }
    > ///////////////////////////
    >
    > void derived::some_func_2()
    > {
    > cout<<"derived some_func_2"<<endl;
    > }
    > ///////////////////////////
    >
    > void main()
    > {
    > base b;
    > b.some_func_2();
    > base::base();
    > derived d;
    > d.some_func();
    > derived::some_func_2();
    > d.some_func_2();
    >
    > derived *pd;
    > pd = reinterpret_cast<derived*>(new base());
    >
    > // derived *pd;
    > // pd = new base(); /// This will cause errors if
    > uncommented.
    >
    > }
    >
    > Here's the output:
    >
    > base constructor
    > base some_func_2
    > base constructor
    > base constructor
    > derived some func
    > derived some_func_2
    > derived some_func_2
    > base constructor
    >
    >
    > My problems here are that
    >
    > 1. I understood that derived class cannot increase the scope of the
    > base class methods. In this case 'derived' increased the scope of
    > the
    > base class private method 'some_func' by making it public and the
    > scope
    > of base class 'some_func_2' by making it static. Is my understading
    > incorrect or there's something wrong?


    The functions defined in derived hides the functions in the class
    base. It's a another set of functions, with the same names. It doesn't
    affect the functions of the base class.

    >
    > 2. The constructor for the base class can be called explicitly, even
    > though I cannot do anything more with that. i fail to understand why
    > should it be allowed. Are there any special circumstances where this
    > is
    > used?


    Not very useful, but also not specifically forbidden by the standard.
    Why should it be?

    >
    > 3. Base class handle can take derived class objects but not the
    > other
    > way around.


    In C++ it is called a pointer, not a handle. A base class pointer can
    point to a derived class, because there is a base part in the derived
    class. A derived class can act as its base, because it has inherited
    (some of) its properties.

    > Casting will remove this error but why doesn't implicit
    > cast like the previous one work?


    Casting doesn't remove the error, it just tells the compiler to shut
    up -- "Trust me, I know what I'm doing!".

    If that isn't really true, all kinds of nasty things are likely to
    happen. :)


    Bo Persson
     
    Bo Persson, Apr 19, 2006
    #2
    1. Advertising

  3. Taran wrote:
    > Hi All,
    >
    > I tried something with the C++ I know and some things just seem
    > strange.
    >
    > consider:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class base
    > {
    > public:
    >
    > base();
    > void some_func_2();
    > private:
    > void some_func();
    > };
    >
    > ///////////////////////////
    > base::base()
    > {
    > cout<<"base constructor"<<endl;
    > }
    > ///////////////////////////
    >
    > void base::some_func()
    > {
    > cout<<"base some func"<<endl;
    > }
    > ///////////////////////////
    >
    > void base::some_func_2()
    > {
    > cout<<"base some_func_2"<<endl;
    > }
    > ///////////////////////////
    >
    > class derived: public base
    > {
    > public:
    > void some_func();
    > static void some_func_2();
    >
    > };
    >
    >
    > void derived::some_func()
    > {
    > cout<<"derived some func"<<endl;
    > }
    > ///////////////////////////
    >
    > void derived::some_func_2()
    > {
    > cout<<"derived some_func_2"<<endl;
    > }
    > ///////////////////////////
    >
    > void main()
    > {
    > base b;
    > b.some_func_2();
    > base::base();
    > derived d;
    > d.some_func();
    > derived::some_func_2();
    > d.some_func_2();
    >
    > derived *pd;
    > pd = reinterpret_cast<derived*>(new base());
    >
    > // derived *pd;
    > // pd = new base(); /// This will cause errors if
    > uncommented.
    >
    > }
    >
    > Here's the output:
    >
    > base constructor
    > base some_func_2
    > base constructor
    > base constructor
    > derived some func
    > derived some_func_2
    > derived some_func_2
    > base constructor
    >
    >
    > My problems here are that
    >
    > 1. I understood that derived class cannot increase the scope of the


    Not sure what you mean here by "increase the scope"...

    > base class methods. In this case 'derived' increased the scope of the
    > base class private method 'some_func' by making it public


    No, it didn't. 'derived' has its own function 'some_func' that is not
    related to the function named the same in the 'base' class.

    > and the
    > scope of base class 'some_func_2' by making it static. Is my
    > understading incorrect or there's something wrong?


    Nothing changes the scope of nothing here. Two new functions introduced
    by the 'derived' type. Both hide the functions with the same name from
    the 'base' class.

    > 2. The constructor for the base class can be called explicitly, even


    No. The syntax you used creates a temporary object (and discards it
    promptly). Yes, it causes the constructor to be called, but it doesn't
    mean that you can call a construtor.

    > though I cannot do anything more with that. i fail to understand why
    > should it be allowed. Are there any special circumstances where this
    > is used?


    Yes. If you want to create a temporary object and use it.

    > 3. Base class handle


    What's that?

    > can take derived class objects but not the other
    > way around.


    You mean, a pointer to a derived class can be converted to a pointer
    of a base class? Yes. A couple conditions apply, but not in this case.

    > Casting will remove this error but why doesn't implicit
    > cast like the previous one work?


    Which error? Which implicit cast? Which one is "previous"?

    > I was using Microsoft Visual Studio 6.0.


    You should get yourself VC++ Express 2005 and enjoy a much better
    compiler (although it shouldn't matter here).

    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 19, 2006
    #3
  4. I think you are a newer,you have a long way to go.
     
    =?utf-8?B?5rW36aOO?=, Apr 20, 2006
    #4
  5. Taran

    benben Guest

    Re: derived class incresing the sopce of base class methods and explicitcall to constructor

    > #include <iostream>
    >
    > using namespace std;
    >
    > class base
    > {
    > public:
    >
    > base();
    > void some_func_2();
    > private:
    > void some_func();
    > };
    >
    > ///////////////////////////
    > base::base()
    > {
    > cout<<"base constructor"<<endl;
    > }
    > ///////////////////////////
    >
    > void base::some_func()
    > {
    > cout<<"base some func"<<endl;
    > }
    > ///////////////////////////
    >
    > void base::some_func_2()
    > {
    > cout<<"base some_func_2"<<endl;
    > }
    > ///////////////////////////
    >
    > class derived: public base
    > {
    > public:
    > void some_func();
    > static void some_func_2();
    >
    > };
    >
    >
    > void derived::some_func()
    > {
    > cout<<"derived some func"<<endl;
    > }
    > ///////////////////////////
    >
    > void derived::some_func_2()
    > {
    > cout<<"derived some_func_2"<<endl;
    > }
    > ///////////////////////////
    >
    > void main()
    > {
    > base b;
    > b.some_func_2();
    > base::base();
    > derived d;
    > d.some_func();
    > derived::some_func_2();
    > d.some_func_2();
    >
    > derived *pd;
    > pd = reinterpret_cast<derived*>(new base());
    >
    > // derived *pd;
    > // pd = new base(); /// This will cause errors if
    > uncommented.


    Of course it is an error. Don't treat a fruit as an apple, because it
    may not be. Treat an apple as a fruit, a derived as a base, not the
    other way round.

    >
    > }
    >
    > Here's the output:
    >
    > base constructor
    > base some_func_2
    > base constructor
    > base constructor
    > derived some func
    > derived some_func_2
    > derived some_func_2
    > base constructor
    >
    >
    > My problems here are that
    >
    > 1. I understood that derived class cannot increase the scope of the
    > base class methods. In this case 'derived' increased the scope of the
    > base class private method 'some_func' by making it public and the scope
    > of base class 'some_func_2' by making it static. Is my understading
    > incorrect or there's something wrong?


    The class derive does not and cannot alter the definition of base. Hence
    the "scope" of base can neither be "increased" nor "decreased".

    The base class has 3 members:
    * the constructor base::base
    * base::some_func
    * base::some_func_2

    The derived class has 5 members:
    * the constructor derived::derived
    * static derived::some_func_2
    * derived::some_func
    * base::some_func
    * base::some_func2

    The derived::some_func simply hides base::some_func in class derived.

    >
    > 2. The constructor for the base class can be called explicitly, even
    > though I cannot do anything more with that. i fail to understand why
    > should it be allowed. Are there any special circumstances where this is
    > used?


    It cannot. Detail answered in other posts.

    >
    > 3. Base class handle can take derived class objects but not the other
    > way around. Casting will remove this error but why doesn't implicit
    > cast like the previous one work?


    If what you mean by "handle" is what we call "pointer"...

    It is true that a pointer to base can take a pointer to derived. But the
    one you commented was doing it the other way round, which doesn't work.

    base* p = new derived; // OK
    dervied* q = new base; // ERROR

    derived* r = p; // ERROR, no implicit conversion supplied
    derived* s = static_case<derived*>(p); //OK, explicit conversion
    >
    >
    > I was using Microsoft Visual Studio 6.0.
    >
    > Thanks in advance,
    >
    > Regards,
    > -- Taran
    >


    Regards,
    Ben
     
    benben, Apr 20, 2006
    #5
  6. Taran

    BobR Guest

    Re: derived class incresing the sopce of base class methods and explicitcall to constructor

    benben wrote in message <44470981$0$7527$>...
    >>
    >> 3. Base class handle can take derived class objects but not the other
    >> way around. Casting will remove this error but why doesn't implicit
    >> cast like the previous one work?

    >
    >If what you mean by "handle" is what we call "pointer"...
    >
    >It is true that a pointer to base can take a pointer to derived. But the
    >one you commented was doing it the other way round, which doesn't work.
    >
    > base* p = new derived; // OK


    OK?? (See what I was going to post (below) and correct me or comment,
    please.)

    > dervied* q = new base; // ERROR
    >
    > derived* r = p; // ERROR, no implicit conversion supplied
    > derived* s = static_case<derived*>(p); //OK, explicit conversion



    [ was to the OP ]
    // -------------------
    [ others have answered your questions, so, I'll just nit-pick a bit.]

    In your main() you made the comment " // This will cause errors if
    uncommented.". Let's be consistant.

    // void main()
    int main(){
    base b;
    b.some_func_2();
    base::base();
    // derived d;
    // d.some_func();
    // derived::some_func_2();
    // d.some_func_2();

    // derived *pd;
    // pd = reinterpret_cast<derived*>(new base());

    // derived *pd;
    // pd = new base(); // This will cause errors if uncommented.

    // delete pd;
    return 0;
    }

    There! That's better.
    Why? Try this:

    class base{ public:
    base();

    // add this:
    ~base(); // test, then comment this Dtor and....
    // virtual ~base(); // uncomment this Dtor

    void some_func_2();
    private:
    void some_func();
    };

    ///////////////////////////
    base::~base(){
    cout<<" ~base destructor "<<endl;
    }
    ///////////////////////////

    // add a destructor (non-virtual, with output) to your 'derived' class also.
    // -- rest of your program same --

    In your 'toy' program it won't hurt you much, but, in a big project it could
    be a big problem.
    [ 'it' being the missing 'virtual destructor' in base class. ]
    // -------------------

    So, am I right, or out in left field somewhere?

    --
    Bob R
    POVrookie
     
    BobR, Apr 20, 2006
    #6
  7. Taran

    Default User Guest

    海风 wrote:

    > I think you are a newer,you have a long way to go.


    A newer what? And who are you talking to?


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Apr 20, 2006
    #7
    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:
    623
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    629
    David Harmon
    Mar 5, 2007
  3. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    412
    Victor Bazarov
    May 23, 2007
  5. Rahul
    Replies:
    16
    Views:
    1,250
    terminator
    Nov 7, 2007
Loading...

Share This Page