标签:obj 多个 ber 路径 语句 指定 派生 虚函数 比较
一、基本概念
#include <iostream>
#include <time.h>
using namespace std;
class B1
{
public:
B1(int i)
{
cout<<"constructing B1 "<<i<<endl;
}
};
class B2
{
public:
B2(int j)
{
cout<<"constructing B2 "<<j<<endl;
}
};
class B3
{
public:
B3()
{
cout<<"constructing B3"<<endl;
}
};
class C: public B2, public B1, public B3
{
public:
C(int a, int b, int c, int d):B1(a), memberB2(d), memberB1(c),B2(b)
{
}
private:
B1 memberB1;
B2 memberB2;
B3 memberB3;
};
int main()
{
C obj(1,2,3,4);
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B1
{
public:
B1(int i)
{
cout<<"constructing B1 "<<i<<endl;
}
~B1()
{
cout<<"destructing B1"<<endl;
}
};
class B2
{
public:
B2(int j)
{
cout<<"constructing B2 "<<j<<endl;
}
~B2()
{
cout<<"destructing B2"<<endl;
}
};
class B3
{
public:
B3()
{
cout<<"constructing B3"<<endl;
}
~B3()
{
cout<<"destructing B3"<<endl;
}
};
class C: public B2, public B1, public B3
{
public:
C(int a, int b, int c, int d):B1(a), memberB2(d), memberB1(c),B2(b)
{
}
private:
B1 memberB1;
B2 memberB2;
B3 memberB3;
};
int main()
{
C obj(1,2,3,4);
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B1
{
public:
int nV;
void fun()
{
cout<<"member of B1 "<<nV<<endl;
}
};
class B2
{
public:
int nV;
void fun()
{
cout<<"member of B2 "<<nV<<endl;
}
};
class D1: public B1, public B2
{
public:
int nV;
void fun()
{
cout<<"member of D1 "<<nV<<endl;
}
};
int main()
{
D1 d1;
d1.nV = 1;
d1.fun();
d1.B1::nV = 2;
d1.B1::fun();
d1.B2::nV = 3;
d1.B2::fun();
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B0
{
public:
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
};
class B1:public B0
{
public:
int nV1;
};
class B2:public B0
{
public:
int nV2;
};
class D1:public B1, public B2
{
public:
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
};
int main()
{
D1 d1;
d1.B1::nV = 2;
d1.B1::fun();
d1.B2::nV = 3;
d1.B2::fun();
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B0
{
public:
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
};
class B1:virtual public B0
{
public:
int nV1;
};
class B2:virtual public B0
{
public:
int nV2;
};
class D1:public B1, public B2
{
public:
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
};
int main()
{
D1 d1;
d1.nV = 2;
d1.fun();
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B0
{
public:
B0(int n)
{
nV = n;
}
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
};
class B1:virtual public B0
{
public:
B1(int a):B0(a)
{
}
int nV1;
};
class B2:virtual public B0
{
public:
B2(int a):B0(a)
{
}
int nV2;
};
class D1:public B1, public B2
{
public:
D1(int a):B0(a), B1(a), B2(a)
{
}
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
};
int main()
{
D1 d1(1);
d1.nV = 2;
d1.fun();
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B0
{
public:
void display()
{
cout<<"B0::display()"<<endl;
}
};
class B1:public B0
{
public:
void display()
{
cout<<"B1::display()"<<endl;
}
};
class B2:public B0
{
public:
void display()
{
cout<<"B2::display()"<<endl;
}
};
void fun(B0 *ptr)
{
ptr->display();
}
int main()
{
B0 b0;
B1 b1;
B2 b2;
fun(&b0);
b0 = b1;
fun(&b0);
b0 = b2;
fun(&b0);
return 0;
}
输出结果为:
B0::display()
B0::display()
B0::display()
通过这种赋值兼容后,每次调用的同名函数都是基类的同名函数,如果想调用派生类的,则需要使用虚函数。
标签:obj 多个 ber 路径 语句 指定 派生 虚函数 比较
原文地址:http://www.cnblogs.com/didiaoxiaoguai/p/7697714.html