有virtual没virtual的区别
#include <iostream>
using namespace std;
class A
{
public:
int a;
A(int x){a=x;}
};
class B:public A //由公共基类A派生出类B
{
public:
int b;
B(int x,int y):A(x){b=y;}
};
class C:public A //由公共基类A派生出类C
{
public:
int c;
C(int x,int y):A(x) {c=y;}
};
class D:public B,public C //由基类B、C派生出类D
{
public:
int d;
D(int x,int y,int z,int m,int n):B(x,y),C(z,m){d=n;}
void Print()
{
cout<<" B:: a="<<B::a<<'\t'<<"b="<<b<<endl;
cout<<" C:: a="<<C::a<<'\t'<<"c="<<c<<endl;
cout<<"d="<<d<<endl;
}
};
int main ()
{
D d1(100,200,300,400,500);
d1.Print();
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
int a;
A(int x=0){a=x;}
};
class B: virtual public A //由公共基类A派生出类B
{
public:
int b;
B(int x,int y):A(x){b=y;}
};
class C: virtual public A //由公共基类A派生出类C
{
public:
int c;
C(int x,int y):A(x) {c=y;}
};
class D:public B,public C //由基类B、C派生出类D
{
public:
int d;
D(int x,int y,int z,int m,int n):B(x,y),C(z,m){d=n;}
void Print()
{
cout<<" B:: a="<<B::a<<'\t'<<"b="<<b<<endl;
cout<<" C:: a="<<C::a<<'\t'<<"c="<<c<<endl;
cout<<"d="<<d<<endl;
}
};
int main ()
{
D d1(100,200,300,400,500);
d1.Print();
d1.a=400;
d1.Print();
return 0;
}
即便是桃李开满韵华,不若你嫣然一笑