#include <iostream>
#include <string>
#include <assert.h>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
using namespace std;
class Base
{
public:
Base() {cout << "Base con" << endl;}
virtual ~Base()
{
cout << "Base destructor" << endl;
}
virtual void Hiberarchy() const = 0;
};
void Base::Hiberarchy() const//pure virtual also can have function body
{
cout << "Base::Hiberarchy" << endl;
}
class Derived : public Base
{
public:
Derived() {cout << "Derived con" << endl;}
virtual void Hiberarchy() const
{
Base::Hiberarchy();
cout << "Derived::Hiberarchy" << endl;
}
// virtual ~Derived()
~Derived()
{
cout << "Derived destructor" << endl;
}
virtual void foo(){cout << "Derived foo()" << endl;}
};
int main()
{
Base *pb = new Derived();
pb->Hiberarchy();
pb->Base::Hiberarchy();
delete pb;
Derived derivedObj;
derivedObj.foo();
//cout << << endl;
return 0;
}
时间: 2024-11-05 11:38:15