C++面向对象高级开发

极客班GeekBand – C++面向对象高级开发 – 2讲师 – 侯捷复习Complex类的实现过程

Complex.h

;class complex {private:double re, im;public:complex(double r = 0, double i = 0) :re(r), im(i) {}complex& operator += (const complex&);double real() const {return re;}double imag() const {return im;}&);};& r) {return __doapl(this, r);}& r) {ths->re += r.re;ths->im += r.im;return *ths;}+(& y) {return complex(x.real() + y.real(), x.imag() + y.imag());}+(const complex& x, double y) {return complex(x.real() + y, x.imag() + y);}+(double x, complex& y) {return complex(x + y.real(), y.imag());}ostream& operator <<(ostream& os, const complex& x) {os << “(” << x.real() << “,” << x.imag() << “)”;return os;}#endif

Complex-test.cpp

#include “Complex.h”int main() {complex c1(3, 6);c1 += 5;cout << c1 << endl;complex c2;c2 = 5 + c1;cout << c2 << endl;cout << c1 << c2 << endl;return 0;}三大函数:拷贝构造,拷贝复制,析构

String.h

#include “String.h”int main() {String s1;String s2(“hello”);String s3(s1); //拷贝构造函数cout << s3 << endl;s3 = s2; //拷贝赋值函数cout << s3 << endl;return 0;}

String-test.h

;#include <string.h>class String{public:String(const char* cstr);//class with a pointer membersString(const String& str);String& operator=(const String& str);~String();char* get_c_str() const {return m_data;}private:char* m_data;};inline String::String(const char* cstr = 0) {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(const String& str) {m_data = new char[strlen(str.m_data) + 1];strcpy(m_data, str.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() {delete[] m_data;}ostream& operator<<(ostream& os, const String& str) {os << str.get_c_str();return os;}#endif栈、堆与内存管理Stack VS. Heap

Stack,是存在于某作用域(scope)的一块内存空间(memory space)。例如当你调用函数,函数本身即会形成一个stack用来放置它所接收的参数,以及返回地址。 在函数本体(Function Body)内声明的任何变量,其所使用的内存块都取自上诉stack。 Heap,或称为system heap,是指由操作系统提供的一块global内存空间,程序可动态分配(dynamic allocated)从某中获得若干区块(blocks)。

class Complex {};{Complex c1(1, 2); //c1所占用的空间来自stackComplex* p = new Complex(3); //Complex(3)是个临时变量,所占用的空间是以new动态分配而得,并由p指向。占用空间来自heap。}stack objects、static local objects、global objects的生命期class Complex {};Complex c3(1, 2);{Complex c1(1, 2);static Complex c2(1, 2);}int main() {}heap objects的生命周期class Complex {};{Complex* p = new Complex;delete p;}p所指的便是heap object,,其生命在它被delete之际结束。class Complex {};{Complex* p = new Complex;}以上出现内存泄露(memory leak),因为当作用域结束,p所指的heap object仍然存在,但指针p的生命却结束了,作用域之外再也看不见p(也就没有机会delete p)new:先分配memory,再调用ctorComplex* pc = new Complex(1, 2);只有他的好身体,没有地方可去,只想到处流浪、人生就像一场旅行,

C++面向对象高级开发

相关文章:

你感兴趣的文章:

标签云: