手機(jī)網(wǎng)站開發(fā)公司百度搜索引擎api
1) C++ 中面向?qū)ο缶幊倘绾螌?shí)現(xiàn)數(shù)據(jù)隱藏?
在 C++ 中,數(shù)據(jù)隱藏是通過將類的成員變量和方法的訪問權(quán)限控制起來實(shí)現(xiàn)的。通常,數(shù)據(jù)隱藏是通過使用 訪問控制 機(jī)制來實(shí)現(xiàn)的,C++ 提供了三種訪問控制修飾符:
- private: 使成員變量和成員函數(shù)只能在類的內(nèi)部訪問,外部無法直接訪問。這是實(shí)現(xiàn)數(shù)據(jù)隱藏的主要方式。
- protected: 使成員變量和成員函數(shù)在當(dāng)前類和派生類中可以訪問,但外部代碼無法訪問。
- public: 使成員變量和成員函數(shù)可以被類的外部直接訪問。
通過將類的內(nèi)部數(shù)據(jù)(如成員變量)聲明為 private
或 protected
,并通過 public
成員函數(shù)來提供對這些數(shù)據(jù)的間接訪問,我們可以有效地隱藏類的內(nèi)部實(shí)現(xiàn)細(xì)節(jié),從而實(shí)現(xiàn)數(shù)據(jù)封裝和數(shù)據(jù)隱藏。
示例代碼:
#include <iostream>
using namespace std;
class Account {
private: double balance; // 余額是私有的,外部無法直接訪問
public: // 構(gòu)造函數(shù) Account(double initial_balance) {
if (initial_balance >= 0) {
balance = initial_balance;
}
else {
balance = 0;
cout << "Initial balance must be positive." << endl;
} } // 提供公共方法來訪問和修改余額
void deposit(double amount) {
if (amount > 0) {
balance += amount;
} }
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
cout << "Invalid withdrawal amount." << endl;
} }
double getBalance() const {
return balance;
} };
int main() {
Account myAccount(1000);
myAccount.deposit(500);
cout << "Balance: " << myAccount.getBalance() << endl;
myAccount.withdraw(300);
cout << "Balance after withdrawal: " << myAccount.getBalance() << endl; return 0;
}
在上面的例子中,balance
是私有的,外部無法直接訪問。通過 deposit
、withdraw
和 getBalance
等公有函數(shù)來訪問和修改 balance
,這樣就隱藏了類的實(shí)現(xiàn)細(xì)節(jié),防止了外部直接修改余額的風(fēng)險(xiǎn)。
2) C++ 中面向?qū)ο缶幊倘绾翁幚懋惓?#xff1f;
C++ 使用 異常處理機(jī)制(Exception Handling)來處理程序運(yùn)行過程中可能出現(xiàn)的錯誤。異常機(jī)制通過 try
、throw
和 catch
語句實(shí)現(xiàn)。
try
塊:包含可能拋出異常的代碼。throw
語句:用于拋出異常。catch
塊:捕獲并處理異常。
在面向?qū)ο缶幊讨?#xff0c;異常處理通常涉及到拋出和捕獲自定義的異常類。C++ 允許開發(fā)者定義自己的異常類型,并且可以通過繼承標(biāo)準(zhǔn)異常類(如 std::exception
)來創(chuàng)建特定類型的異常。
示例代碼:
#include <iostream>
#include <stdexcept> // 引入標(biāo)準(zhǔn)異常類
using namespace std;
// 自定義異常類
class InsufficientFundsException : public exception {
public: const char* what() const noexcept override {
return "Insufficient funds in the account!";
} };
class Account {
private: double balance;
public: Account(double initial_balance) {
if (initial_balance < 0) {
throw invalid_argument("Initial balance cannot be negative.");
}
balance = initial_balance;
}
void deposit(double amount) {
if (amount <= 0) { throw invalid_argument("Deposit amount must be positive.");
}
balance += amount;
}
void withdraw(double amount) {
if (amount > balance) {
throw InsufficientFundsException(); // 拋出自定義異常
}
balance -= amount;
}
double getBalance() const {
return balance; } }; int main() {
try { Account myAccount(500); myAccount.deposit(200);
myAccount.withdraw(800); // 這將拋出異常
}
catch (const InsufficientFundsException& e) {
cout << "Error: " << e.what() << endl; } catch (const exception& e) {
cout << "Standard Exception: " << e.what() << endl;
}
catch (...) {
cout << "Unknown exception occurred." << endl; } return 0;
}
關(guān)鍵點(diǎn):
- 異常拋出:當(dāng)
withdraw
方法檢測到余額不足時,拋出了InsufficientFundsException
異常。 - 異常捕獲:使用
catch
塊捕獲特定的異常類型??梢愿鶕?jù)不同的異常類型執(zhí)行不同的處理邏輯。 - 標(biāo)準(zhǔn)異常類:C++ 標(biāo)準(zhǔn)庫提供了許多預(yù)定義的異常類,如
std::invalid_argument
、std::out_of_range
、std::runtime_error
等,通常會繼承自std::exception
。
總結(jié):
- 數(shù)據(jù)隱藏:通過使用
private
或protected
訪問修飾符,并通過public
方法進(jìn)行數(shù)據(jù)訪問和操作來實(shí)現(xiàn)。 - 異常處理:通過
try
、throw
和catch
來處理異常,能夠捕獲并處理程序中的錯誤。可以使用標(biāo)準(zhǔn)異常類或自定義異常類來表達(dá)不同的錯誤情況。