C++学习笔记——友元及重载运算符

目录

一、友元

1.1声明友元函数

1.2声明友元类

二、运算符重载

2.1重载加号运算符

2.2重载流插入运算符

三、一个简单的银行管理系统

四、 详细的介绍


一、友元

在 C++ 中,友元是一个函数或类,它可以访问另一个类的私有成员或保护成员。通常情况下,我们在类定义中声明友元函数或友元类,以便它们能够访问类的私有成员。友元可以是一个函数、类、或整个命名空间。

1.1声明友元函数

首先,我们来看一下如何声明友元函数。假设我们有一个 Box 类,它表示一个三维立方体:

class Box {
public:Box(double l, double w, double h) : length(l), width(w), height(h) {}private:double length;double width;double height;
};

我们想要计算一个 Box 对象的体积,但是 lengthwidthheight 都是私有成员,无法在类外直接访问。这时候,我们可以使用友元函数来实现。我们可以在类定义中声明一个友元函数,并将其定义为一个全局函数:

class Box {
public:Box(double l, double w, double h) : length(l), width(w), height(h) {}friend double getVolume(Box box);private:double length;double width;double height;
};double getVolume(Box box) {return box.length * box.width * box.height;
}

在上面的代码中,我们在 Box 类定义中声明了一个友元函数 getVolume(),并将其定义为一个全局函数。在 getVolume() 函数中,我们可以直接访问 Box 类的私有成员 lengthwidthheight,计算出它的体积并返回。

1.2声明友元类

除了友元函数,我们还可以声明友元类。假设我们有一个 Stack 类,它表示一个栈:

template <typename T>
class Stack {
public:Stack() : top(-1) {}private:T data[10];int top;friend class StackIterator<T>;
};

我们想要实现一个迭代器类来遍历栈中的元素。但是,由于 datatop 都是私有成员,无法在迭代器类中直接访问。这时候,我们可以声明 StackIterator 类为 Stack 类的友元类:

template <typename T>
class StackIterator {
public:StackIterator(Stack<T>& s) : stack(s), index(s.top) {}T next() {return stack.data[index--];}private:Stack<T>& stack;int index;
};template <typename T>
class Stack {
public:Stack() : top(-1) {}friend class StackIterator<T>;private:T data[10];int top;
};

在上面的代码中,我们声明了一个友元类 StackIterator,并在 Stack 类定义中将其声明为友元类。在 StackIterator 类中,我们可以直接访问 Stack 类的私有成员 datatop,实现了对栈的迭代。

二、运算符重载

C++ 中有一些运算符是可以被重载的,比如加号、减号、乘号、除号等等。通过重载运算符,我们可以改变它的行为,使其能够作用于我们自定义的类型。

2.1重载加号运算符

假设我们有一个 Complex 类,它表示一个复数:

class Complex {
public:Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}private:double real;double imag;
};

我们想要实现复数的加法运算,但是加号运算符无法直接作用于自定义的类型。这时候,我们可以重载加号运算符 +,使其能够对两个 Complex 对象进行相加,并返回一个新的 Complex 对象:

class Complex {
public:Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}Complex operator+(const Complex& other) const {return Complex(real + other.real, imag + other.imag);}private:double real;double imag;
};

在上面的代码中,我们重载了加号运算符 +,并定义了一个成员函数 operator+()。在函数中,我们定义了一个新的 Complex 对象,其实部为两个复数的实部相加,虚部为两个复数的虚部相加,并将其返回。

2.2重载流插入运算符

除了加号运算符,我们还可以重载其他运算符。比如,我们可以重载流插入运算符 <<,使其能够输出我们自定义的类型。假设我们有一个 Person 类,它表示一个人:

#include <iostream>
#include <string>class Person {
public:Person(std::string n, int a) : name(n), age(a) {}friend std::ostream& operator<<(std::ostream& os, const Person& p);private:std::string name;int age;
};std::ostream& operator<<(std::ostream& os, const Person& p) {os << "Name: " << p.name << ", Age: " << p.age;return os;
}int main() {Person p("Alice", 20);std::cout << p << std::endl;return 0;
}

在上面的代码中,我们重载了流插入运算符 <<,并将其定义为一个友元函数。在函数中,我们将 Person 对象的姓名和年龄输出到标准输出流 os 中,然后返回该流。在主函数中,我们创建了一个 Person 对象 p,并使用 << 运算符将其输出到标准输出流中。

