Saturday, December 6, 2008

C++ Code Example: pure virtual function

Sphere: Related Content #include <iostream>
using namespace std;

class Base{
 public:
  int basicF();
  int virtual virtualF(){
   cout << "hi this base virtual function" << endl;
    return 0;
  }
 void virtual pureVirtualF() = 0;

};

class Derived : public Base{
 public:
 int virtual virtualF(){
  cout << "hi this derived virtual function" << endl;
   return 0;
 }
 void virtual pureVirtualF(){
  cout << "This pure virtual function in Derived" << endl; }
};
#if 1

class Derived1 : public Derived {
 public:
 int virtual virtualF(){
  cout << "hi this derived1 virtual function" << endl;
  return 0;
 }

};
#endif


int main(){
 Base *p;
 Derived d, *p1;
 Derived1 d1;
#if 0
 a.virtualF();
 p = &a;
 p->virtualF();
#endif

 p = &d;
 p->virtualF();
 p->pureVirtualF();
 p = &d1;
 p->virtualF();
 p1 = &d1;
 p1->virtualF();
 //p->pureVirtualF();
 return 0;
}

No comments: