Classes, inheritance and over-riding

This is a discussion on Classes, inheritance and over-riding within the C++ Programming forums, part of the General Programming Boards category; I've C knowledge, but newish to C++. I am trying to learn Orge 3D, which heavily uses OOP. Can anyone ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    Classes, inheritance and over-riding

    I've C knowledge, but newish to C++. I am trying to learn Orge 3D, which heavily uses OOP.

    Can anyone explain the constructor of the following class?

    Code:
    class CameraTrackListener: public ExampleFrameListener
    {
    protected:
    public:
        CameraTrackListener(RenderWindow* win, Camera* cam)
            : ExampleFrameListener(win, cam)
        {
        }
    
        bool frameStarted(const FrameEvent& evt)
        {
    
            mAnimState->addTime(evt.timeSinceLastFrame);
    
            // Call default
            return ExampleFrameListener::frameStarted(evt);
            
    
        }
    };

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    Run this code and see what happens:
    Code:
    class A
    {
    public:
        A(int n) {cout << "A constructor - " << n << endl;}
    };
    
    class B : public A
    {
    public:
        B(int n) : A(n) {cout << "B constructor - " << n << endl;}
    };
    
    int main()
    {
       B b(5);
    
       return 0;
    }
    Then, get yourself a good C++ book. You're halfway there if you already know C.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    4
    Cheers dude, can you recommend a book?

    I've read the tutorials on this site, but they didn;t address this particular question

  4. #4
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Re: Classes, inheritance and over-riding

    Originally posted by charliemouse
    I've C knowledge, but newish to C++. I am trying to learn Orge 3D, which heavily uses OOP.

    Can anyone explain the constructor of the following class?

    Code:
    class CameraTrackListener: public ExampleFrameListener
    {
    protected:
    public:
        CameraTrackListener(RenderWindow* win, Camera* cam)
            : ExampleFrameListener(win, cam)
        {
        }
    
        bool frameStarted(const FrameEvent& evt)
        {
    
            mAnimState->addTime(evt.timeSinceLastFrame);
    
            // Call default
            return ExampleFrameListener::frameStarted(evt);
            
    
        }
    };
    The constructor of CameraTrackListener is calling a constructor of
    ExampleFrameListener (which is a base class that has its own
    constructors) in its initialization stage... I'm thinking, anyway.
    Staying away from General.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    4
    Cheers, I got constructors sorted.

    Next, I thought functions from parent classes got inhirited?

    If so, can somebody correct the following code snipit please

    Code:
    #include <iostream.h>
    
    class mploygon
    {
       public:
       mploygon ()
       {
       }
       mploygon(int a, int b)
       {
          cout << "Total = " << a + b << endl;
       }
    
       bool Area(int a, int b)
       {
          cout << "Area = " << a * b << endl;
          return (1);
       }
    };
    
    class mTriangle : mploygon
    {
       public:
       mTriangle(int a, int b) : mploygon (a,b)
       {
          cout << "This is the triangle constructor" << endl;
       }
    };
    
    int main()
    {
       mTriangle * b = new mTriangle(5,5);
       b->Area(5,5);
    
       return(0);
    }
    The dodgy line is ;
    Code:
    b->Area(5,5);

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    class mTriangle : mploygon

    this means

    class mTriangle : private mploygon

    This means is implemented in terms of. It does not imply in any way that a mTriangle IS A mploygon.

    btw polygon is spelt like that and not ploygon.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    • "mploygon" => "mPolygon"? mVarName is usually used for hungarian notation for a member variable, not class name
    • "class mTriangle : mploygon" == "class mTriangle : private mploygon" - which means you can't access mploygon members through an instance of mTriangle.
    • "mTriangle * b = new mTriangle(5,5);" - You better delete that!!

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    4
    Cheers dudes.

    As you can tell i'm a serious noobie, but this is a very friendly forum!!!

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Dont use any form f hungarian notation because it is counterintuitive. It actually makes for less readable code. Common practice these days to distinguish member vars from nonmember vars is that member vars have either prepended or appended an underscore('_'). More and more these days we see this appended and not prepended because the compiler reserves many underscore-prepended names for its own use.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21