#include<iostream>
using namespace std;
class A1
{
public:
A1(){ a = 0;};
virtual void print(){cout<<"A1"<<endl;};
protected:
int a;
private:
};
class A2 :public A1
{
public:
A2():A1(){ a = 1;};
virtual void print(){cout<<"A2"<<endl;};
};
class A3 :public A1
{
public:
A3():A1(){ a = 2;};
virtual void print(){cout<<"A3"<<endl;};
};
class B1
{
public:
B1(){ b1 = 1;};
virtual void print(){cout<<"B1"<<endl;};
void read(){cout<<"Read B1"<<endl;};
protected:
int b1;
};
class B2
{
public:
B2(){ b2 = 2;};
virtual void print(){cout<<"B2"<<endl;};
void read(){cout<<"Read B2"<<endl;};
protected:
int b2;
};
class B3 : public B1,B2
{
public:
B3(){b1 = b2 = 3;};
virtual void print(){cout<<"B3"<<endl;};
};
int main()
{
class A1* a1 = new A2();
class A2* a2;
class A3* a3;
class A1* a;
a2 = dynamic_cast<A2 *>(a1);
if(a2 != NULL)a2->print();
a3 = dynamic_cast<A3 *>(a1);
if(a3 != NULL)a3->print();
class B1 * b1;
class B2* b2;
class B3* b3;
class B3* b;
b1 = new B3();
b1->print();
b1->read();
b3 = dynamic_cast<B3*>(b1);
b3->print();
//b3->read();
//b2 = b3;
//b2->print();
//b2->read();
b = new B3();
//b2->print();
//b2->read();
b1 = b;
b1->print();
b1->read();
return 0;
}
继承多态
最新推荐文章于 2024-03-08 10:00:00 发布