阿牛哥C++笔记---8

第三十九讲 实验 派生类中从基类中继承过画的成员的控制属

用实验证明

通过继承,数据成员可以访问,而派生类不可以访问

通过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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值