/** 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.