一、传引用
#include "stdafx.h" #include <iostream> using namespace std; class A { public: virtual void show(){cout<<"A"<<endl;} }; class B:public A { public: virtual void show(){cout<<"B"<<endl;} }; class C:public A { public: virtual void show(){cout<<"C"<<endl;} }; void show(A &a) //void show (A a) { a.show(); } int main() { A a; B b; C c; show(a); show(b); show(c); return 0; }
执行结果:
二、传值
#include "stdafx.h" #include <iostream> using namespace std; class A { public: virtual void show(){cout<<"A"<<endl;} }; class B:public A { public: virtual void show(){cout<<"B"<<endl;} }; class C:public A { public: virtual void show(){cout<<"C"<<endl;} }; void show (A a)//void show(A &a) //void show (A a) { a.show(); } int main() { A a; B b; C c; show(a); show(b); show(c); return 0; }
时间: 2024-10-05 05:05:55