Read the program for 14th weeks and understand the virtual and pure virtual functions

Source: Internet
Author: User
/** Copyright and version declaration section of the Program * Copyright (c) 2012, student * All rightsreserved from computer College of Yantai University. * Author: Li Yang * Completion Date: July 15, May 31, 2013 * version: v1.0 * input Description: none * Problem description: none * program output: none */# include <iostream> using namespace std; class Vehicle // Transportation Tool {public: void run () const {cout <"run a vehicle. "<endl ;}; class Car: public Vehicle // car {public: void run () const {cout <" run a Car. "<endl ;}; class Airplane: public Vehicle // aircraft {public: void run () const {cout <" run a airplane. "<endl ;}}; int main () {cout <" (a) directly uses an object to access the member function: "<endl; Vehicle v; v. run (); Car car; Airplane airplane; car. run (); airplane. run (); cout <"(B) use a pointer to the base class to access the member function:" <endl; Vehicle * vp; vp = & car; vp-> run (); vp = & airplane; vp-> run (); return 0 ;}

When a base class Pointer Points to a derived class, the pointer is used to call a member function with the same name. The base class member function is executed, not the member function of the derived class.

# Include <iostream> using namespace STD; Class vehicle // transport {public: Virtual void run () const // (2) Run () is a pure virtual function {cout <"run a vehicle. "<Endl ;}; class car: public vehicle // car {public: void run () const {cout <" run a car. "<Endl ;}; class airplane: public vehicle // aircraft {public: void run () const {cout <" run a airplane. "<Endl ;}}; int main () {cout <" (a) directly uses an object to access the member function: "<Endl; vehicle V; V. run (); car; airplane; car. run (); airplane. run (); cout <"(B) use a pointer to the base class to access the member function:" <Endl; vehicle * VP; Vp = & car; VP-> Run (); Vp = & airplane; VP-> Run (); Return 0 ;}

Modify the definition of the vehicle class to a virtual function, and the rest remain unchanged. When the base class Pointer Points to a derived class, use a pointer to call the virtual member function of the same name and execute a member function of the derived class.

# Include <iostream> using namespace std; class Vehicle // Transportation Tool {public: virtual void run () const = 0; // (3) run () is a pure virtual function}; class Car: public Vehicle // car {public: void run () const {cout <"run a Car. "<endl ;}; class Airplane: public Vehicle // aircraft {public: void run () const {cout <" run a airplane. "<endl ;}; int main () {cout <" (a) directly access the member function using an object: "<endl; // Vehicle v; // v. run (); Car car; Airplane airplane; car. run (); airplane. run (); cout <"(B) use a pointer to the base class to access the member function:" <endl; Vehicle * vp; vp = & car; vp-> run (); vp = & airplane; vp-> run (); return 0 ;}

Modify the definition of the Vehicle class to a pure virtual function. Find out the main () function and change the line with compilation errors to comments.

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.