#include <iostream>
using namespace std;
class original {
public:
original(int i = 0, int j = 0) {
x0 = i;
y0 = j;
}
virtual void set()=0;
virtual void draw()=0;
private:
int x0, y0;
};
class son1: public original {
public:
son1(int i = 0, int j = 0, int m = 0, int n=0):original(i,j){
x1 = m; y1 = n;
}
void set() {
cout << "son1::set( " << x1++ << " )called\n";
}
void draw() {
cout << "son1::draw( " << --y1 << " )called\n";
}
protected:
int x1, y1;
};
class son2:public original {
public:
son2(int i=0, int j=0, int p=0, int q=0):original(i,j) {
x2 = p;
y2 = q;
}
void set() {
cout << "son2::set( " << ++x2 << " )called.\n";
}
void draw() {
cout << "son2::draw( " << y2-- << " )called.\n";
}
protected:
int x2, y2;
};
void drawobj(original *p) {
p->draw();
}
void setobj(original *p) {
p->set();
}
int main() {
son1 *s1obj = new son1;
son2 *s2obj = new son2;
drawobj(s1obj);
drawobj(s2obj);
cout << "another one!" << endl;
setobj(s1obj);
setobj(s2obj);
cout << "\nRedraw the objects\n";
drawobj(s1obj);
drawobj(s2obj);
return 0;
}
纯虚函数的入门程序
最新推荐文章于 2022-02-10 21:04:30 发布