class Animal{public:// turn the following virtual modifier on/off to see what happens//virtualstd::string Says() { return "?"; }};
class Dog: public Animal{public: std::string Says() { return "Woof"; }};
void test(){Dog* d = new Dog();Animal* a = d; // refer to Dog instance with Animal pointer
std::cout << d->Says(); // always Woofstd::cout << a->Says(); // Woof or ?, depends on virtual}
#include<iostream>
using namespace std;
class A{public:void show(){cout << " Hello from Class A";}};
class B :public A{public:void show(){cout << " Hello from Class B";}};
int main(){
A *a1 = new B; // Create a base class pointer and assign address of derived object.a1->show();
}
输出将是:
Hello from Class A.
但具有虚拟功能:
#include<iostream>
using namespace std;
class A{public:virtual void show(){cout << " Hello from Class A";}};
class B :public A{public:virtual void show(){cout << " Hello from Class B";}};
int main(){
A *a1 = new B;a1->show();
}
#include <iostream>using namespace std;
class father{public: void get_age() {cout << "Fathers age is 50 years" << endl;}};
class son: public father{public : void get_age() { cout << "son`s age is 26 years" << endl;}};
int main(){father *p_father = new father;son *p_son = new son;
p_father->get_age();p_father = p_son;p_father->get_age();p_son->get_age();return 0;}
输出:
Fathers age is 50 yearsFathers age is 50 yearsson`s age is 26 years
具有虚拟功能的程序:
#include <iostream>using namespace std;
class father{public:virtual void get_age() {cout << "Fathers age is 50 years" << endl;}};
class son: public father{public : void get_age() { cout << "son`s age is 26 years" << endl;}};
int main(){father *p_father = new father;son *p_son = new son;
p_father->get_age();p_father = p_son;p_father->get_age();p_son->get_age();return 0;}
输出:
Fathers age is 50 yearsson`s age is 26 yearsson`s age is 26 years
class Animal:def says(self):return "??"
class Dog(Animal):def says(self):return "woof"
def func(a):return a.says()
if __name__ == "__main__":
a = Animal()d = Dog()ad = d # dynamic typing by assignment
print("Animal a says\t\t{}".format(a.says()))print("Dog d says\t\t{}".format(d.says()))print("Animal dog ad says\t{}".format(ad.says()))
print("func(a) :\t\t{}".format(func(a)))print("func(d) :\t\t{}".format(func(d)))print("func(ad):\t\t{}".format(func(ad)))
输出是:
Animal a says ??Dog d says woofAnimal dog ad says wooffunc(a) : ??func(d) : wooffunc(ad): woof
这与C++的虚拟定义相同。请注意,d和ad是两个不同的指针变量,引用/指向同一个Dog实例。表达式(ad is d)返回True,它们的值相同<主要. Dog对象at 0xb79f72cc>。
#include <iostream>
using namespace std;
class Animal {public:virtual void MakeTypicalNoise() = 0; // no implementation needed, for abstract classesvirtual ~Animal(){};};
class Cat : public Animal {public:virtual void MakeTypicalNoise(){cout << "Meow!" << endl;}};
class Dog : public Animal {public:virtual void MakeTypicalNoise() { // needs to be virtual, if subtype polymorphism is also needed for Dogscout << "Woof!" << endl;}};
class Doberman : public Dog {public:virtual void MakeTypicalNoise() {cout << "Woo, woo, woow!";cout << " ... ";Dog::MakeTypicalNoise();}};
int main() {
Animal* apObject[] = { new Cat(), new Dog(), new Doberman() };
const int cnAnimals = sizeof(apObject)/sizeof(Animal*);for ( int i = 0; i < cnAnimals; i++ ) {apObject[i]->MakeTypicalNoise();}for ( int i = 0; i < cnAnimals; i++ ) {delete apObject[i];}return 0;}
class base {public:void helloWorld() { std::cout << "Hello World!"; }};
class derived: public base {public:void helloWorld() { std::cout << "Greetings World!"; }};
int main () {base hwOne;derived hwTwo = new derived();base->helloWorld(); //prints "Hello World!"derived->helloWorld(); //prints "Hello World!"
好的,这就是我们所知道的。现在让我们尝试使用成员函数指针:
#include <iostream>using namespace std;
class base {public:void helloWorld() { std::cout << "Hello World!"; }};
class derived : public base {public:void displayHWDerived(void(derived::*hwbase)()) { (this->*hwbase)(); }void(derived::*hwBase)();void helloWorld() { std::cout << "Greetings World!"; }};
int main(){base* b = new base(); //Create base objectb->helloWorld(); // Hello World!void(derived::*hwBase)() = &derived::helloWorld; //create derived memberfunction pointer to base functionderived* d = new derived(); //Create derived object.d->displayHWDerived(hwBase); //Greetings World!
char ch;cin >> ch;}