需要注意的是,在重载运算符时需要遵循一些规则。比如,我们必须使用正确的参数类型和返回值类型,以及保证运算符的语义符合预期。此外,C++ 中还存在一些运算符是无法被重载的,比如条件运算符 ?:,作用域运算符 :: 等等。

三、一个简单的银行管理系统

#include <iostream>
#include <string>
#include <vector>class Account {
public:Account(std::string name, std::string accountNumber, double balance): name(name), accountNumber(accountNumber), balance(balance) {}void deposit(double amount) {balance += amount;std::cout << "Deposit successful. New balance: " << balance << std::endl;}void withdraw(double amount) {if (balance >= amount) {balance -= amount;std::cout << "Withdrawal successful. New balance: " << balance << std::endl;} else {std::cout << "Insufficient balance." << std::endl;}}void displayInfo() {std::cout << "Name: " << name << std::endl;std::cout << "Account Number: " << accountNumber << std::endl;std::cout << "Balance: " << balance << std::endl;}private:std::string name;std::string accountNumber;double balance;
};class Bank {
public:void createAccount(std::string name, std::string accountNumber, double balance) {Account account(name, accountNumber, balance);accounts.push_back(account);std::cout << "Account created successfully." << std::endl;}void deposit(std::string accountNumber, double amount) {Account* account = findAccount(accountNumber);if (account != nullptr) {account->deposit(amount);} else {std::cout << "Account not found." << std::endl;}}void withdraw(std::string accountNumber, double amount) {Account* account = findAccount(accountNumber);if (account != nullptr) {account->withdraw(amount);} else {std::cout << "Account not found." << std::endl;}}void displayAllAccounts() {for (const auto& account : accounts) {account.displayInfo();std::cout << "---------------------------" << std::endl;}}private:std::vector<Account> accounts;Account* findAccount(std::string accountNumber) {for (auto& account : accounts) {if (account.getAccountNumber() == accountNumber) {return &account;}}return nullptr;}
};int main() {Bank bank;int choice;do {std::cout << "1. Create Account" << std::endl;std::cout << "2. Deposit" << std::endl;std::cout << "3. Withdraw" << std::endl;std::cout << "4. Display All Accounts" << std::endl;std::cout << "5. Exit" << std::endl;std::cout << "Enter your choice: ";std::cin >> choice;if (choice == 1) {std::string name, accountNumber;double balance;std::cout << "Enter name: ";std::cin >> name;std::cout << "Enter account number: ";std::cin >> accountNumber;std::cout << "Enter initial balance: ";std::cin >> balance;bank.createAccount(name, accountNumber, balance);} else if (choice == 2) {std::string accountNumber;double amount;std::cout << "Enter account number: ";std::cin >> accountNumber;std::cout << "Enter amount to deposit: ";std::cin >> amount;bank.deposit(accountNumber, amount);} else if (choice == 3) {std::string accountNumber;double amount;std::cout << "Enter account number: ";std::cin >> accountNumber;std::cout << "Enter amount to withdraw: ";std::cin >> amount;bank.withdraw(accountNumber, amount);} else if (choice == 4) {bank.displayAllAccounts();}} while (choice != 5);return 0;
}

在上面的代码中,我们定义了两个类:AccountBankAccount 类代表一个银行账户,具有姓名、账号和余额属性,以及存款、取款和展示信息的方法。Bank 类代表整个银行系统,具有创建账户、存款、取款和显示所有账户的方法。其中,存款和取款的操作会调用 Account 类相应的方法。

在主函数中,我们通过简单的菜单来模拟用户与银行系统的交互。用户可以选择创建账户、存款、取款或显示所有账户的操作,直到选择退出。

四、 详细的介绍

以下是代码中每个部分的更详细解释:

class Account {
public:Account(std::string name, std::string accountNumber, double balance): name(name), accountNumber(accountNumber), balance(balance) {}

Account 类的构造函数中,我们使用传入的参数来初始化私有成员变量 nameaccountNumberbalance

    void deposit(double amount) {balance += amount;std::cout << "Deposit successful. New balance: " << balance << std::endl;}

deposit 方法用于存款操作。它接受一个 double 类型的参数 amount,代表要存入的金额。方法内部,我们将传入的金额加到余额上,并输出新的余额。

