another question about virtual functions

This is a discussion on another question about virtual functions within the C++ Programming forums, part of the General Programming Boards category; Hi I found that don't affect the derived class. if a virtual method is defined with "private" or "procted", the ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    another question about virtual functions

    Hi

    I found that don't affect the derived class.

    if a virtual method is defined with "private" or "procted", the derived class still can implement it, right? So doesn't make it sense to put "private" or "procted" in front of virtual methods in deriving class?

    Thanks
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    Banned
    Join Date
    Oct 2004
    Posts
    250
    A class wich is deriving from another one will not be able to see private data, if you dont declare what the data is it be automatically private.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    Sorry, seems I didn't make myself clear:

    for example:

    Code:
    #include <iostream.h>
    class A
    {
        private:
                virtual void test(){return;}
                virtual void test2()=0;
    };
    
    class B : public A
    {
         public:
               void test(){cout<<"replaced by B"<<endl;}
               void test2(){cout<<"implement by B"<<endl;}
    };
    void main()
    {
          //implenment!!
          B* b = new B;
          b->test();
          b->test2();
    }
    I compiled it and no error. My question is that even 2 "test" functions are declared as private or protected, but they are still can be modified. so "private" doesn't restrcit the access.

    Thanks again

    sorry for the mistake, now it really compiles with no error
    Last edited by SuperNewbie; 01-27-2005 at 08:30 AM.
    Don't laugh at me,I am just a SuperNewbie.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    But there is no possibility to call either A::test() or A::test2() from outside A, so the private access still holds.
    Last edited by Sang-drax; 01-27-2005 at 06:06 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    But there is no possibility to call either A::test() or A::test2() from outside A, so the private access still holds
    If I understand right, this is for common case because test2() is pure virtual function and test() is not static. So nothing about "private", right?
    Don't laugh at me,I am just a SuperNewbie.

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by SuperNewbie
    So nothing about "private", right?
    I'm not sure I understand, but if you hadn't declared the methods private, B would've been able to do:
    Code:
    A::test();
    Last edited by Sang-drax; 01-27-2005 at 08:03 AM. Reason: OK, let's see if it works now :)
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Except that there is no 'test1', and if it were 'test', 'test' is a pure virtual funciton, and as such, has no actual function you can invoke. Right?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Hmm, I was almost certain I edited 'test1' to 'test' just after posting.
    I meant test() and it is not a pure virtual function. Only test2() is pure virtual.

    (ignoring errors like a void function returning 0 etc.)
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right. I could have sworn test was the pure virtual function, and test2 wasn't. Anyway, we can both agree on if there is a pure virtual function in there some place, you can't call it. But you could call non-private non-virtual functions.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I can't grasp what concept you guys are trying to understand. What I get from this is is that the private virtual functions are completely useless:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class A
    {
       private:
          virtual void test1( )
          {
             cout << "Yo yo" << endl;
          }
       
          virtual void test2( )
          {
             cout << "Blah blah blah" << endl;   
          }
    };
    
    class B : public A
    {
       public:
          void test1( )
          {
             cout << "replaced by B" << endl;
          }
       
          void test2( )
          {
             cout << "implement by B" << endl;
          }
    };
    
    int main( )
    {
       // Test out classes
       
       B* b = new B;
       b->test1();
       b->test2();
    
       // Return success
    
       return 0;
    }

  11. #11
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Private will not allow any children to touch the function, but virtual will allow them to ovveride it. So classB can have it's own test(), but cannot call the test that is in classA (If they were public, then classB would be able to call classA's).

    The only situation I can think of that will allow you to understand what's going on would be to create a class that is a friend of both classA and classB, and then test out what happens when it calls the private functions using different pointers.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    Sorry for confusing you guys, but here I make the conclusion according to my point of view:

    Private will not allow any children to touch the function, but virtual will allow them to ovveride it.
    Right, private and virtual they are totally different and nothing to do each other:

    private emphasizes on its own members, means that no other derived classes or instance (or even deriving class instance) can access them, but the deriving class itself.

    virtual emphasizes on the override and implementation, so even the pure virtual function is tagged as "private" in deriving class must be implemented by its sub class if the sub class has its instance. (notice the difference between virtual and pure virtual)

    So the "private virtual" I think it doesn't make any sense here

    So the new problem is for example:

    what would the situation if:

    class B: public A
    and
    class B: protected A
    and
    class B: private A

    I think those happen unfrequently, but it is still good if we know it.
    Last edited by SuperNewbie; 01-29-2005 at 03:04 AM.
    Don't laugh at me,I am just a SuperNewbie.

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Try the following code:
    Code:
    class A {
      private:
        virtual void test() { cout<<"test from A"; }
      public:
        void doSomething() { test(); }
    };
    
    class B: public A {
      virtual void test() {cout << "test from B";}
    };
    
    int main()  {
      A* obj = new B;
      obj.doSomething();
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    I think virtual function can be override no matther if it is "private" or not because private means only the other members in the same class can invoke it. so
    Code:
    class B: public A
    where
    Code:
    virtual void test()
    in A has been overrided.

    Code:
    int main()  {
      A* obj = new B; // object ONLY has the function doSomething()
      obj->doSomething(); // test() in A has been overrided already, 
                                      //so use test() in B class!!
      return 1;
    }
    Right??
    Don't laugh at me,I am just a SuperNewbie.

  15. #15
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by SuperNewbie
    I think virtual function can be override no matther if it is "private" or not because private means only the other members in the same class can invoke it.
    Yes, that's correct.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual functions but non-virtual destructor
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2007, 12:08 PM
  2. Pointers to member functions using virtual methods
    By Will B-R in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2006, 07:59 AM
  3. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 11:48 AM
  4. Inheritance and virtual functions
    By Cryptomega in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2003, 02:59 PM
  5. Virtual Functions
    By Lazy Student in forum C++ Programming
    Replies: 7
    Last Post: 10-04-2002, 01:55 PM

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