公有继承私有继承

//Point.h
#ifndef _POINT_H
#define _POINT_H
class Point{
	public:
		void initPoint(float x = 0, float y = 0){
			this->x = x;
			this->y = y; 
		}
		void move(float offX, float offY){
			x += offX;
			y += offY;
		} 
		float getX() const {
			return x;
		}
		float getY() const {
			return y;
		}
		
	private:
		float x, y;
};
#endif


//Rectangle.h
#ifndef _RECTANGLE_H
#define _RECTANGLE_H
#include "Point.h"
class Rectangle: public Point{ //派生类定义部分 
	public:
		void initRectangle(float x, float y, float w, float h){   //新增公有函数 
			initPoint(x, y);   //调用基类共有成员函数
			this->w = w;
			this->h = h; 
		} 
		float getH() const {
			return h;
		}
		float getW() const {
			return w;
		}
		
	private:
		float w, h;
}; 
#endif


/*公有继承*/ 
#include <iostream>
#include <cmath>
#include "Rectangle.h"
using namespace std;
int main(){
	Rectangle rect;
	rect.initRectangle(2, 3, 20, 10);
	rect.move(3, 2);
	cout <<"The data of rect(x, y, w, h): " << endl;
	cout << rect.getX() << ", " << rect.getY() << ", " << rect.getW() << ", " << rect.getH() << endl; 
	return 0;
}


/*私有继承*/ 
#include <iostream>
using namespace std;

class Point{
	public:
		void initPoint(float x = 0, float y = 0){
			this->x = x;
			this->y = y; 
		}
		void move(float offX, float offY){
			x += offX;
			y += offY;
		} 
		float getX() const {
			return x;
		}
		float getY() const {
			return y;
		}
		
	private:
		float x, y;
};
class Rectangle: private Point{ //派生类定义部分 
	public:
		void initRectangle(float x, float y, float w, float h){   //新增公有函数 
			initPoint(x, y);   //调用基类共有成员函数
			this->w = w;
			this->h = h; 
		} 
		void move(float offX, float offY){  
			Point::move(offX, offY); //私有继承而来的成员函数在类外不能直接访问,必须要在类里定义访问接口 
		}
		float getX() const {
			return Point::getX(); 
		}
		float getY() const{
			return Point::getY();
		}
		float getH() const {
			return h;
		}
		float getW() const {
			return w;
		}
		
	private:
		float w, h;
}; 


int main(){
	Rectangle rect;
	rect.initRectangle(2, 3, 20, 10);
	rect.move(3, 2);
	cout <<"The data of rect(x, y, w, h): " << endl;
	cout << rect.getX() << ", " << rect.getY() << ", " << rect.getW() << ", " << rect.getH() << endl; 
	return 0;
}

### C++ 中公有继承私有继承的区别及使用场景 #### 一、定义区别 在 C++ 的继承机制中,公有继承私有继承的主要差异在于基类成员的访问权限如何传递到派生类以及外部环境中。 - **公有继承**:当一个类通过 `public` 关键字继承另一个类时,基类的公共成员会保持其公共属性,保护成员则变为保护属性[^3]。这意味着这些成员可以被派生类的对象直接访问。 - **私有继承**:当一个类通过 `private` 关键字继承另一个类时,基类的所有非私有成员都会成为派生类中的私有成员[^4]。因此,即使基类的某些成员原本是公开的,在私有继承下也无法由派生类对象直接访问。 #### 二、语义上的不同 - 公有继承通常用于表达“is-a”的关系,即派生类是一种特定类型的基类实例。例如,“狗”是一个动物的一种形式,这种情况下适合采用公有继承[^1]。 - 私有继承更多体现的是“implemented-in-terms-of”,也就是实现细节的关系。在这种模式下,派生类利用了基类的功能作为其实现的一部分,但它并不希望暴露任何关于该功能的信息给外界知道[^2]。 #### 三、具体例子说明两者用法 下面提供了一个简单的代码示例来展示这两种继承方式的实际应用: ```cpp #include <iostream> using namespace std; class Base { protected: int protectedVar; public: void setProtected(int val){ this->protectedVar = val; } }; // Public Inheritance Example class DerivedPublic : public Base{ public: void display(){ cout << "DerivedPublic Protected Variable Value: "<<this->protectedVar<<endl; } }; // Private Inheritance Example class DerivedPrivate : private Base{ // Note the 'private' keyword here. public: void useBaseFunctionalityAndDisplay(){ setProtected(789); cout << "DerivedPrivate Accessed via Member Function: "<<this->protectedVar<<endl; } }; int main() { DerivedPublic dp; dp.setProtected(100); dp.display(); DerivedPrivate dprv; dprv.useBaseFunctionalityAndDisplay(); } ``` 上述程序展示了两种不同的继承方法及其效果。对于 `DerivedPublic`, 它能够自由地调用并显示来自 `Base` 类的数据成员因为它是公用继承的结果。然而, 对于 `DerivedPrivate`, 虽然它可以内部操作原来属于 `Base` 的受保护变量 (由于它是在类体内完成的操作),但是这个字段对外界完全隐藏起来了。 #### 四、总结适用场合 - 如果需要让子类保留父类接口以便其他部分可以直接当作原始类型处理,则应选择 **公有继承**。 - 若仅想借用某个现有结构的部分逻辑而不愿将其整体特征展现出来的话,则应该倾向于运用 **私有继承** 或者组合(composition)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金州饿霸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值