Saturday, February 12, 2005

Testing virtual functions, destructor and static_cast properties


#include <iostream>

class A {
public:
virtual void function () {
cout << "A" << endl;
}
~A () {
cout << "DA" << endl;
}
};

class B : public A {
public:
virtual void function () {
cout << "B" << endl;
}
~B () {
cout << "DB" << endl;
}
void f1 () {
cout << "F1" << endl;
}
};

class C : public B {
public:
virtual void function () {
cout << "C" << endl;
}
~C () {
cout << "DC" << endl;
}
};

int main(int argc, char* argv[]) {
A* a;
B* b;
C* c=new C();
a=c;
a->function();
b=static_cast<B*>(a);
b->f1();
delete b;
return 0;
}

0 Comments:

Post a Comment

<< Home