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

Thread: Polymorphism

Threaded View

  1. #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.

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