Project 3 in week 3 -- use function pointers to call functions (eat, sleep, and Doudou), and 3 in week 3 --

Source: Internet
Author: User

Project 3 in week 3 -- use function pointers to call functions (eat, sleep, and Doudou), and 3 in week 3 --

/** Copyright (c) 2014, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Liu Chang * Completion Date: July 15, December 14, 2014 * version number: v1.0 ** Problem description: Call a function using a function pointer; * input Description: enter a command; * program output: output.

 

# Include <iostream> using namespace std; void eat (); void sleep (); void hitdoudou (); void run (void (* f) (); int main () {int iChoice; do {cout <"select (1-eat; 2-sleep; 3-beat; Other-return):"; cin> iChoice; if (iChoice = 1) run (eat); else if (iChoice = 2) run (sleep); else if (iChoice = 3) run (hitdoudou ); else {cout <"It's boring to go out and play. "<Endl; break;} while (true); return 0;} void run (void (* f) () {(* f )();} void eat () {cout <"My stomach has been hungry and impatient. Let me have a full seat. "<Endl;} void sleep () {cout <" never stayed up late again. Let me wake up naturally. "<Endl;} void hitdoudou () {cout <", this year is quite a year, so let me play all the beans! "<Endl ;}


Running result:

 

Learning Experience:

What does it seem to understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.