面向?qū)ο缶幊蹋ㄒ唬?/strong>
一、面向?qū)ο缶幊探榻B
(一)什么是面向?qū)ο螅?/strong>
面向?qū)⑾到y(tǒng)看成通過(guò)交互作用來(lái)完成特定功能的對(duì)象的集合。每個(gè)對(duì)象用自己的方法來(lái)管理數(shù)據(jù)。也就是說(shuō)只有對(duì)象內(nèi)部的代碼能夠操作對(duì)象內(nèi)部的數(shù)據(jù)。
(二)面向?qū)ο蟮膬?yōu)點(diǎn)
·面向過(guò)程的缺點(diǎn)
不容易維護(hù),靈活性差,不容易擴(kuò)展,更談不上復(fù)用,由于客戶的需求多變,導(dǎo)致程序員加班加點(diǎn),甚至整個(gè)項(xiàng)目經(jīng)常返工。
·面向?qū)ο蟮膬?yōu)點(diǎn)
通過(guò)繼承、封裝、多態(tài)降低程序的耦合度,并結(jié)合設(shè)計(jì)模式讓程序更容易修改和擴(kuò)展,并且容易復(fù)用。
(三)面向?qū)ο蟮奶攸c(diǎn)
抽象、封裝、繼承、多態(tài)
1.抽象:
·抽象是人們認(rèn)識(shí)事物的一種方法
·抓住事物本質(zhì),而不是內(nèi)部具體細(xì)節(jié)或者具體實(shí)現(xiàn)
2.封裝
·封裝是指按照信息屏蔽的原則,把對(duì)象的屬性和操作結(jié)合一起,構(gòu)成獨(dú)立的對(duì)象。
·通過(guò)限制對(duì)屬性和操作的訪問(wèn)權(quán)限,可以將屬性“隱藏”在對(duì)象內(nèi)部,對(duì)外提供一定的接口,在對(duì)象之外只能通過(guò)接口對(duì)對(duì)象進(jìn)行操作。
·封裝性增加了對(duì)象的獨(dú)立性,從而保證數(shù)據(jù)的可靠性。
·外部對(duì)象不能直接對(duì)操作對(duì)象的屬性,只能使用對(duì)象提供的服務(wù)。
3.繼承
·繼承表達(dá)了對(duì)對(duì)象的一般與特殊的關(guān)系。特殊類(lèi)的對(duì)象具有一般類(lèi)的全部屬性和服務(wù)。
·當(dāng)定義了一個(gè)類(lèi)后,又需定義一個(gè)新類(lèi),這個(gè)新類(lèi)與原來(lái)的類(lèi)相比,只是增加或修改了部分屬性和操作,這時(shí)可以用原來(lái)的類(lèi)派生出新類(lèi),新類(lèi)中只需描述自己所特有的屬性和操作。
·繼承性大大簡(jiǎn)化了對(duì)問(wèn)題的描述,大大提高了程序的可重用性,從而提高了程序設(shè)計(jì)、修改、擴(kuò)充的效率等。
4.多態(tài)
·多態(tài)性:同一個(gè)消息被不同對(duì)象接收時(shí),產(chǎn)生不同結(jié)果,即實(shí)現(xiàn)同一接口,不同方法。
·一般類(lèi)中定義的屬性和服務(wù),在特殊類(lèi)中不改變其名字,但通過(guò)各自不同的實(shí)現(xiàn)后,可以具有不同的數(shù)據(jù)類(lèi)型或者具有不同的行為。