    void withdraw(double amount) {if (balance >= amount) {balance -= amount;std::cout << "Withdrawal successful. New balance: " << balance << std::endl;} else {std::cout << "Insufficient balance." << std::endl;}}

withdraw 方法用于取款操作。它接受一个 double 类型的参数 amount,代表要取出的金额。方法内部,我们首先检查账户余额是否足够,如果足够就从余额中减去取款金额,并输出新的余额。如果余额不足,则输出错误信息。

    void displayInfo() {std::cout << "Name: " << name << std::endl;std::cout << "Account Number: " << accountNumber << std::endl;std::cout << "Balance: " << balance << std::endl;}

displayInfo 方法用于展示账户的信息。它输出账户的姓名、账号和余额。

class Bank {
public:void createAccount(std::string name, std::string accountNumber, double balance) {Account account(name, accountNumber, balance);accounts.push_back(account);std::cout << "Account created successfully." << std::endl;}

createAccount 方法用于创建一个新的账户。它接受三个参数:姓名、账号和初始余额。方法内部,我们创建一个新的 Account 对象,并将其添加到 accounts 列表中。然后,我们输出成功创建账户的消息。

    void deposit(std::string accountNumber, double amount) {Account* account = findAccount(accountNumber);if (account != nullptr) {account->deposit(amount);} else {std::cout << "Account not found." << std::endl;}}

deposit 方法用于给指定账户存款。它接受两个参数:账户号码和存款金额。方法内部,我们通过调用 findAccount 方法来查找要操作的账户。如果找到了账户,则调用该账户的 deposit 方法执行存款操作。如果未找到账户,则输出错误信息。

    void withdraw(std::string accountNumber, double amount) {Account* account = findAccount(accountNumber);if (account != nullptr) {account->withdraw(amount);} else {std::cout << "Account not found." << std::endl;}}

withdraw 方法用于给指定账户取款。它接受两个参数:账户号码和取款金额。方法内部,我们通过调用 findAccount 方法来查找要操作的账户。如果找到了账户,则调用该账户的 withdraw 方法执行取款操作。如果未找到账户,则输出错误信息。

