派生类对象的初始化 实验
- #include <iostream>
-
#include <cstring>
-
-
using namespace std;
-
class A
-
{
-
public:
-
A(int xx);
-
protected:
-
int x;
-
};
-
-
A::A(int xx)
-
{
-
x=xx;
-
}
-
-
class B
-
{
-
public:
-
B(int yy);
-
protected:
-
int y;
-
};
-
-
B::B(int yy)
-
{
-
y=yy;
-
}
-
-
class C :public A,public B
-
{
-
public:
-
C(int xx,int yy,int zz);
-
void output();
-
int z;
-
};
-
-
C::C(int xx,int yy,int zz):A(xx),B(yy)
-
{
-
z=zz;
-
}
-
-
void C::output()
-
{
-
cout<<x<<" "<<y<<" "<<z<<endl;
-
}
-
-
int main()
-
{
-
C c(1,1,1); //有参构造函数,所以一定要传递初始化 参数
-
c.output();
-
return 0;
- }
- ywx@yuweixian:~/yu/c++/pais$ ./pshi
-
1 1 1
- ywx@yuweixian:~/yu/c++/pais$