赞
踩
目录
- #include <iostream>
-
- class Person
- {
- public:
- //友元函数
- friend void FriendAsk(Person& ref);
-
- public:
- Person(int age, int money) : m_Aage(age), m_Money(money){}
- int GetInfo()
- {
- return this->m_Money;
- }
-
- public:
- int m_Aage;
-
- private:
- int m_Money;
- };
-
- void FriendAsk(Person& ref)
- {
- std::cout << ref.m_Money << std::endl;
- }
-
- int main()
- {
- Person p1(18, 100);
- std::cout << p1.GetInfo() << std::endl;
- FriendAsk(p1);
-
- return 0;
- }
- #include <iostream>
-
- class MyClass
- {
- private:
- int value;
-
- public:
- MyClass(int v) : value(v) {}
-
- friend class FriendClass;
- };
-
- class FriendClass {
- public:
- void displayValue(const MyClass& obj) {
- std::cout << "The value is: " << obj.value << std::endl;
- }
- };
-
- int main() {
- MyClass obj(42);
- FriendClass fc;
- fc.displayValue(obj); // 使用友元类的成员函数访问私有成员
-
- return 0;
- }
友元函数的访问权限:
友元函数的声明和定义:
友元函数和封装:
友元函数的应用:
算术运算符:+、-、*、/、%等
- #include <iostream>
-
- class Calc
- {
- public:
-
- //友元函数
- friend Calc operator-(const Calc& ref1, const Calc& ref2);
-
- //构造函数
- Calc(int nNum):n_Num(nNum){}
-
- Calc operator+(const Calc& ref)
- {
- return Calc(this->n_Num + ref.n_Num);
- }
-
-
- private:
- int n_Num;
- };
-
- Calc operator-(const Calc& ref1, const Calc& ref2)
- {
- return Calc(ref1.n_Num - ref2.n_Num);
- }
-
- int main()
- {
-
- Calc c1(10);
- Calc c2(5);
- Calc c3 = c1 + c2;
- Calc c4 = c1 - c2;
-
- return 0;
- }
==、!=、>、<、>=、<=等
- #include <iostream>
-
- class Calc
- {
- public:
-
- //友元函数
- friend Calc operator-(const Calc& ref1, const Calc& ref2);
-
- //构造函数
- Calc(int nNum):n_Num(nNum){}
-
- //算数运算
- Calc operator+(const Calc& ref)
- {
- return Calc(this->n_Num + ref.n_Num);
- }
-
- Calc operator-(const Calc& ref)
- {
- return Calc(this->n_Num - ref.n_Num);
- }
-
- Calc operator*(const Calc& ref)
- {
- return Calc(this->n_Num * ref.n_Num);
- }
-
- Calc operator/(const Calc& ref)
- {
- return Calc(this->n_Num / ref.n_Num);
- }
-
- Calc operator%(const Calc& ref)
- {
- return Calc(this->n_Num % ref.n_Num);
- }
-
- //比较运算
- bool operator==(const Calc& ref)
- {
- return this->n_Num == ref.n_Num;
- }
-
- bool operator!=(const Calc& ref)
- {
- return this->n_Num != ref.n_Num;
- }
-
- bool operator>(const Calc& ref)
- {
- return this->n_Num > ref.n_Num;
- }
-
- bool operator<(const Calc& ref)
- {
- return this->n_Num < ref.n_Num;
- }
-
- bool operator>=(const Calc& ref)
- {
- return this->n_Num >= ref.n_Num;
- }
-
- bool operator<=(const Calc& ref)
- {
- return this->n_Num <= ref.n_Num;
- }
-
-
-
- private:
- int n_Num;
- };
-
-
- int main()
- {
- Calc c1(10);
- Calc c2(5);
-
- //算数运算
- Calc c3 = c1 + c2;
-
- //笔记运算
- bool bret = c1 > c2;
-
- return 0;
- }
!、&&、||等
- #include <iostream>
-
- class Calc
- {
- public:
- //构造函数
- Calc(int nNum) :n_Num(nNum) {}
-
- bool operator!()
- {
- return !this->n_Num;
- }
-
- bool operator||(const Calc& ref)
- {
- return this->n_Num || ref.n_Num;
- }
-
- bool operator&&(const Calc& ref)
- {
- return this->n_Num && ref.n_Num;
- }
-
- private:
- int n_Num;
- };
-
-
- int main()
- {
- Calc c1(10);
- Calc c2(5);
-
- // ||
- // &&
- // !
- std::cout << !c1 << std::endl;
- std::cout << (c1 || c2) << std::endl;
- std::cout << (c1 && c2) << std::endl;
-
-
- return 0;
- }
=、+=、-=、*=、/=等
- #include <iostream>
-
- class Number
- {
- private:
- int value;
-
- public:
- Number(int val) : value(val) {}
-
- // 赋值运算符重载:=
- Number& operator=(const Number& other)
- {
- if (this == &other)
- {
- return *this;
- }
- value = other.value;
- return *this;
- }
-
- // 复合赋值运算符重载:+=
- Number& operator+=(const Number& other)
- {
- value += other.value;
- return *this;
- }
-
- // 复合赋值运算符重载:-=
- Number& operator-=(const Number& other)
- {
- value -= other.value;
- return *this;
- }
-
- // 复合赋值运算符重载:*=
- Number& operator*=(const Number& other)
- {
- value *= other.value;
- return *this;
- }
-
- // 复合赋值运算符重载:/=
- Number& operator/=(const Number& other)
- {
- if (other.value == 0)
- {
- throw std::runtime_error("Divide by zero error");
- }
- value /= other.value;
- return *this;
- }
-
- void display() const
- {
- std::cout << "Value: " << value << std::endl;
- }
- };
-
- int main() {
- Number num1(10);
- Number num2(0);
- try
- {
- num1 += num2;
- num1.display();
-
- num1 -= num2;
- num1.display();
-
- num1 *= num2;
- num1.display();
-
- num1 /= num2;
- num1.display();
- }
- catch (const std::exception& e)
- {
- std::cout << e.what() << std::endl;
- }
-
-
- return 0;
- }
++、--等
- #include <iostream>
-
- class Counter
- {
- private:
- int count;
-
- public:
- Counter(int c = 0) : count(c) {}
-
- // 前置自增运算符重载:++
- Counter& operator++()
- {
- count++;
- return *this;
- }
-
- // 后置自增运算符重载:++
- Counter operator++(int)
- {
- Counter temp = *this;
- ++(*this);
- return temp;
- }
-
- // 前置自减运算符重载:--
- Counter& operator--()
- {
- count--;
- return *this;
- }
-
- // 后置自减运算符重载:--
- Counter operator--(int)
- {
- Counter temp = *this;
- --(*this);
- return temp;
- }
-
- void display() const
- {
- std::cout << "Count: " << count << std::endl;
- }
- };
-
- int main()
- {
- Counter c(5);
-
- ++c;
- c.display();
-
- c--;
- c.display();
-
- return 0;
- }
[]
- #include <iostream>
-
- class StudentInfo
- {
- public:
- StudentInfo(int nCount): m_Data(new int[nCount]), m_Count(nCount){}
-
- int& operator[](int nIndex)
- {
- return m_Data[nIndex];
- }
-
- void display() const
- {
- for (size_t i = 0; i < m_Count; i++)
- {
- std::cout << "m_Data[" << i <<"] -> " << m_Data[i] << std::endl;
- }
- }
-
- private:
- int m_Count;
- int* m_Data;
- };
-
-
-
- int main()
- {
- StudentInfo Info(5);
- Info[2] = 3;
-
- Info.display();
-
-
- return 0;
- }
重载输入运算符
- istream& operator>>(istream& input, YourType& object)
- {
- // 从输入流中读取数据,存储到 object 中
- // 对输入进行验证和处理
- // 返回输入流对象
- return input;
- }
重载输出运算符
- ostream& operator<<(ostream& output, const YourType& object)
- {
- // 将 object 的数据输出到输出流中
- // 格式化输出
- // 返回输出流对象
- return output;
- }
示例代码
- #include <iostream>
-
- class Student
- {
- public:
- friend std::ostream& operator<<(std::ostream& output, const Student& object);
- friend std::istream& operator>>(std::istream& intput, Student& object);
-
- private:
- int m_Age;
- int m_Id;
- };
-
- std::ostream& operator<<(std::ostream& output, const Student& object)
- {
- // 将 object 的数据输出到输出流中
- output << object.m_Age << "|" << object.m_Id;
-
- // 返回输出流对象
- return output;
- }
-
- std::istream& operator>>(std::istream& intput, Student& object)
- {
- // 将 object 的数据输出到输出流中
- intput >> object.m_Age >> object.m_Id;
-
- // 返回输出流对象
- return intput;
- }
-
-
- int main()
- {
- Student s1;
- std::cin >> s1;
- std::cout << s1 << std::endl;
-
- return 0;
- }
访问权限:
左操作数和右操作数:
参数传递方式:
对称性:
全局函数方式的运算符重载是将运算符重载函数定义为全局函数,而不是类的成员函数。
全局函数方式的运算符重载函数可以在函数定义中访问类的私有成员,通过将类声明为友元来实现。
- #include <iostream>
-
- class Complex {
- private:
- double real;
- double imag;
-
- public:
- Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
-
- // 声明全局函数为友元
- friend Complex operator+(const Complex& c1, const Complex& c2);
-
- void display() const {
- std::cout << real << " + " << imag << "i" << std::endl;
- }
- };
-
- // 全局函数方式的运算符重载
- Complex operator+(const Complex& c1, const Complex& c2) {
- return Complex(c1.real + c2.real, c1.imag + c2.imag);
- }
-
- int main() {
- Complex c1(2.0, 3.0);
- Complex c2(4.0, 5.0);
-
- Complex sum = c1 + c2;
-
- sum.display(); // 输出:6 + 8i
-
- return 0;
- }
成员函数方式的运算符重载是将运算符重载函数定义为类的成员函数。
运算符重载函数作为成员函数时,隐含地访问了类的成员变量和成员函数。
- #include <iostream>
-
- class Complex {
- private:
- double real;
- double imag;
-
- 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);
- }
-
- void display() const {
- std::cout << real << " + " << imag << "i" << std::endl;
- }
- };
-
- int main() {
- Complex c1(2.0, 3.0);
- Complex c2(4.0, 5.0);
-
- Complex sum = c1 + c2;
-
- sum.display(); // 输出:6 + 8i
-
- return 0;
- }
- 返回类型 operator 运算符(参数列表)
- {
- // 实现运算符的操作
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。