欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ 中封裝的含義和簡(jiǎn)單實(shí)現(xiàn)方式

 更新時(shí)間:2022年07月25日 09:57:45   作者:止步聽(tīng)風(fēng)  
這篇文章主要介紹了C++ 中封裝的含義和簡(jiǎn)單實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

其實(shí)封裝并不是編程中的一個(gè)思想,對(duì)于很多領(lǐng)域來(lái)說(shuō)都是這樣。對(duì)于電子器件來(lái)說(shuō),我們不關(guān)心其內(nèi)部的結(jié)構(gòu),只在乎該器件能夠?qū)崿F(xiàn)什么樣的功能。這樣對(duì)于顧客來(lái)說(shuō),不用花時(shí)間研究?jī)?nèi)部的實(shí)現(xiàn)過(guò)程,而對(duì)于商家來(lái)說(shuō),也可以更好的保護(hù)它們的商業(yè)秘密。

而對(duì)于 C++ 來(lái)說(shuō)也是這樣,借由數(shù)據(jù)類(lèi)型也可以實(shí)現(xiàn)封裝。這樣做的好處就是對(duì)外屏蔽了功能實(shí)現(xiàn),對(duì)內(nèi)開(kāi)放了數(shù)據(jù)權(quán)限。

C++ 中的類(lèi)和對(duì)象是經(jīng)由 C 中的 struct 發(fā)展而來(lái)的,就好像 struct 是由數(shù)組發(fā)展而來(lái)的一樣。因此我們可以先通過(guò) struct 實(shí)現(xiàn)封裝。

封裝實(shí)現(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;
}

結(jié)果為:

1 2
3 4
4 6
-2 -2
-5 10

上面的程序使用 struct 構(gòu)建了類(lèi)似復(fù)數(shù)的結(jié)果,并使用運(yùn)算符重載實(shí)現(xiàn)了復(fù)數(shù)的加、減、乘運(yùn)算。這樣如果我們要進(jìn)行復(fù)數(shù)的運(yùn)算的話(huà),可以直接使用 +-* 而不用具體關(guān)心內(nèi)部的實(shí)現(xiàn)過(guò)程,因?yàn)槲覀冊(cè)谝獾闹皇墙Y(jié)果的正確性。

封裝屬性

封裝的作用就像之前提到的那樣:對(duì)外提供接口,對(duì)內(nèi)提供數(shù)據(jù)。

雖然上邊的函數(shù)在全局構(gòu)建了接口函數(shù),但是卻也暴露了函數(shù)的實(shí)現(xiàn)過(guò)程,并且我們還能夠在外部直接訪問(wèn) struct 內(nèi)的數(shù)據(jù),這并不是我們想要的封裝形式。這是由 struct 的性質(zhì)決定的,在 C++ 中,提供了 class 的形式實(shí)現(xiàn)整個(gè)的封裝過(guò)程。

struct 和 class 的不同在于,struct 中的數(shù)據(jù)和方法都是 public 的,而 class 中的數(shù)據(jù)和方法卻是可以自定義的:

 

屬性內(nèi)部外部
publicyes

yes

protectedyesno
privateyesno

protected 和 private 的區(qū)別在繼承形式上。

class 封裝

對(duì)于上邊的 complex,如果使用 class 來(lái)封裝:

#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;
}

結(jié)果為:

1 2
3 4
4 6
-2 -2
-5 10

上邊的程序使用 class 的概念封裝了 complex 的形式,該形式下能夠從外部調(diào)用對(duì)象的方法,但是卻不能夠從外部訪問(wèn)對(duì)象的數(shù)據(jù),達(dá)到了封裝的要求。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論