CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: Polymorphism

  1. #1
    Join Date
    Oct 2008
    Posts
    25

    Polymorphism

    Hello.
    I have 3 classes. B and C inherit from A. I want that class B will be able to call functions of C, so I tried to do this:
    Code:
    class A
    {
        public:
           A(int b=0) {a=b;}
        protected:
           void Print() {cout<<"Hello world"<<endl;}
        private:
           int a;
    };
    class B: public A
    {
        public:
           B(int b=0): A(b){}
           void Do(A& a) {a.Print();} //error
    };
    class C: public A
    {
        C(int b=0): A(b){}
    
        int c;
    };
    but i get an error:
    error: `virtual void A::Print()' is protected.

    How can I do it without using "friend" (and without making Print public) ?
    Last edited by ovidiucucu; March 26th, 2010 at 01:16 AM. Reason: added [CODE] tags

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

    Re: Polymorphism

    Why does A not have any virtual functions, not even a virtual destructor? It looks like you cannot use inheritance based polymorphism unless you change A.

    Other than that... what does B have to do with C?
    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

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Polymorphism

    Quote Originally Posted by omerd View Post
    Hello.
    I have 3 classes. B and C inherit from A. I want that class B will be able to call functions of C, so I tried to do this:
    In that case B and C must be related by inheritance like for example,

    class A {};
    class C : public A {};
    class B : public C {};

    Both B and C inherits A, C directly and B indirectly via C.

    This is also possible,

    class A {};
    class C : public A {};
    class B : public A, public C {};

    It's essentially as before apart from that if A carries implementation it's now an example of the (in)famous so called diamond inheritance. You can resolve the ambiguity by the use of virtual inheritance like this,

    class A {};
    class C : virtual public A {};
    class B : virtual public A, public C {};

    If the purpose if inheriting C in B was only to get at the implementation of C then you can inherit C privately, like

    class A {};
    class C : virtual public A {};
    class B : virtual public A, private C {};

    Now B isn't a C class as it was before, Just an A class.

    As you can see there are many options and ample opportunity to make a mess out of a design in C++.
    Last edited by nuzzle; March 26th, 2010 at 04:12 AM.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Polymorphism

    error: `virtual void A::Print()' is protected.
    This error shows the code you shown is incorrect. Please post the actual snippet, otherwise we can't help you too much.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    Your thread title, code and description don't have much in common.

    There's no polymorphism in the code you provided.

    You're not trying to call in functions in C and the code you provided doesn't produce the error you report.

    What are you really trying to do?

  6. #6
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    maybe
    I changed the code a little bit. Now it is more specific, but I also tried to keep it simple.
    Actually, B and C manage a list of objects of type D, and in some cases B need to get C's D objects. B and C create objects of D in different ways.
    If it's help you, C holds a list of object of type B.

    Code:
     class D
    {
        public:
          D(string s): name(s)
            {another_data =-1;}
          D(string s,int data): name(s),another_data(data)
            {}
          D(const D& d)
          {/*code*/}
          string GetName() {return name;}
          int GetData() {return another_data;}
    
        private:
          string name;
          int another_data;
    };
    
    class A
    {
        public:
           A(int b=0) {a=b;}
           virtual ~A()
           {
               //code;
            }
        protected:
           virtual D* FindD(string)=0;
        private:
           int a;
    };
    class B: public A
    {
        friend class A;
        public:
           B(int b=0): A(b){}
    
        D* FindD(string name)
        {
            //code that find a specific D
            //and return it
        }
           void Do(A& a, string name)
           {
               a.FindD(name); //error: `virtual D A::FindD(std::string)' is protected|
           }
    
        private:
        list<D> names;
    
    };
    class C: public A
    {
        C(int b=0): A(b){}
    
        D* FindD(string name)
        {
            //code that find a specific D
            //and return it
        }
    
        list<D> names;
    };
    Last edited by omerd; March 26th, 2010 at 08:01 AM.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    Why not put the list in A?

  8. #8
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    you right, but it doesn't help. class B need to call "FindD" on objects of type C (which is the argument in function "Do")

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    Quote Originally Posted by omerd View Post
    you right, but it doesn't help. class B need to call "FindD" on objects of type C (which is the argument in function "Do")
    Okay, so class B would need an instance of class C to do that. What's the problem? Just call FindD from your B object using your instance of C.

    From an inheritance POV, if B and C both have a list of D, that list should be in a common base class.

  10. #10
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    Quote Originally Posted by GCDEF View Post
    Okay, so class B would need an instance of class C to do that. What's Just call FindD from your B object using your instance of C.
    that what's I did. B call "FindD" on C's object, and I get an error.
    Last edited by omerd; March 26th, 2010 at 09:12 AM.

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

    Re: Polymorphism

    Quote Originally Posted by omerd
    that what's I did. B call "FindD" on C's object, and I get an error.
    Show the smallest and simplest compilable program that demonstrates that error.
    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

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    Quote Originally Posted by omerd View Post
    that what's I did. B call "FindD" on C's object, and I get an error.
    Don't you think explaining the error a bit may help us help you?

  13. #13
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    Quote Originally Posted by GCDEF View Post
    Don't you think explaining the error a bit may help us help you?
    I wrote the error above:
    error: `virtual void A::Print()' is protected.

    Quote Originally Posted by laserlight View Post
    Show the smallest and simplest compilable program that demonstrates that error.
    I will edit this message in a few hours with the code.
    Last edited by omerd; March 26th, 2010 at 10:30 AM.

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Polymorphism

    So either make FindD public in C, or else declare that B is a friend of C. I'm not sure which is more appropriate in this specific situation.

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    But B and C are both derived from A. Both classes contain a list of D. Why not move both the list and the FindD function into A?

Page 1 of 2 12 LastLast

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