473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How do I print the displaydata() function?

9
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!

Expand|Select|Wrap|Line Numbers
  1. class animal {
  2. protected:char sound[10];
  3.     int fly;
  4. public:virtual void displaydata();
  5. };
  6.  
  7. class cow : public animal {
  8.        private:int numberoflegs;
  9.     public : void displaydata();
  10. };
  11.  
  12. class bird : public animal {
  13. public:void displaydata();
  14. };
  15.  
  16. int main (int argc, char * const argv[]) {
  17.     bird *tweet = new bird;
  18.     tweet->displaydata();
  19.     return 0;
  20. }
Aug 9 '10 #1
7 2639
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. animal *ptr = new bird; 
  2.     ptr->displaydata(); 
So you create a bird but use4 as an animal. When you call displadata() you are supposed to call animal::displaydata() becuse ptr is an animal pointer. However, the virtual keyword directs the call to the derived class (bird) so it is bird::displaydata() 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.
Aug 9 '10 #2
tam13
9
Ok I read the article on visitor design pattern.
Aug 9 '10 #3
Oralloy
988 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. class Animal
  2. {
  3.   public:
  4.     void displayData(ostream &out) = 0;
  5.     .
  6.     .
  7.     .
  8. }
Then, you'd use the object as

Expand|Select|Wrap|Line Numbers
  1. MyObject->displayData(cout);
Aug 9 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
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():

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    animal* ptr = new cow;
  4.    Visitor v;
  5.    ptr->Visit(&v);
  6.    ptr->displaydata();
  7.  
  8. }
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:

Expand|Select|Wrap|Line Numbers
  1. class cow;
  2. class bird;
  3. class Visitor
  4. {
  5. public:
  6.     void ItsACow(cow* obj);
  7.     void ItsABird(bird* obj);
  8.  
  9. };
  10.  
  11.  
  12. class animal { 
  13. protected:char sound[10]; 
  14.     int fly; 
  15. public:virtual void displaydata() = 0; 
  16.        void Visit(Visitor* obj);
  17. private: virtual void DoVisit(Visitor* obj);
  18. }; 
  19. void animal::Visit(Visitor* obj)
  20. {
  21.     DoVisit(obj);
  22. }
  23. void animal::DoVisit(Visitor* obj)
  24. {
  25.     return; 
  26. }
  27.  
  28.  
  29.  
  30.  
  31. class cow : public animal { 
  32.        private:int numberoflegs; 
  33.     public : void displaydata();
  34. public: void DoVisit(Visitor* obj);
  35. public: void SetLegs(int val);
  36.  
  37. }; 
  38.  
  39. class bird : public animal { 
  40. public:void displaydata(); 
  41. }; 
  42.  
  43.  
  44.  
  45. void cow::SetLegs(int val)\
  46. {
  47.     this->numberoflegs = val;
  48. }
  49. void cow::displaydata()
  50. {
  51.    cout << "A cow has " << numberoflegs << " legs."<< endl;
  52. }
  53. void bird::displaydata()
  54. {
  55.     cout << "Tweet. Tweet." << endl;
  56. }
  57.  
  58. void cow::DoVisit(Visitor* obj)
  59. {
  60.     obj->ItsACow(this);
  61. }
  62.  
  63. void Visitor::ItsACow(cow* obj)
  64. {
  65.   //Set the cow's number of legs here
  66.     obj->SetLegs(4);
  67. }
  68.  
  69. int main()
  70. {
  71.    animal* ptr = new cow;
  72.    Visitor v;
  73.    ptr->Visit(&v);
  74.    ptr->displaydata();
  75.  
  76. }
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.
Aug 10 '10 #5
whodgson
542 512MB
Why would you want to do that? viz;change the number of legs on a cow?
Aug 11 '10 #6
weaknessforcats
9,208 Expert Mod 8TB
May it got injured and has only 3 legs now. Maybe this is a veternarian's application.
Aug 11 '10 #7
whodgson
542 512MB
Yes...I see.
Aug 12 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Steven Bethard | last post by:
Philippe C. Martin wrote: > class Debug_Stderr: > __m_text = '' > __m_log_text = None > __m_dbg = None > __m_refresh_count = 0 <rant> I don't see the benefit in 99.9% of cases for...
1
by: weiwei | last post by:
HI I am having a problem with print friendly function with asp, I have download some code regard with print friendly, it works fine with static html information, however, I have the dynamic pages...
3
by: Steven Woody | last post by:
Hi, i am using gcc. in the following code void ( * on_data_receive_of_ph ) ( const short line, const uint8_t c ); .... printf( "addr of function = %x\n", (void*)on_data_receive_of_ph ) ---...
22
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the...
8
by: Sullivan WxPyQtKinter | last post by:
When debugging using 'print' statement, I usually want to print some important values together with the function name as the context of the values printed out. So my hope is that I could get the...
4
by: Michael Yanowitz | last post by:
I am still new to Python but have used it for the last 2+ months. One thing I'm still not used to is that functions parameters can't change as expected. For example in C, I can have status =...
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
1
by: somenath | last post by:
Hi ALL , I need to print the name of the function as my debug message. I have tried the following way #include<stdio.h> #include<stdlib.h> #define __STR(x) _VAL(x) #define _VAL(x) #x int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.