第三十九讲 实验 派生类中从基类中继承过画的成员的控制属
性
用实验证明
通过继承,数据成员可以访问,而派生类不可以访问
通过public继承来的派生类,基类中的public和protect仍可
被派生类的数据成员访问,但派生类的对象不能访问
protected数据成员
能过protected继承来的派生类,基类中的public和protected
继承后的性为protected,只能被派生类的中成员访问,派生
类的对象是不能访问的,
通过private继承方式,继承后属性为private,只能被本类中
的成员访问,其他成员及对象均不可访问。
不知道这样理没有理清楚,要是继承来的是public,这是对外
公开的,是可访问的。
第三十讲 派生类对象的初始化
并不是所有的基类成员都会继承到派生类当中。
一个类的构造函数不被继承到派生类当中
派生类的初始化,
调用基类的成员函数的方法
加上基类的类控制符
例如
class A
{
public:
A(int xx);
};
A::A(int xx)
{
x = xx;
}
class B
{
public:
B(int yy);
};
B::B(int yy)
{
y = yy;
}
class C: public A,public B
{
public:
C(int xx , int yy , int zz);
private:
int z;
}
C::C(int xx , int yy , int zz)
{
A::A(xx);
B::B(yy); //当然这里的前提是可访问的
z = zz; //但是由于构造函数不可以被继承,这种方式是不
对的
}
这样才可以,即使是没有被继承下来,这种方式提供了可访问
对基类构造函数的方法
C::C(int xx,int yy,int zz):A(xx),B(yy)
{
z = zz;
}
void main()
{
C c(1,2,3);
}
第四十一讲 实验 派生类初始化
#include <iostream.h>
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;
};
void C::output()
{
cout<<x<<","<<y<<","<<z<<endl;
}
C::C(int xx,int yy,int zz):A(xx),B(yy)
{
z = zz;
}
void main()
{
C c(1,2,3);
c.output();
}