How do I print the displaydata() function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tam13
    New Member
    • Jul 2010
    • 9

    How do I print the displaydata() function?

    Hi Guys,

    We're working with c++ to create an animal hierarchy and I'm having trouble trying to assign values and create the displaydata() function. Can anyone point me in the right direction please.

    Thanks!

    Code:
    class animal {
    protected:char sound[10];
    	int fly;
    public:virtual void displaydata();
    };
    
    class cow : public animal {
           private:int numberoflegs;
    	public : void displaydata();
    };
    
    class bird : public animal {
    public:void displaydata();
    };
    
    int main (int argc, char * const argv[]) {
    	bird *tweet = new bird;
    	tweet->displaydata();
        return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    bird *tweet = new bird;
    tweet->displaydata( );


    The animal class is your base class. When you use polymorphism (virtual functions) you create derived objects and access them using base pointers. That means you code should be:

    Code:
    animal *ptr = new bird; 
        ptr->displaydata();
    So you create a bird but use4 as an animal. When you call displadata() you are supposed to call animal::display data() becuse ptr is an animal pointer. However, the virtual keyword directs the call to the derived class (bird) so it is bird::displayda ta() that is called. Hence, your animal tweets because it is really a bird.

    This means that all access to derived emember functions has to come through the animal class. Therefore, if there is added data for a bird (like the sound of the tweet)you have to use the base class. Unfortunately, not all anumals tweet. So what you need now is a bird pointer to change the data in the bird object. Unfortunately again, you can't cast the animal pointer to a bird pointer because you can't guarantee the animal pointer points to a biord object. It could point to a cow object and cow objects don't tweet.

    Here is where the Visitor design pattern ius used. Please red the article on the Visitor desing pattern in the C/C++ Insights and post again afterwards. Then I will add the code need to get the bird pointer using the animal pointer but you need to read the article first to be able to follow the code.

    Comment

    • tam13
      New Member
      • Jul 2010
      • 9

      #3
      Ok I read the article on visitor design pattern.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        I'd recommend implementing an animal class as above, but I'd change the method signature to allow use of a stream by reference, so that you don't have to bind one into the object as an attribute.

        Code:
        class Animal
        {
          public:
            void displayData(ostream &out) = 0;
            .
            .
            .
        }
        Then, you'd use the object as

        Code:
        MyObject->displayData(cout);

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          OK assuming you read the Visitor article, here we go.

          The idea is to use an animal pointer that points to one of your animals ( a cow or a bird) and use it to call a method on the derived class. That is, it the pointer points to a cow then the idea is to call a cow method using an animal pointer. That way you can use an animal pointer to change the numberoflegs on a cow.

          This is the main():

          Code:
          int main()
          {
             animal* ptr = new cow;
             Visitor v;
             ptr->Visit(&v);
             ptr->displaydata();
             
          }
          You create a cow and use it as an animal pointer.

          Next, you visit the object using a Visitor pointer. That will set the number of legs.

          Next, you display the data for the cow.

          This is the code to make the main() work:

          Code:
          class cow;
          class bird;
          class Visitor
          {
          public:
          	void ItsACow(cow* obj);
          	void ItsABird(bird* obj);
          
          };
          
          
          class animal { 
          protected:char sound[10]; 
              int fly; 
          public:virtual void displaydata() = 0; 
          	   void Visit(Visitor* obj);
          private: virtual void DoVisit(Visitor* obj);
          }; 
          void animal::Visit(Visitor* obj)
          {
              DoVisit(obj);
          }
          void animal::DoVisit(Visitor* obj)
          {
              return; 
          }
          
          
          
            
          class cow : public animal { 
                 private:int numberoflegs; 
              public : void displaydata();
          public: void DoVisit(Visitor* obj);
          public: void SetLegs(int val);
          
          }; 
            
          class bird : public animal { 
          public:void displaydata(); 
          }; 
          
          
          
          void cow::SetLegs(int val)\
          {
              this->numberoflegs = val;
          }
          void cow::displaydata()
          {
             cout << "A cow has " << numberoflegs << " legs."<< endl;
          }
          void bird::displaydata()
          {
              cout << "Tweet. Tweet." << endl;
          }
          
          void cow::DoVisit(Visitor* obj)
          {
              obj->ItsACow(this);
          }
          
          void Visitor::ItsACow(cow* obj)
          {
            //Set the cow's number of legs here
          	obj->SetLegs(4);
          }
          
          int main()
          {
             animal* ptr = new cow;
             Visitor v;
             ptr->Visit(&v);
             ptr->displaydata();
             
          }
          I suggest you follow this code using the insight article and then step through it with your debugger to see how the design pattern works.

          Then post again if you still have questions.

          I only implemented the cow.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            Why would you want to do that? viz;change the number of legs on a cow?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              May it got injured and has only 3 legs now. Maybe this is a veternarian's application.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                Yes...I see.

                Comment

                Working...