C++類和對象之封裝詳解
封裝的意義以及示例
封裝是C++面向?qū)ο笕筇卣髦?br /> 封裝的意義:
將屬性和行為作為一個整體,表現(xiàn)生活中的事物將屬性和行為加以權(quán)限控制
語法:class 類名{? 訪問權(quán)限 : 屬性? /? 行為 };

類的對象的公共數(shù)據(jù)成員可以使用直接成員訪問運算符 . 來訪問。
示例1: 設計一個圓類,求圓的周長
#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:設計一個學生類,屬性有姓名和學號,可以給姓名和學號賦值,可以顯示學生的姓名和學號
#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)限
類在設計時,可以把屬性和行為放在不同的權(quán)限下,加以控制
訪問權(quán)限有三種: 1.public 公共權(quán)限 2.protected 保護權(quán)限 3.private 私有權(quán)限
公共權(quán)限 public
成員類內(nèi)可以訪問 類外不可以訪問
class Box
{
public:
double length;
void setWidth( double wid );
double getWidth( void );
};
保護權(quán)限 protected
成員類內(nèi)可以訪問 類外不可以訪問 兒子可以訪問父親中的保護內(nèi)容 protected(受保護)成員變量或函數(shù)與私有成員十分相似,但有一點不同, protected(受保護)成員在派生類(即子類)中是可訪問的。
class Box
{
protected:
double length;
void setWidth( double wid );
double getWidth( void );
};
下面的實例與前面的實例類似,在這里 width 成員可被派生類 smallBox 的任何成員函數(shù)訪問。
私有權(quán)限 private
成員類內(nèi)可以訪問 類外不可以訪問
兒子可以訪問父親中的私有內(nèi)容
私有成員變量或函數(shù)在類的外部是不可訪問的,甚至是不可查看的。只有類和友元函數(shù)可以訪問私有成員。
默認情況下,類的所有成員都是私有的。例如在下面的類中,width 是一個私有成員,
這意味著,如果您沒有使用任何訪問修飾符,類的成員將被假定為私有成員
class Box
{
private:
double length;
void setWidth( double wid );
double getWidth( void );
};
struct 和 class的區(qū)別
在C++中struct 和class的默認訪問權(quán)限不同
區(qū)別:
struct 默認訪問權(quán)限為公共
class 默認訪問權(quán)限為私有
#include<iostream>
using namespace std;
struct C1
{
int m_A;//默認權(quán)限為公有
};
class C2
{
int m_A;//默認權(quán)限為私有
};
int main()
{
C1 c1;
C2 c2;
c1.m_A = 100;
//c2.m_A = 100; 此處無法訪問
return 0;
}
成員屬性私有化
優(yōu)點1:將所有成員設置為私有,可以自己控制讀寫權(quán)限
優(yōu)點2:對于寫權(quán)限,我們可以檢測數(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ù)和成員函數(shù)判斷兩個立方體是否相等
代碼實現(xiàn):
#include<iostream>
#include<string>
using namespace std;
class Cube
{
public:
//設置長
void setL(int l)
{
m_L = l;
}
//設置寬
void setW(int w)
{
m_W = w;
}
//設置高
void setH(int h)
{
m_H = h;
}
//獲取長
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ù)判斷兩個立方體是否相等
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;//長
int m_W;//寬
int m_H;//高
};
//利用全局函數(shù)判斷兩個立方體是否相等
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:點和圓的關(guān)系
要求:設計一個圓類型(Cricle),和一個(Point),計算點和圓的關(guān)系。
1. 點在圓外
2.點在圓內(nèi)
3.點在圓上
#include<iostream>
using namespace std;
class Point
{
public:
//設置x
void setX(int x)
{
m_X = x;
}
//設置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:
//設置半徑
void setR(int r)
{
m_R = r;
}
//獲取半徑
int getR()
{
return m_R;
}
//設置圓心
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;//圓心
};
//判斷點和圓的關(guān)系
void isInCircle(Circle& c, Point& p)
{
//計算距離的平方
int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) +
(c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
//計算半徑的平方
int RDistance = c.getR() * c.getR();
//判斷關(guān)系
if (distance == RDistance)
cout << "點在圓上" << endl;
else if (distance > RDistance)
cout << "點在圓外" << endl;
else
cout << "點在圓內(nèi)" << endl;
}
int main()
{
//創(chuàng)建一個圓
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++類和對象之封裝詳解的文章就介紹到這了,更多相關(guān)C++封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)LeetCode(126.詞語階梯之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(126.詞語階梯之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
C/C++中數(shù)據(jù)類型轉(zhuǎn)換詳解及其作用介紹
這篇文章主要介紹了C/C++中數(shù)據(jù)類型轉(zhuǎn)換詳解及其作用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

