设置friend 就可以直接获取 和public一样, 但是不写friend 获取就只能通过函数,效率会下降.
complex.h
#ifndef __MYCOMPLEX__
#define __MYCOMPLEX__
class complex;
complex&
__doapl (complex* ths, const complex& r);
complex&
__doami (complex* ths, const complex& r);
complex&
__doaml (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&);
};
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)
{
double f = ths->re * r.re - ths->im * r.im;
ths->im = ths->re * r.im + ths->im * r.re;
ths->re = f;
return *ths;
}
inline complex&
complex::operator *= (const complex& r)
{
return __doaml (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) // 正值
{
return x;
}
inline complex
operator - (const complex& x) // 负值
{
return complex (-real (x), -imag (x));
}
inline complex
operator * (const complex& x, const complex& y)
{
return complex (real (x) * real (y) - imag (x) * imag (y),
real (x) * imag (y) + imag (x) * real (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));
}
complex
operator / (const complex& x, double y)
{
return complex (real (x) / y, imag (x) / y);
}
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;
}
#include
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);
}
#endif //__MYCOMPLEX__
在 C++ 中,浅拷贝和深拷贝是两种不同的复制对象的方法。浅拷贝是通过简单地复制原始对象的所有变量的数据来创建一个对象。这种方法适用于对象的变量没有在堆内存区域定义的情况。如果某些变量是从堆内存区域动态分配的内存,则复制的对象变量也将引用相同的内存位置。这将导致歧义和运行时错误,悬空指针。由于两个对象都将引用相同的内存位置,因此一个对象所做的更改也会反映在另一个对象上。由于我们希望创建一个对象的副本,因此浅拷贝无法实现这一目的。
深拷贝是通过复制所有变量的数据并为对象分配相同值的类似内存资源来创建一个对象。为了执行深拷贝,我们需要显式地定义复制构造函数并根据需要分配动态内存。此外,还需要在其他构造函数中为变量动态分配内存 。
inline
String::String(const String& str) // 拷贝本身 收到的参数就是它本身
{
m_data = new char[ strlen(str.m_data) + 1 ];
strcpy(m_data, str.m_data);
}
Stack
是存在于某作用域 (scope) 的一块内存空间 (memory space)。例如当你调用函数,函数本身即会形成一个 stack 用来放置它所接收的参数,以及返回地址。在函数本体 (function body) 内声明的任何变量,其所使用的内存块都取自上述 stack。Heap
,或谓 system heap,是指由操作系统提供的一块 global 内存空间,程序可动态分配 (dynamic allocated)
从某中获得若干区块 (blocks)。希望这些信息对您有所帮助!如果您有其他问题,请随时告诉我。new
它, 但是你就必须要有delete
它的职责。new
& delete
操作new
和 delete
都会调用 C语言
中的 malloc
和 free
函数#ifndef __MYSTRING__
#define __MYSTRING__
class String
{
public:
// 如果成员函数为指针 , 必须重写拷贝构造 和 拷贝复制 ,
// 不然系统会 一个字节一个字节复制
String(const char* cstr=0); //拷贝构造
String(const String& str); //拷贝构造 拷贝本身()
String& operator=(const String& str); // 拷贝复制
~String();
char* get_c_str() const { return m_data; } // 获取c风格的 str
private:
char* m_data;
};
#include
inline
String::String(const char* cstr)
{
if (cstr) {
m_data = new char[strlen(cstr)+1];
strcpy(m_data, cstr);
}
else {
m_data = new char[1];
*m_data = '\0';
}
}
inline
String::~String()
{
delete[] m_data;
}
inline
String& String::operator=(const String& str) //
{
if (this == &str) // 检测自我赋值
return *this;
delete[] m_data;
m_data = new char[ strlen(str.m_data) + 1 ];
strcpy(m_data, str.m_data);
return *this;
}
inline
String::String(const String& str) // 拷贝本身
{
m_data = new char[ strlen(str.m_data) + 1 ];
strcpy(m_data, str.m_data);
}
#include
using namespace std;
ostream& operator<<(ostream& os, const String& str)
{
os << str.get_c_str();
return os;
}
#endif
cout
和 模板如何设计几个窗口, 内容是共享的, 一个改变 另外的类也会改变 ?