概述
class Complex
C语言&C++语言
- C语言
Data(global)+Functions=>create_variables
全局变量+全局方法构建全局变量实现功能,没有对象概念,功能复杂情况下变量和方法区分不开 - C++语言
class,struct(Data_Members+Functions_Member)=>create_objects
变量和方法封装在class/struct构建对象,对象使用自己的变量和方法来实现功能
Notes
- Class without pointer
- 头文件防卫式声明,防止重复包含
- Complex类实部和虚部类型不确定,设置为模板
- inline(内联函数) 定义在类本体内部,是否是内联函数由编译器决定
- access level 成员函数和成员变量设置不同访问级别
- 构造函数
- 构造对象时自动调用
- 没有返回值
- 初始参数可以设置默认值
- 初始化阶段利用初始化列表初始化
- Private中不允许构造 单例模式
- 函数重载
- 编译时编译器会对函数名重新编码,其实不同
- 函数名相同,语义上不允许相同
- 操作符重载
- 成员函数隐含了一个this指针
- 全局函数,无this指针
- 常量成员函数,不会改变成员变量,const对象调用非const成员函数出现不兼容的类型限定符
- 传值和传引用
- 参数传递尽量传引用相当于传指针(底层为指针)
- 传递者无需知道接收者是以引用形式接收
- 参数返回可以的情况下,尽量传引用相当于传指针(底层为指针)
- 返回值为传入参数
- 临时变量不能传引用
- 有元函数
- 自由取得friend的private成员
- 相同class的各个Object互为有元
代码示例
https://gitee.com/NiMiKiss/learning-notes.git
#ifndef _CLASS_COMPLEX_
#define _CLASS_COMPLEX_
#include <iostream>
#include <math.h>
class Complex;
Complex&
__doapl(Complex* ths, const Complex& r);
Complex&
__doami(Complex* ths, const Complex& r);
Complex&
__doaml(Complex* ths, const Complex& r);
Complex&
__doapi(Complex* ths, const Complex& r);
class Complex
{
public:
Complex(double r = 0, double i = 0) : re(r), im(i) { }
Complex& operator += (const Complex&);
Complex& operator -= (const Complex&);
Complex& operator *= (const Complex&);
Complex& operator /= (const Complex&);
double real() const { return re; }
double imag() const { return im; }
private:
double re, im;
friend Complex& __doapl(Complex*, const Complex&);
friend Complex& __doami(Complex*, const Complex&);
friend Complex& __doaml(Complex*, const Complex&);
friend Complex& __doapi(Complex*, const Complex&);
};
inline Complex&
__doapl(Complex* ths, const Complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline Complex&
Complex::operator += (const Complex& r)
{
return __doapl(this, r);
}
inline Complex&
__doami(Complex* ths, const Complex& r)
{
ths->re -= r.re;
ths->im -= r.im;
return *ths;
}
inline Complex&
Complex::operator -= (const Complex& r)
{
return __doami(this, r);
}
inline Complex&
__doaml(Complex* ths, const Complex& r)
{
ths->re = ths->re * r.re - ths->im * r.im;
ths->im = ths->im * r.re + ths->re * r.im;
return *ths;
}
inline Complex&
Complex::operator *= (const Complex& r)
{
return __doaml(this, r);
}
inline Complex&
__doapi(Complex* ths, const Complex& r)
{
ths->re = (ths->re * r.re + ths->im * r.im) / (pow(r.re, 2) + pow(r.im, 2));
ths->im = (ths->im * r.re - ths->re * r.im) / (pow(r.re, 2) + pow(r.im, 2));
return *ths;
}
inline Complex&
Complex::operator /= (const Complex& r)
{
return __doapi(this, r);
}
inline double
imag(const Complex& x)
{
return x.imag();
}
inline double
real(const Complex& x)
{
return x.real();
}
inline Complex
operator + (const Complex& x, const Complex& y)
{
return Complex(real(x) + real(y), imag(x) + imag(y));
}
inline Complex
operator + (const Complex& x, double y)
{
return Complex(real(x) + y, imag(x));
}
inline Complex
operator + (double x, const Complex& y)
{
return Complex(x + real(y), imag(y));
}
inline Complex
operator - (const Complex& x, const Complex& y)
{
return Complex(real(x) - real(y), imag(x) - imag(y));
}
inline Complex
operator - (const Complex& x, double y)
{
return Complex(real(x) - y, imag(x));
}
inline Complex
operator - (double x, const Complex& y)
{
return Complex(x - real(y), -imag(y));
}
inline Complex
operator * (const Complex& x, const Complex& y)
{
return Complex(real(x) * real(y) - imag(x) * imag(y),
imag(x) * real(y) + real(x) * imag(y));
}
inline Complex
operator * (const Complex& x, double y)
{
return Complex(real(x) * y, imag(x) * y);
}
inline Complex
operator * (double x, const Complex& y)
{
return Complex(x * real(y), x * imag(y));
}
inline Complex
operator / (const Complex& x, double y)
{
return Complex(real(x) / y, imag(x) / y);
}
inline Complex
operator / (const Complex& x, const Complex& y)
{
return Complex((real(x) * real(y) + imag(x) * imag(y)) / (pow(real(y), 2) + pow(imag(y), 2)),
(imag(x) * real(y) - real(x) * imag(y)) / (pow(real(y), 2) + pow(imag(y), 2)));
}
inline Complex
operator + (const Complex& x)
{
return x;
}
inline Complex
operator - (const Complex& x)
{
return Complex(-real(x), -imag(x));
}
inline bool
operator == (const Complex& x, const Complex& y)
{
return real(x) == real(y) && imag(x) == imag(y);
}
inline bool
operator == (const Complex& x, double y)
{
return real(x) == y && imag(x) == 0;
}
inline bool
operator == (double x, const Complex& y)
{
return x == real(y) && imag(y) == 0;
}
inline bool
operator != (const Complex& x, const Complex& y)
{
return real(x) != real(y) || imag(x) != imag(y);
}
inline bool
operator != (const Complex& x, double y)
{
return real(x) != y || imag(x) != 0;
}
inline bool
operator != (double x, const Complex& y)
{
return x != real(y) || imag(y) != 0;
}
inline Complex
polar(double r, double t)
{
return Complex(r * cos(t), r * sin(t));
}
inline Complex
conj(const Complex& x)
{
return Complex(real(x), -imag(x));
}
inline double
norm(const Complex& x)
{
return real(x) * real(x) + imag(x) * imag(x);
}
std::ostream&
operator << (std::ostream& os, const Complex& x)
{
return os << '(' << real(x) << ',' << imag(x) << ')';
}
#endif // !_CLASS_COMPLEX_
客户端调用
#include "Class_Complex.h"
#include <iostream>
int main()
{
Complex c1(2, 1);
Complex c2(4, 0);
std::cout << c1 << std::endl;
std::cout << c2 << std::endl;
std::cout << c1 + c2 << std::endl;
std::cout << c1 - c2 << std::endl;
std::cout << c1 * c2 << std::endl;
std::cout << c1 / c2 << std::endl;
std::cout << conj(c1) << std::endl;
std::cout << norm(c1) << std::endl;
std::cout << polar(10, 4) << std::endl;
std::cout << (c1 == c2) << std::endl;
std::cout << (c1 != c2) << std::endl;
std::cout << +c2 << std::endl;
std::cout << -c2 << std::endl;
std::cout << (c2 - 2) << std::endl;
std::cout << (5 + c2) << std::endl;
return EXIT_SUCCESS;
}
最后
以上就是疯狂人生为你收集整理的c++ 学习笔记--class Complex(01)class Complex的全部内容,希望文章能够帮你解决c++ 学习笔记--class Complex(01)class Complex所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复