    void displayAllAccounts() {for (const auto& account : accounts) {account.displayInfo();std::cout << "---------------------------" << std::endl;}}

displayAllAccounts 方法用于展示银行中所有账户的信息。它遍历 accounts 列表,并对每个账户调用 displayInfo 方法来输出信息。在每个账户信息输出之后,我们打印分隔线以区分不同的账户。

int main() {Bank bank;int choice;do {std::cout << "1. Create Account" << std::endl;std::cout << "2. Deposit" << std::endl;std::cout << "3. Withdraw" << std::endl;std::cout << "4. Display All Accounts" << std::endl;std::cout << "5. Exit" << std::endl;std::cout << "Enter your choice: ";std::cin >> choice;if (choice == 1) {std::string name, accountNumber;double balance;std::cout << "Enter name: ";std::cin >> name;std::cout << "Enter account number: ";std::cin >> accountNumber;std::cout << "Enter initial balance: ";std::cin >> balance;bank.createAccount(name, accountNumber, balance);} else if (choice == 2) {std::string accountNumber;double amount;std::cout << "Enter account number: ";std::cin >> accountNumber;std::cout << "Enter amount to deposit: ";std::cin >> amount;bank.deposit(accountNumber, amount);} else if (choice == 3) {std::string accountNumber;double amount;std::cout << "Enter account number: ";std::cin >> accountNumber;std::cout << "Enter amount to withdraw: ";std::cin >> amount;bank.withdraw(accountNumber, amount);} else if (choice == 4) {bank.displayAllAccounts();}} while (choice != 5);return 0;
}

main 函数中,我们使用一个循环来模拟用户与银行系统的交互。我们显示一个简单的菜单,让用户选择不同的操作。根据用户的选择,我们调用 Bank 类的相应方法来执行操作。当用户选择退出时,循环结束,程序终止。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/329075.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

浅析观察者模式在Java中的应用

观察者模式&#xff08;Observer Design Pattern&#xff09;,也叫做发布订阅模式&#xff08;Publish-Subscribe Design Pattern&#xff09;、模型-视图&#xff08;Model-View&#xff09;模式、源-监听器&#xff08;Source-Listener&#xff09;模式、从属者&#xff08;D…

Docker部署 SRS rtmp/flv流媒体服务器

一、介绍 SRS&#xff08;Simple Realtime Server&#xff09;是一款开源的流媒体服务器&#xff0c;具有高性能、高可靠性、高灵活性的特点&#xff0c;能够支持直播、点播、转码等多种流媒体应用场景。SRS 不仅提供了流媒体服务器&#xff0c;还提供了适用于多种平台的客户端…

Springboot进行多环境配置的2种方式

本文来说下Springboot使用Spring Profile和Maven Profile进行多环境配置 文章目录 概述Spring Profile多环境主配置文件与不同环境的配置文件 Maven ProfileProfile配置资源过滤 Spring Profile与Maven Profile具体使用 概述 原因 在实际的项目上&#xff0c;一般会分三种环境d…

端云协同,Akamai 与快手联合落地 QUIC 提升海外用户视频体验

10月10日&#xff0c;负责支持和保护数字化体验且深受全球企业信赖的解决方案提供商阿卡迈技术公司( Akamai Technologies, Inc.&#xff0c;以下简称&#xff1a;Akamai )( NASDAQ&#xff1a;AKAM )携手全球领先的短视频记录和分享平台快手(HK&#xff1a;1024)通过全面落地 …

静态网页设计——环保网(HTML+CSS+JavaScript)(dw、sublime Text、webstorm、HBuilder X)

前言 声明&#xff1a;该文章只是做技术分享&#xff0c;若侵权请联系我删除。&#xff01;&#xff01; 感谢大佬的视频&#xff1a; https://www.bilibili.com/video/BV1BC4y1v7ZY/?vd_source5f425e0074a7f92921f53ab87712357b 使用技术&#xff1a;HTMLCSSJS&#xff08;…

我与nano实验室交流群

感兴趣的同学、朋友可以加入群聊共同学习聊天哦。 主要是工训赛、电赛、光电、集成电路等等&#xff0c;会分享一些开源代码&#xff0c;博主自己做的项目&#xff0c;自己画的PCB等等&#xff0c;包含但不限于STM32、K210、V831、机器视觉&#xff0c;机械臂&#xff0c;ROS&a…

Python爬虫获取百度的图片

一. 爬虫的方式&#xff1a; 主要有2种方式: ①ScrapyXpath (API 静态 爬取-直接post get) ②seleniumXpath (点击 动态 爬取-模拟) ScrapyXpath XPath 是 Scrapy 中常用的一种解析器&#xff0c;可以帮助爬虫定位和提取 HTML 或 XML 文档中的数据。 Scrapy 中使用 …

09.简单工厂模式与工厂方法模式

道生一&#xff0c;一生二&#xff0c;二生三&#xff0c;三生万物。——《道德经》 最近小米新车亮相的消息可以说引起了不小的轰动&#xff0c;我们在感慨SU7充满土豪气息的保时捷设计的同时&#xff0c;也深深的被本土品牌的野心和干劲所鼓舞。 今天我们就接着这个背景&…

广义零样本学习综述的笔记

1 Title A Review of Generalized Zero-Shot Learning Methods&#xff08;Farhad Pourpanah; Moloud Abdar; Yuxuan Luo; Xinlei Zhou; Ran Wang; Chee Peng Lim&#xff09;【IEEE Transactions on Pattern Analysis and Machine Intelligence 2022】 2 conclusion Generali…

STM32F407ZGT6时钟源配置

1、26M外部时钟源 1、25M外部时钟源

Open3D 读写并显示PLY文件 (2)

Open3D 读写并显示PLY文件 &#xff08;2&#xff09; 一、算法介绍二、算法实现1.代码2.注意 一、算法介绍 读取PLY文件中的点云坐标&#xff0c;写出到新的文件中&#xff0c;并显示在屏幕上。 二、算法实现 1.代码 import open3d as o3dprint("读取点云") pl…

本地部署 gemini-openai-proxy,使用 Google Gemini 实现 Openai API

本地部署 gemini-openai-proxy&#xff0c;使用Google Gemini 实现 Openai API 0. 背景1. 申请 Google Gemini API key2. (Optional)Google Gemini 模型说明3. gemini-openai-proxy Github 地址4. 本地部署 gemini-openai-proxy5. 测试 0. 背景 使用 Google Gemini 实现 Opena…