C++中的联编分两种:
(1)静态联编;
(2)动态联编;
·
· #include<iostream>
·
· using namespacestd;
· class father
· {
· public:
· father(){}
· ~father(){}
· public:
· void run() const{cout<<"父亲可以跑万米"<<endl;}
· };
· class son : publicfather
· {
· public:
· son(){}
· ~son(){}
· public:
· void run() const{cout<<"儿子亲可以跑十万米"<<endl;}
· };
· int main()
· {
· father f;
· f.run();
· son s;
· s.run();
· system("pause");
· return 0;
· }
· =>父亲可以跑万米
儿子亲可以跑十万米
·
· #include<iostream>
·
· using namespace std;
· class father
· {
· public:
· father(){}
· ~father(){}
· public:
· void run() const{cout<<"父亲可以跑万米"<<endl;}
· };
· class son : publicfather
· {
· public:
· son(){}
· ~son(){}
· public:
· void run() const{cout<<"儿子亲可以跑十万米"<<endl;}
· };
· int main()
· {
· int choice = 0;
· while(1)
· {
· cout<<"(1)father(2)son(3)quit:";
· father *p;
· bool quit = false;
· cin>>choice;
· switch(choice)
· {
· case 1:
· {
· p = new father;
· break;
· }
· case 2:
· {
· p = new son;
· break;
· }
· case 3:
· {
· quit = true;
· break;
· }
· default:
· {
· cout<<"请输入1~3之间的整数"<<endl;
· }
· }
· if(true == quit)
· {
· break;
· }
· p->run();
· }
· system("pause");
· return 0;
· }
· =>(1)father(2)son(3)quit:1
· 父亲可以跑万米
· (1)father(2)son(3)quit:2
· 父亲可以跑万米
(1)father(2)son(3)quit:3
· #include<iostream>
·
· using namespacestd;
· class father
· {
· public:
· father(){}
· ~father(){}
· public:
· virtual void run() const{cout<<"父亲可以跑万米"<<endl;}
· };
· class son : publicfather
· {
· public:
· son(){}
· virtual ~son(){}
· public:
· void run() const{cout<<"儿子亲可以跑十万米"<<endl;}
· };
· int main()
· {
· father f;
· son s;
· f.run();
· f = s;
· f.run();
· system("pause");
· return 0;
· }
· =>父亲可以跑万米
父亲可以跑万米
·
· #include<iostream>
·
· using namespacestd;
· class father
· {
· public:
· father(){}
· ~father(){}
· public:
· virtual void run() const{cout<<"父亲可以跑万米"<<endl;}
· };
· class son : publicfather
· {
· public:
· son(){}
· virtual ~son(){}
· public:
· void run() const{cout<<"儿子亲可以跑十万米"<<endl;}
· };
· int main()
· {
· father *f=new father;
· f->run();
· son s;
· father *p = &s;
· p->run();
· system("pause");
· return 0;
· }
· =>父亲可以跑万米
儿子亲可以跑十万米
参考文献: [1]《C++全方位学习》范磊——第十三章 [2]《C++程序设计教程(第二版)》钱能——第五章、第六章、第七章 [3]《C++ Primer(第5版)》王刚 杨巨峰——第一章、第六章 [4] 百度搜索关键字:C++函数联编、虚函数、非虚函数、静态联编、编译时静态联编、运行时静态联编、动态联编、编译时动态联编、运行时动态联编