當(dāng)向圖形對(duì)象發(fā)送消息進(jìn)行繪圖服務(wù)請(qǐng)求后,圖形對(duì)象會(huì)自動(dòng)判斷自己的所屬類(lèi)然后執(zhí)行相應(yīng)的繪圖服務(wù)。
總結(jié):面向?qū)ο缶幊痰膬?yōu)缺點(diǎn)
面向?qū)ο缶幊痰膬?yōu)點(diǎn):
·易維護(hù):可讀性高,即使改變需求,由于繼承的存在,維護(hù)也只是在局部模塊,維護(hù)起來(lái)是非常方便和較低成本的。
·質(zhì)量高:可重用現(xiàn)有的,在以前的項(xiàng)目的領(lǐng)域中已被測(cè)試過(guò)的類(lèi)使系統(tǒng)滿足業(yè)務(wù)需求并具有較高質(zhì)量。
·效率高:在軟件開(kāi)發(fā)時(shí),根據(jù)設(shè)計(jì)的需要對(duì)現(xiàn)實(shí)世界的事物進(jìn)行抽象,產(chǎn)生類(lèi)。這樣的方法解決問(wèn)題,接近于日常生活和自然的思考方式,勢(shì)必提高軟件開(kāi)發(fā)的效率和質(zhì)量
·易擴(kuò)展:由于繼承、封裝、多態(tài)的特性,自然設(shè)計(jì)出高內(nèi)聚、低耦合的系統(tǒng)結(jié)構(gòu),使得系統(tǒng)更靈活、更容易擴(kuò)展,而且成本較低。
面向?qū)ο缶幊痰娜秉c(diǎn):
運(yùn)行效率會(huì)下降10%左右
二、類(lèi)與對(duì)象
(一)類(lèi)的聲明
//類(lèi)是一種用戶自定義類(lèi)型,聲明形式:
class 類(lèi)名稱(chēng)
{
public:
公有成員(外部接口)
PRivate:
私有成員
protected:
保護(hù)成員
};
例:
#include <iostream> using namespace std; class Test{public:int x_;protected:int y_;private:int z_;}; int main(){Test t;t.x_ = 5;t.y_ = 6; //Error!不可訪問(wèn)t.z_ = 7; //Error1不可訪問(wèn) cout << "t.x: " << t.x_ << endl;return 0;}升級(jí)版:
#include <iostream> using namespace std; class Test{public:int x_;void setX(int x){x_ = x;}void setY(int y){y_ = y;}void setZ(int z){z_ = z;}int getX(){return x_;}int getY(){return y_;}int getZ(){return z_;}protected:int y_;private:int z_;}; int main(){Test t;t.x_ = 5; cout << "t.x: " << t.x_ << endl;cout << "t.y: " << t.getY() << endl;cout << "t.z: " << t.getZ() << endl;return 0;}編譯結(jié)果:

PS:y,z未進(jìn)行初始化所以是亂碼。
(二)public、private、protected
·在關(guān)鍵字public后面聲明,它們是類(lèi)與外部的接口,外部函數(shù)都可以訪問(wèn)公有類(lèi)型數(shù)據(jù)和函數(shù)。
·在關(guān)鍵詞private后面聲明,只允許本類(lèi)中的函數(shù)訪問(wèn),而類(lèi)外部的任何函數(shù)都不能訪問(wèn)。
·在關(guān)鍵詞protected后面聲明,與private類(lèi)似,其差別表現(xiàn)在繼承與派生時(shí)對(duì)派生類(lèi)的影響不同。
(三)成員函數(shù)
1.類(lèi)內(nèi)實(shí)現(xiàn)成員函數(shù)
函數(shù)類(lèi)內(nèi)實(shí)現(xiàn)默認(rèn)為inline函數(shù),占用空間,所有一般類(lèi)外實(shí)現(xiàn)函數(shù)
上述例題中函數(shù)為類(lèi)內(nèi)實(shí)現(xiàn)
2.類(lèi)外實(shí)現(xiàn)成員函數(shù)
例:
Test.h #ifndef _TEST_H_ #define _TEST_H_ class Test { public: int x_; void initXYZ(int x, int y, int z); void display(); protected: int y_; private: int z_; };#endifTest.c: #include "Test.h" #include <iostream> using namespace std; void Test::initXYZ(int x, int y, int z) { x_ = z; y_ = y; z_ = x; } void Test::display() { cout << "x:" << x_ << "/t" << "y:" << y_ << "/t" << "x:" << z_ << "/t" << endl; }lei.c:#include <iostream>#include "Test.h" using namespace std; int main(){Test t; t.initXYZ(1, 2, 3);t.display(); return 0;}運(yùn)行結(jié)果:


