CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2010
    Posts
    1

    Smile New to OOP, please Help

    Hi All,

    I am new to OOP. I am trying to do something similar like the following code (using inheritance).

    Code:
    class Person {
      public:
        Person ()
    		{ cout << "Person: constructor\n"; }
    
    	void talk()
    		{ cout << "Person: talk\n"; }
    
    	void walk()
    		{ cout << "Person: walk\n"; }
    };
    
    class Boy : public Person {
      public:
        Boy ()
    		{ cout << "Boy: constructor\n"; }
    
    	void gotoFootball()
    		{ cout << "Boy: football\n"; }
    };
    
    class Girl : public Person {
      public:
        Girl ()
    		{ cout << "Girl: constructor\n"; }
    
    	void gotoDance()
    		{ cout << "Girl: dance\n"; }
    };

    then in the main function, have the following code

    Code:
    Person *p1;
    	p1 = new Boy();
    
    	p1->gotoFootball();
    this however doesn't compile as gotoFootball is not recognized by Person. Is it possible to achieve something like this please? Also I know this is a 'stupid' example but I tried to keep it simple till I understand how it works.

    Thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: New to OOP, please Help

    Either
    Code:
    Boy*p1;
    	p1 = new Boy();
    
    	p1->gotoFootball();
    or
    Code:
    Person *p1;
    	p1 = new Boy();
    
    	((Boy*)p1)->gotoFootball();
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2010
    Posts
    94

    Re: New to OOP, please Help

    So where is the fish ?

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: New to OOP, please Help

    Quote Originally Posted by phuzer88 View Post
    Hi All,

    I am new to OOP. I am trying to do something similar like the following code (using inheritance).

    Code:
    class Person {
      public:
        Person ()
            { cout << "Person: constructor\n"; }
    
        void talk()
            { cout << "Person: talk\n"; }
    
        void walk()
            { cout << "Person: walk\n"; }
    };
    
    class Boy : public Person {
      public:
        Boy ()
            { cout << "Boy: constructor\n"; }
    
        void gotoFootball()
            { cout << "Boy: football\n"; }
    };
    
    class Girl : public Person {
      public:
        Girl ()
            { cout << "Girl: constructor\n"; }
    
        void gotoDance()
            { cout << "Girl: dance\n"; }
    };
    then in the main function, have the following code

    Code:
    Person *p1;
        p1 = new Boy();
    
        p1->gotoFootball();
    this however doesn't compile as gotoFootball is not recognized by Person. Is it possible to achieve something like this please? Also I know this is a 'stupid' example but I tried to keep it simple till I understand how it works.

    Thanks
    You need to learn about polymorphism. If this tutorial doesn't answer your question, I can help explain.

    Quote Originally Posted by VictorN View Post
    Either
    Code:
    Boy*p1;
        p1 = new Boy();
    
        p1->gotoFootball();
    or
    Code:
    Person *p1;
        p1 = new Boy();
    
        ((Boy*)p1)->gotoFootball();
    I can't believe you would answer that.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: New to OOP, please Help

    Quote Originally Posted by phuzer88 View Post
    Hi All,

    I am new to OOP. I am trying to do something similar like the following code (using inheritance).

    Code:
    class Person {
      public:
        Person ()
    		{ cout << "Person: constructor\n"; }
    
    	void talk()
    		{ cout << "Person: talk\n"; }
    
    	void walk()
    		{ cout << "Person: walk\n"; }
    };
    Your Person object is missing a virtual destructor. Therefore this code has undefined behaviour:
    Code:
    Person *p1;
    p1 = new Boy();
    p1->gotoFootball();
    delete p1; // you forgot this but you are now introducing undefined behaviour.
    When you are creating a class that is meant to be derived from, you must have a virtual destructor defined in the base class. If you don't do that, deriving from your base class and then creating objects using a base pointer leads to undefined behaviour when that base class pointer is deleted.
    Code:
    class Person
    {
     //...
        public:
           virtual ~Person() { }  // you must add this
    };
    Regards,

    Paul McKenzie

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: New to OOP, please Help

    Quote Originally Posted by Paul McKenzie View Post
    Your Person object is missing a virtual destructor. Therefore this code has undefined behaviour:
    Good point, Paul!

    And I'm sorry for the incorrect reply!
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2006
    Posts
    151

    Re: New to OOP, please Help

    Your Person object is missing a virtual destructor. Therefore this code has undefined behaviour:

    Code:
    Person *p1;
    p1 = new Boy();
    p1->gotoFootball();
    delete p1; // you forgot this but you are now introducing undefined behaviour.
    Don't you mean "well-defined but incorrect behaviour", rather than "undefined behaviour"?

    Won't the compiler generate a non-virtual destructor for Person which will be invoked by the delete statement? Since the destructor is not in the v-table, however, derived classes won't override it and will therefore potentially leak resources?

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: New to OOP, please Help

    Quote Originally Posted by GeoRanger
    Don't you mean "well-defined but incorrect behaviour", rather than "undefined behaviour"?
    No, that delete results in undefined behaviour. That much is explicitly stated in the C++ standard.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Nov 2007
    Posts
    35

    Re: New to OOP, please Help

    The trouble with polymorphism, is you need to anticipate in the base class all the stuff you will need to do through a base class pointer, in the derived classes. iow virtual functions. You can change what happens when MyVirtual() is called through the base class pointer by deriving a class and changing what happens in the implementation of the method. What you can't do, is go back to scratch when you find out you really need MyVirtual2(). That's why you see stuff like .NET Framework 1.0 1.1 2.0 2.1 3.0 3.5 4.0 4.5 yadda yadda and also COM dll MyServer and after a bit MyServer2. Unless you polish your crystal ball you can't anticipate everything you're going to need 2 years from now in the initial design.

    That's why there's an adage to do with code a prototype, get it to work. learn how the task really functions, now that you know, throw the first design away and come up with something that will actually work well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured