#include<iostream>
#include<string>
using namespace std;
class A
{
public:
virtual void f1() = 0;
};
class B
{
public:
virtual void f2() = 0;
};
class C
{
public:
virtual void f3() = 0;
};
class D
{
public:
D(A *a, B *b, C *c)
{
this->a = a;
this->b = b;
this->c = c;
}
void f()//接口
{
a->f1();
b->f2();
c->f3();
}
~D()
{
if (a != NULL)
{
delete a;
a = NULL;
}
if (b != NULL)
{
delete b;
b = NULL;
}
if (c != NULL)
{
delete c;
c = NULL;
}
}
private:
A *a;
B *b;
C *c;
};
class E1 :public A
{
public:
virtual void f1()
{
cout << "E1_a" << endl;
}
};
class E2 :public B
{
public:
virtual void f2()
{
cout << "E2_b" << endl;
}
};
class E3 :public C
{
public:
virtual void f3()
{
cout << "E3_c" << endl;
}
};
class F1 :public A
{
public:
virtual void f1()
{
cout << "F1_a" << endl;
}
};
class F2 : public B
{
public:
virtual void f2()
{
cout << "F2_b" << endl;
}
};
class F3 : public C
{
public:
virtual void f3()
{
cout << "F3_c" << endl;
}
};
void t1()
{
A *E_a = new E1;
B *E_b = new E2;
C *E_c = new E3;
D *d1 = new D(E_a, E_b, E_c);
d1->f();
delete d1;
cout << "-------------------"<<endl;
A *F_a = new F1;
B *F_b = new F2;
C *F_c = new F3;
D *d2 = new D(F_a, F_b, F_c);
d2->f();
delete d2;
}
int main()
{
t1();
system("pause");
return 0;
}
具体的类才写具体的函数实现