3.成員函數(shù)的重載及默認(rèn)參數(shù)
(四)class VS struct
class數(shù)據(jù)成員默認(rèn)私有
struct數(shù)據(jù)成員默認(rèn)公有
例:
#include <iostream> using namespace std; class Test{public:int x_;int y_;int z_;};struct Test1{int x_;int y_;int z_; void initXYZ(int x, int y, int z){x_ = x;y_ = y;z_ = z;}};int main(){Test t1;t1.x_ = 5; Test1 t2;t2.initXYZ(1, 2, 3); cout << "t1:" << t1.x_ << endl;cout << "t2:" << t2.x_ << endl; cout << sizeof(t1) << endl;cout << sizeof(t2) << endl; return 0; }運(yùn)行結(jié)果:

C++編譯器對(duì)struct升級(jí):
在結(jié)構(gòu)體內(nèi)可以加入函數(shù)
由運(yùn)行結(jié)果可知:
·類(lèi)的大小與成員函數(shù)無(wú)關(guān),只與成員有關(guān)
·對(duì)齊方式與大小與結(jié)構(gòu)體一樣
·對(duì)象的大小由成員決定
·方法是共享的
(五)對(duì)象的存儲(chǔ)類(lèi)型

代碼驗(yàn)證:
#include <iostream>using namespace std; class Test{private:int x_;int y_;int z_;public:void initXYZ(int x, int y, int z){this->x_ = x;this->y_ = y;this->z_ = z;}};int main(){Test *t1 = new Test();t1->initXYZ(1, 2, 4); return 0;}調(diào)試結(jié)果:

(六)類(lèi)的作用域
前向聲明:只能定義指針或者引用,不能有另一個(gè)類(lèi)的對(duì)象
代碼示例:
A.h#ifndef _A_H_#define _A_H_ class B;//前向聲明 class A{public:A();~A();private:B *b; //B &b};#endifB.h#ifndef _B_H_#define _B_H_ class B{public:B();~B();};#endifA.cpp#include "A.h"#include <iostream> using namespace std; A::A(){cout << "init A!" << endl;}A::~A(){cout << "destory A!" << endl;}B.cpp#include "B.h"#include <iostream> using namespace std; B::B(){cout << "init B!" << endl;}B :: ~B(){cout << "destory B!" << endl;}main.c#include <iostream>#include "A.h"#include "B.h" int main(){return 0;}編譯結(jié)果:

(七)嵌套類(lèi)(內(nèi)部類(lèi))
·從作用域的角度看,嵌套類(lèi)被隱藏在外圍類(lèi)中,該類(lèi)名只能在外圍類(lèi)中使用。如果
在外圍類(lèi)的作用域使用該類(lèi)名時(shí),需要加名字限定
·嵌套類(lèi)中的成員函數(shù)可以在它的類(lèi)體外定義
·嵌套類(lèi)的成員函數(shù)對(duì)外圍類(lèi)的成員沒(méi)有訪問(wèn)權(quán),反之亦然。
·嵌套類(lèi)僅僅只是語(yǔ)法上的嵌入
(八)局部類(lèi)
·類(lèi)也可以定義在函數(shù)體內(nèi),這樣的類(lèi)被稱(chēng)為局部類(lèi)(local class)。局
部類(lèi)只在定義它的局部域內(nèi)可見(jiàn)
·局部類(lèi)的成員函數(shù)必須被定義在類(lèi)體中。
·局部類(lèi)中不能有靜態(tài)成員
代碼示例:
#include <iostream> using namespace std; class Test{public: int x_; class Inner //嵌套類(lèi) { public: int num; void func(); }; void setX(int x) { x_ = x;display(); } void setY(int y) {y_ = y;display(); } void setZ(int z) { z_ = z; } int getX() { return x_; } int getY() { return y_; } int getZ() { return z_; } protected: int y_; void display() { cout << x_ << y_ << z_ << endl; } private: int z_;}; void Test::Inner::func(){ cout << "hello world" << endl;} int main() { class LocalClass //局部類(lèi) { void func(){ } } Test t; Test::Inner n; t.x_ = 6; t.setY(6); t.setZ(7); cout << "t.y_ = " << t.getY() << endl; cout << "t.z_ = " << t.getZ() << endl; cout << "t.x_ = " << t.x_ << endl; return 0;}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注