網(wǎng)站首頁 編程語言 正文
其實封裝并不是編程中的一個思想,對于很多領域來說都是這樣。對于電子器件來說,我們不關心其內(nèi)部的結構,只在乎該器件能夠?qū)崿F(xiàn)什么樣的功能。這樣對于顧客來說,不用花時間研究內(nèi)部的實現(xiàn)過程,而對于商家來說,也可以更好的保護它們的商業(yè)秘密。
而對于 C++ 來說也是這樣,借由數(shù)據(jù)類型也可以實現(xiàn)封裝。這樣做的好處就是對外屏蔽了功能實現(xiàn),對內(nèi)開放了數(shù)據(jù)權限。
C++ 中的類和對象是經(jīng)由 C 中的 struct 發(fā)展而來的,就好像 struct 是由數(shù)組發(fā)展而來的一樣。因此我們可以先通過 struct 實現(xiàn)封裝。
封裝實現(xiàn)
#include <iostream>
?
using std::cout;
using std::endl;
?
typedef struct complex
{
? ? int x;
? ? int y;
}COMP;
?
void init(COMP &tmp,int x,int y)
{
? ? tmp.x = x;
? ? tmp.y = y;
}
?
COMP * operator +(COMP &tmp1,COMP &tmp2)
{
? ? COMP *p = static_cast<COMP *>(new COMP);
? ? p->x = tmp1.x + tmp2.x;
? ? p->y = tmp1.y + tmp2.y;
? ? return p;
}
?
COMP * operator -(COMP &tmp1,COMP &tmp2)
{
? ? COMP *p = static_cast<COMP *>(new COMP);
? ? p->x = tmp1.x - tmp2.x;
? ? p->y = tmp1.y - tmp2.y;
? ? return p;
}
?
COMP * operator *(COMP &tmp1,COMP &tmp2)
{
? ? COMP *p = static_cast<COMP *>(new COMP);
? ? p->x = tmp1.x*tmp2.x - tmp1.y*tmp2.y;
? ? p->y = tmp1.x*tmp2.y + tmp1.y*tmp2.x;
? ? return p;
}
?
int main()
{
? ? COMP x,y;
? ? init(x,1,2);
? ? init(y,3,4);
? ? cout<<x.x<<" "<<x.y<<endl;
? ? cout<<y.x<<" "<<y.y<<endl;
?
? ? COMP *z;
? ? z = x+y;
? ? cout<<z->x<<" "<<z->y<<endl;
? ? delete z;
?
? ? z = x-y;
? ? cout<<z->x<<" "<<z->y<<endl;
? ? delete z;
?
? ? z = x*y;
? ? cout<<z->x<<" "<<z->y<<endl;
? ? delete z;
?
? ? return 0;
}
結果為:
1 2
3 4
4 6
-2 -2
-5 10
上面的程序使用 struct 構建了類似復數(shù)的結果,并使用運算符重載實現(xiàn)了復數(shù)的加、減、乘運算。這樣如果我們要進行復數(shù)的運算的話,可以直接使用 +-* 而不用具體關心內(nèi)部的實現(xiàn)過程,因為我們在意的只是結果的正確性。
封裝屬性
封裝的作用就像之前提到的那樣:對外提供接口,對內(nèi)提供數(shù)據(jù)。
雖然上邊的函數(shù)在全局構建了接口函數(shù),但是卻也暴露了函數(shù)的實現(xiàn)過程,并且我們還能夠在外部直接訪問 struct 內(nèi)的數(shù)據(jù),這并不是我們想要的封裝形式。這是由 struct 的性質(zhì)決定的,在 C++ 中,提供了 class 的形式實現(xiàn)整個的封裝過程。
struct 和 class 的不同在于,struct 中的數(shù)據(jù)和方法都是 public 的,而 class 中的數(shù)據(jù)和方法卻是可以自定義的:
?
屬性 | 內(nèi)部 | 外部 |
public | yes | yes |
protected | yes | no |
private | yes | no |
protected 和 private 的區(qū)別在繼承形式上。
class 封裝
對于上邊的 complex,如果使用 class 來封裝:
#include <iostream>
using std::cout;
using std::endl;
class complex
{
public:
complex()
{
this->x = 0;
this->y = 0;
}
complex(int x, int y):x(x),y(y){}
complex * operator +(complex &tmp)
{
complex *p = static_cast<complex *>(new complex);
p->x = this->x + tmp.x;
p->y = this->y + tmp.y;
return p;
}
complex * operator -(complex &tmp)
{
complex *p = static_cast<complex *>(new complex);
p->x = this->x - tmp.x;
p->y = this->y - tmp.y;
return p;
}
complex * operator *(complex &tmp)
{
complex *p = static_cast<complex *>(new complex);
p->x = this->x*tmp.x - this->y*tmp.y;
p->y = this->x*tmp.y + this->y*tmp.x;
return p;
}
void display()
{
cout<<this->x<<" "<<this->y<<endl;
}
private:
int x;
int y;
};
int main()
{
complex x(1,2),y(3,4);
x.display();
y.display();
complex *z;
z = x+y;
z->display();
delete z;
z = x-y;
z->display();
delete z;
z = x*y;
z->display();
delete z;
return 0;
}
結果為:
1 2
3 4
4 6
-2 -2
-5 10
上邊的程序使用 class 的概念封裝了 complex 的形式,該形式下能夠從外部調(diào)用對象的方法,但是卻不能夠從外部訪問對象的數(shù)據(jù),達到了封裝的要求。
原文鏈接:https://blog.csdn.net/SAKURASANN/article/details/105802854
相關推薦
- 2022-04-16 Python實現(xiàn)杰卡德距離以及環(huán)比算法講解_python
- 2022-05-14 C++?STL中vector容器的使用_C 語言
- 2022-07-09 docker 中進程的信號
- 2022-04-14 Python實現(xiàn)注冊登錄功能_python
- 2022-09-19 pytorch中使用LSTM詳解_python
- 2023-12-09 【設計模式-3.1】結構型——外觀模式
- 2022-10-19 Android?webview加載H5方法詳細介紹_Android
- 2022-05-08 ASP.NET?MVC視圖尋址_實用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支