C++類和對(duì)象之封裝詳解
封裝的意義以及示例
封裝是C++面向?qū)ο笕筇卣髦?br /> 封裝的意義:
將屬性和行為作為一個(gè)整體,表現(xiàn)生活中的事物將屬性和行為加以權(quán)限控制
語法:class 類名{? 訪問權(quán)限 : 屬性? /? 行為 };
類的對(duì)象的公共數(shù)據(jù)成員可以使用直接成員訪問運(yùn)算符 . 來訪問。
示例1: 設(shè)計(jì)一個(gè)圓類,求圓的周長(zhǎng)
#include<iostream> using namespace std; #define PI 3.14 class Circle { //訪問權(quán)限 //公共權(quán)限 public: //屬性 int r; //行為 double calculate() { return 2 * PI * r; } }; int main() { Circle a; a.r = 2; cout << a.calculate() << endl; system("pause"); return 0; }
示例2:設(shè)計(jì)一個(gè)學(xué)生類,屬性有姓名和學(xué)號(hào),可以給姓名和學(xué)號(hào)賦值,可以顯示學(xué)生的姓名和學(xué)號(hào)
#include<iostream> #include<string> using namespace std; class student { public: string name; int Id; void CinSudent() { cin >> name; cin >> Id; } void ShowStudent() { cout << name << endl; cout << Id << endl; } }; int main() { student a; a.CinSudent(); a.ShowStudent(); system("pause"); return 0; }
訪問權(quán)限
類在設(shè)計(jì)時(shí),可以把屬性和行為放在不同的權(quán)限下,加以控制
訪問權(quán)限有三種: 1.public 公共權(quán)限 2.protected 保護(hù)權(quán)限 3.private 私有權(quán)限
公共權(quán)限 public
成員類內(nèi)可以訪問 類外不可以訪問
class Box { public: double length; void setWidth( double wid ); double getWidth( void ); };
保護(hù)權(quán)限 protected
成員類內(nèi)可以訪問 類外不可以訪問 兒子可以訪問父親中的保護(hù)內(nèi)容 protected(受保護(hù))成員變量或函數(shù)與私有成員十分相似,但有一點(diǎn)不同, protected(受保護(hù))成員在派生類(即子類)中是可訪問的。
class Box { protected: double length; void setWidth( double wid ); double getWidth( void ); };
下面的實(shí)例與前面的實(shí)例類似,在這里 width 成員可被派生類 smallBox 的任何成員函數(shù)訪問。
私有權(quán)限 private
成員類內(nèi)可以訪問 類外不可以訪問
兒子可以訪問父親中的私有內(nèi)容
私有成員變量或函數(shù)在類的外部是不可訪問的,甚至是不可查看的。只有類和友元函數(shù)可以訪問私有成員。
默認(rèn)情況下,類的所有成員都是私有的。例如在下面的類中,width 是一個(gè)私有成員,
這意味著,如果您沒有使用任何訪問修飾符,類的成員將被假定為私有成員
class Box { private: double length; void setWidth( double wid ); double getWidth( void ); };
struct 和 class的區(qū)別
在C++中struct 和class的默認(rèn)訪問權(quán)限不同
區(qū)別:
struct 默認(rèn)訪問權(quán)限為公共
class 默認(rèn)訪問權(quán)限為私有
#include<iostream> using namespace std; struct C1 { int m_A;//默認(rèn)權(quán)限為公有 }; class C2 { int m_A;//默認(rèn)權(quán)限為私有 }; int main() { C1 c1; C2 c2; c1.m_A = 100; //c2.m_A = 100; 此處無法訪問 return 0; }
成員屬性私有化
優(yōu)點(diǎn)1:將所有成員設(shè)置為私有,可以自己控制讀寫權(quán)限
優(yōu)點(diǎn)2:對(duì)于寫權(quán)限,我們可以檢測(cè)數(shù)據(jù)的有效性
示例:
#include<iostream> #include<string> using namespace std; class Person { public: void Setname(string name1) { name = name1; } string Showname() { return name; } int Showage() { return age; } private: //私有權(quán)限 string name; int age=18; }; int main() { Person a; a.Setname("張三"); cout << "姓名為: " << a.Showname() << endl; cout << "年齡為: " << a.Showage() << endl; system("pause"); return 0; }
案例1:設(shè)計(jì)立方體類
要求:
設(shè)計(jì)立方體
求出立方體的面積和體積
分別用全局函數(shù)和成員函數(shù)判斷兩個(gè)立方體是否相等
代碼實(shí)現(xiàn):
#include<iostream> #include<string> using namespace std; class Cube { public: //設(shè)置長(zhǎng) void setL(int l) { m_L = l; } //設(shè)置寬 void setW(int w) { m_W = w; } //設(shè)置高 void setH(int h) { m_H = h; } //獲取長(zhǎng) int getL() { return m_L; } //獲取寬 int getW() { return m_W; } //獲取高 int getH() { return m_H; } //獲取面積 int calculateS() { return 2 * (m_L * m_W + m_L * m_H + m_W * m_H); } //獲取體積 int calculateV() { return m_L * m_H * m_W; } //利用成員函數(shù)判斷兩個(gè)立方體是否相等 bool isSameByClass(Cube& c) { if (m_H == c.getH() && m_L == c.getL() && m_W == c.getW()) return true; else return false; } private: int m_L;//長(zhǎng) int m_W;//寬 int m_H;//高 }; //利用全局函數(shù)判斷兩個(gè)立方體是否相等 bool isSame(Cube &c1,Cube &c2) { if (c1.getH() == c2.getH() && c1.getL() == c2.getL() && c1.getW() == c2.getW()) return true; else return false; } int main() { Cube c1; c1.setH(10); c1.setL(13); c1.setW(45); cout << "c1面積是:" << c1.calculateS() << endl; cout << "c2體積是:" << c1.calculateV() << endl; Cube c2; c2.setH(10); c2.setL(13); c2.setW(45); //利用全局函數(shù)判斷 if (isSame(c1, c2)) cout << "c1和c2相等" << endl; else cout << "c1和c2不相等" << endl; //利用成員函數(shù)判斷 if (c1.isSameByClass(c2)) cout << "c1和c2相等" << endl; else cout << "c1和c2不相等" << endl; system("pause"); return 0; }
案例2:點(diǎn)和圓的關(guān)系
要求:設(shè)計(jì)一個(gè)圓類型(Cricle),和一個(gè)(Point),計(jì)算點(diǎn)和圓的關(guān)系。
1. 點(diǎn)在圓外
2.點(diǎn)在圓內(nèi)
3.點(diǎn)在圓上
#include<iostream> using namespace std; class Point { public: //設(shè)置x void setX(int x) { m_X = x; } //設(shè)置y void setY(int y) { m_Y = y; } //獲取x int getX() { return m_X; } int getY() { return m_Y; } private: int m_X; int m_Y; }; class Circle { public: //設(shè)置半徑 void setR(int r) { m_R = r; } //獲取半徑 int getR() { return m_R; } //設(shè)置圓心 void setCenter(int x, int y) { m_Center.setX(x); m_Center.setY(y); } //獲取圓心 Point getCenter() { return m_Center; } private: int m_R; //半徑 Point m_Center;//圓心 }; //判斷點(diǎn)和圓的關(guān)系 void isInCircle(Circle& c, Point& p) { //計(jì)算距離的平方 int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY()); //計(jì)算半徑的平方 int RDistance = c.getR() * c.getR(); //判斷關(guān)系 if (distance == RDistance) cout << "點(diǎn)在圓上" << endl; else if (distance > RDistance) cout << "點(diǎn)在圓外" << endl; else cout << "點(diǎn)在圓內(nèi)" << endl; } int main() { //創(chuàng)建一個(gè)圓 Circle c; c.setR(10); c.setCenter(10, 0); Point p; p.setX(10); p.setY(10); //判斷關(guān)系 isInCircle(c,p); system("pause"); return 0; }
總結(jié)
到此這篇關(guān)于C++類和對(duì)象之封裝詳解的文章就介紹到這了,更多相關(guān)C++封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ OpenCV學(xué)習(xí)之圖像金字塔與圖像融合詳解
圖像金字塔分為兩種:高斯金字塔和拉普拉斯金字塔。圖像金字塔在保持細(xì)節(jié)的條件下進(jìn)行圖像融合等多尺度編輯操作非常有用。本文將利用圖像金字塔實(shí)現(xiàn)圖像融合,需要的可以參考一下2022-03-03Matlab實(shí)現(xiàn)繪制立體玫瑰花的示例代碼
這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)繪制更立體的玫瑰花,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下2023-02-02C語言實(shí)現(xiàn)餐飲點(diǎn)餐管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)餐飲點(diǎn)餐管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01C++中l(wèi)ist的使用與模擬實(shí)現(xiàn)
list相較于vector來說會(huì)顯得復(fù)雜,它的好處是在任意位置插入,刪除都是一個(gè)O(1)的時(shí)間復(fù)雜度,下面這篇文章主要給大家介紹了關(guān)于C++中l(wèi)ist的使用與模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-05-05