C++继承访问权限

关于C++的继承模式

一、public 继承

(1)若一个类关于一个基类公用继承那么,建立这个类的对象时可以调用基类里面的公有成员。

而在此类内可以调用基类的公有以及保护的数据成员,但不能调用的私有。对于私有数据,这个类还是把他们都继承过来了的只是不可用而已。

若在son类中的public里加了一句:

father::Fa;//原为基类的保护成员,本不可调用

那么此时在建立son的对象时可以调用Fa。若加到protected 或 private 明显在对象中是不可调用的。

class father {
protected:
	int Fa;
private :
	int Fb;
public:
	int Fc;
	father(int a=0, int b=0, int c=0):Fa(a),Fb(b),Fc(c) {}
	~father() {
		cout << "father destructor!" << endl;
	}
};
class son : public father {
public:
	int Sa;
	son(int a=0,int b=0,int c=0):Sa(a),Sb(b),Sc(c) {}
	~son() {
		cout << "son destructor!!!" << endl;
	}
protected:
	int Sb;
private:
	int Sc;
};

(2)若有三层继承,如下代码

class father {
protected:
	int Fa;
private :
	int Fb;
public:
	int Fc;
	father(int a=0, int b=0, int c=0):Fa(a),Fb(b),Fc(c) {}
	~father() {
		cout << "father destructor!" << endl;
	}
};
class son : public father {
public:
	int Sa;
	son(int a=0,int b=0,int c=0):Sa(a),Sb(b),Sc(c) {}
	~son() {
		cout << "son destructor!!!" << endl;
	}
protected:
	int Sb;
private:
	int Sc;
};
class grand :public son {
public :
	grand(int a=0, int b=0,int c=0):Ca(a),Cb(b),Cc(c){}
	int Ca;
protected:
	int Cb;
private: 
	int Cc;
};

此时建立grand 的对象,仍然可以调用father的公用数据成员,且可调用son的公用数据成员。在grand的类内可以调用father类的公有以及保护成员,也可以调用son的公有以及保护成员。

二、protected 继承

(1)二重继承时

protected:
	int Fa;
private :
	int Fb;
public:
	int Fc;
	father(int a=0, int b=0, int c=0):Fa(a),Fb(b),Fc(c) {}
	~father() {
		cout << "father destructor!" << endl;
	}
};
class son : protected father {
public:
	int Sa;
	son(int a=0,int b=0,int c=0):Sa(a),Sb(b),Sc(c) {}
	~son() {
		cout << "son destructor!!!" << endl;
	}
protected:
	int Sb;
private:
	int Sc;
};

建立son的对象只能调用son的公有成员,不能调用father的数据了。

而在son内的话除了私有成员应该都还是可以调用的

(2)三重继承(第三重是公有,第二重是protect)

class father {
protected:
	int Fa;
private :
	int Fb;
public:
	int Fc;
	father(int a=0, int b=0, int c=0):Fa(a),Fb(b),Fc(c) {}
	~father() {
		cout << "father destructor!" << endl;
	}
};
class son : protected father {
public:
	int Sa;
	son(int a=0,int b=0,int c=0):Sa(a),Sb(b),Sc(c) {}
	~son() {
		cout << "son destructor!!!" << endl;
	}
protected:
	int Sb;
private:
	int Sc;
};
class grand :public son {
public :
	grand(int a=0, int b=0,int c=0):Ca(a),Cb(b),Cc(c){}
	int Ca;
protected:
	int Cb;
private: 
	int Cc;
};

此时grand内可以调用son的公有以及保护成员,也可调用father的公有以及保护。

对于grand的对象只能调用son的公有成员和自己的公有数据。

(3)三重继承(都是protect)

class father {
protected:
	int Fa;
private :
	int Fb;
public:
	int Fc;
	father(int a=0, int b=0, int c=0):Fa(a),Fb(b),Fc(c) {}
	~father() {
		cout << "father destructor!" << endl;
	}
};
class son : protected father {
public:
	int Sa;
	son(int a=0,int b=0,int c=0):Sa(a),Sb(b),Sc(c) {}
	~son() {
		cout << "son destructor!!!" << endl;
	}
protected:
	int Sb;
private:
	int Sc;
};
class grand :protected son {
public :
	grand(int a=0, int b=0,int c=0):Ca(a),Cb(b),Cc(c){
	}
	int Ca;
protected:
	int Cb;
private: 
	int Cc;
};

其实也差不多在grand内可以调用father,以及son 的公有以及保护内容。

而建立grand的对象的话,只能调用grand的公有成员了

三、private 继承

(1)二重继承

在son类内还是可以调用father的非private的成员。

其实private继承把father封闭了,除非用:

	father::Fa;
	father::Fc;

这两句在son的public内用就可在son的对象中用father的这两个数据,而在protect里面对象就不可用,而son自己可用。

(2)三重private继承

class father {
protected:
	int Fa;
private :
	int Fb;
public:
	int Fc;
	father(int a=0, int b=0, int c=0):Fa(a),Fb(b),Fc(c) {}
	~father() {
		cout << "father destructor!" << endl;
	}
};
class son : private father {
public:
	int Sa;
	son(int a=0,int b=0,int c=0):Sa(a),Sb(b),Sc(c) {
	}
	~son() {
		cout << "son destructor!!!" << endl;
	}
protected:
	int Sb;
private:
	int Sc;
};
class grand :private son {
public :
	grand(int a=0, int b=0,int c=0):Ca(a),Cb(b),Cc(c){
		
	}
	int Ca;
protected:
	int Cb;
private: 
	int Cc;
};

若没有意外,在grand内可以访问son的公有和保护,而不可访问father。

而有如下情节就不一样了:

class father {
protected:
	int Fa;
private :
	int Fb;
public:
	int Fc;
	father(int a=0, int b=0, int c=0):Fa(a),Fb(b),Fc(c) {}
	~father() {
		cout << "father destructor!" << endl;
	}
};
class son : private father {
public:
	int Sa;
	father::Fa;//!!!!!!!!!!!注意这里
	father::Fc;//!!!!!!!!!!!注意这里
	son(int a=0,int b=0,int c=0):Sa(a),Sb(b),Sc(c) {
	}
	~son() {
		cout << "son destructor!!!" << endl;
	}
protected:
	int Sb;
private:
	int Sc;
};
class grand :private son {
public :
	grand(int a=0, int b=0,int c=0):Ca(a),Cb(b),Cc(c){
	
	}
	int Ca;
protected:
	int Cb;
private: 
	int Cc;
};

代码中的注意部分出现那么即使在grand那是private继承,在grand类内还是可以访问Fa,Fc,同样可以访问son的非私有成员。

当然建立了对象就不可了。

四、总结

​ 反正不管怎么继承父类的私有数据咋都动不了,其他看情况即可。

你可能感兴趣的:(C++,c++,开发语言)