Sdut 6-2 polymorphism and virtual functions
6-2 polymorphism and virtual functions Time Limit: 1000 ms Memory limit: 65536 K any questions? Click Here ^_^ This topic describes the concept of polymorphism and the definition and use of virtual functions. It is required to define a basic class Pet, it has a character pointer data member name and a virtual member function Speak () used to output the pet call .; The derived classes Dog and Cat are derived from the base class Pet. They inherited from the base class and rewritten the speak () function, which is used to output the sounds of the Dog class and Cat class respectively (for specific output content, refer to the example output ). Virtual function technology is required to write code so that the program can output the following content. Input
None
Output
There are three rows of output data. The output content must be the same as that in the example.
Sample Input
Sample output
How does a pet speak ?I am a cat,My name is Tom My sound is miao!miao!I am a dog,My name is Snoppy My sound is wang!wang!
Prompt Source
# Include
Using namespace std; class Pet {public: virtual void speak () // defines the virtual function {cout <"How does a pet speak? "<
Speak (); // call p. speak function t = & c; // t pointer pointing to c t-> speak (); // calls c. speak function t = & d; // t pointer to d t-> speak (); // call d. speak function return 0 ;}