<file_sep>file(REMOVE_RECURSE
  "CMakeFiles/conversion_function.dir/main.cpp.o"
  "conversion_function.pdb"
  "conversion_function"
)

# Per-language clean rules from dependency scanning.
foreach(lang CXX)
  include(CMakeFiles/conversion_function.dir/cmake_clean_${lang}.cmake OPTIONAL)
endforeach()
<file_sep>cmake_minimum_required(VERSION 3.13)
project(Observer)

set(CMAKE_CXX_STANDARD 14)

add_executable(Observer main.cpp Subject.h Subject.cpp Observer.h Observer.cpp Observer1.h Observer1.cpp)<file_sep>cmake_minimum_required(VERSION 3.13)
project(Complex)

set(CMAKE_CXX_STANDARD 14)

add_executable(Complex main.cpp complex.cpp complex.h my_complex.h my_complex.cpp)<file_sep>#ifndef OBSERVER_SUBJECT_H
#define OBSERVER_SUBJECT_H

#include <vector>
#include <iostream>
#include "Observer.h"
using namespace std;

class Subject{
public:
    Subject(int val = 0) {
        value = val;
        observers.reserve(10);
    }
    void addObserver(Observer& observer) {
        observers.push_back(&observer);
    }
    void setValue(int val) {
        value = val;
        notify();
    }
    void notify() {
        for(size_t i = 0; i < observers.size(); i++)
            observers[i]->update(value);
    }
private:
    int value;
    vector<Observer*> observers;
};

#endif
<file_sep>cmake_minimum_required(VERSION 3.13)
project(template_pattern)

set(CMAKE_CXX_STANDARD 14)

add_executable(template_pattern main.cpp library.h library.cpp application.h application.cpp)<file_sep>#ifndef OBSERVER_OBSERVER1_H
#define OBSERVER_OBSERVER1_H

#include <iostream>
#include "Observer.h"
#include "Subject.h"
using namespace std;

class Subject;
class Observer1:public Observer{
public:
    Observer1(int id = 0) {
        m_id = id;
    }
    Observer1(Subject& s, int id = 0) {
        s.addObserver(*this);
        m_id = id;
    }
    virtual void update(int val) {
        cout << "Observer1id:" << m_id << " value:" << val << endl;
    }
private:
    int m_id;
};

#endif
<file_sep>#include "my_complex.h"

inline
my_complex& __doaplus(my_complex* ths, const my_complex& c){
    ths->re += c.re;
    ths->im += c.im;
    return *ths;
}

inline
my_complex& my_complex::operator += (const my_complex &c){
    return __doaplus(this, c);
}

ostream& operator << (ostream& os, const my_complex c) {
    os << c.real() << "+" << c.image() << "i" << endl;
    return os;
}<file_sep>cmake_minimum_required(VERSION 3.14)
project(conversion_function)

set(CMAKE_CXX_STANDARD 14)

add_executable(conversion_function main.cpp Fraction.h)<file_sep>#include "complex.h"

// 类定义

inline complex& complex::operator+=(const complex & r) {
    // 所有的成员函数都包含一个隐藏的this函数
    return __doap1(this, r);
}

// 第一个传入参数被修改了，所以不用const修饰，第二个参数不进行修改，利用const修饰，同时pass-by-reference
// 返回值为引用类型，因为返回的对象的空间本来就存在，所以可以pass-by-reference
// 如果是实现+而不是+=，则会在函数内部创建新的临时对象，此时必须pass-by-value
inline complex& __doap1(complex* ths, const complex &r)
{
    // 友元函数中可以自由取得friend的私有成员
    // 注意：相同类的各个object互为友元
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
}

// 此处返回不可以pass-by-reference
complex operator + (const complex& x, const complex& y)
{
    // 临时变量
    return complex(x.real() + y.real(), x.image() + y.real());
}<file_sep>#include <iostream>
#include "operator_new_delete.h"

int main() {
    cout << "----------------array new_delete----------" << endl;
    my_operator* m_new_delete = new my_operator[3];
    delete[] m_new_delete;

    cout << "----------------new_delete---------------" << endl;
    m_new_delete = new my_operator(2, 6);
    delete m_new_delete;
    return 0;
}<file_sep>cmake_minimum_required(VERSION 3.14)
project(operator_new)

set(CMAKE_CXX_STANDARD 14)

add_executable(operator_new main.cpp operator_new_delete.cpp operator_new_delete.h)<file_sep>
#ifndef CONVERSION_FUNCTION_FRACTION_H
#define CONVERSION_FUNCTION_FRACTION_H
class Fraction {
public:
    // explicit防止其他类隐式转换成本类
    explicit Fraction(int n, int d = 1)
    : m_numerator(n), m_denominator(d) { }

    // conversion function转换函数，将本类转换成其他类
    operator double() const {
        return (double)m_numerator/m_denominator;
    }

    Fraction operator + (const Fraction&f) {
        return Fraction(f.m_numerator * m_denominator + f.m_denominator * m_numerator,
                f.m_denominator * m_denominator);
    }

private:
    int m_numerator;
    int m_denominator;
};

#endif //CONVERSION_FUNCTION_FRACTION_H
<file_sep>//
// Created by bisheng on 19-4-23.
//
#include "library.h"

void Library::run()
{
    Step1();
    Step2();
    Step3();
    Step4();
    Step5();
}

void Library::Step1()
{
    std::cout << "Step1" << std::endl;
}

void Library::Step3()
{
    std::cout << "Step3" << std::endl;
}

void Library::Step5()
{
    std::cout << "Step5" << std::endl;
}


<file_sep>
#ifndef SINGLETON_SINGLETON_H
#define SINGLETON_SINGLETON_H

#include <iostream>

class Singleton;

class Singleton
{
public:
    static Singleton& getInstance();//static 关键字
    void function() {//function公有成员
        std::cout << "function" << std::endl;
    }
private:
    Singleton() {  }//构造函数私有成员
};

#endif //SINGLETON_SINGLETON_H
<file_sep># The set of languages for which implicit dependencies are needed:
set(CMAKE_DEPENDS_LANGUAGES
  "CXX"
  )
# The set of files for implicit dependencies of each language:
set(CMAKE_DEPENDS_CHECK_CXX
  "C:/Users/BiShe/Desktop/cpp/C++OOP/Design pattern/template_pattern/application.cpp" "C:/Users/BiShe/Desktop/cpp/C++OOP/Design pattern/template_pattern/cmake-build-debug/CMakeFiles/template_pattern.dir/application.cpp.obj"
  "C:/Users/BiShe/Desktop/cpp/C++OOP/Design pattern/template_pattern/library.cpp" "C:/Users/BiShe/Desktop/cpp/C++OOP/Design pattern/template_pattern/cmake-build-debug/CMakeFiles/template_pattern.dir/library.cpp.obj"
  "C:/Users/BiShe/Desktop/cpp/C++OOP/Design pattern/template_pattern/main.cpp" "C:/Users/BiShe/Desktop/cpp/C++OOP/Design pattern/template_pattern/cmake-build-debug/CMakeFiles/template_pattern.dir/main.cpp.obj"
  )
set(CMAKE_CXX_COMPILER_ID "GNU")

# The include file search paths:
set(CMAKE_CXX_TARGET_INCLUDE_PATH
  )

# Targets to which this target links.
set(CMAKE_TARGET_LINKED_INFO_FILES
  )

# Fortran module output directory.
set(CMAKE_Fortran_TARGET_MODULE_DIR "")
<file_sep>#include <iostream>
#include "var_template.h"
int main() {
    print(1, "hello", "world", 1);
    return 0;
}<file_sep>#include <iostream>
#include "mystring.h"
#include "String.h"
using namespace std;
int main() {
    return 0;
}<file_sep>#include <iostream>
#include "plus.h"
using namespace std;
int main() {
    myplus<int> p;
    auto result1 = p(5, 3);

    auto result2 = myplus<int>() (2, 6);

    cout << result1 << endl;
    cout << result2 << endl;
    return 0;
}<file_sep>//
// Created by bisheng on 19-4-23.
//

#ifndef TEMPLATE_PATTERN_LIBRARY_H
#define TEMPLATE_PATTERN_LIBRARY_H

#include <iostream>

class Library
{
public:
    Library(){}
    // 模板，即算法骨架（稳定）
    void run();
    virtual ~Library(){};

private:
    void Step1();
    void Step3();
    void Step5();

protected:
    // 算法的某些步骤 (变化)，延迟到子类中实现
    virtual void Step2() {};
    virtual void Step4() {};
};
#endif //TEMPLATE_PATTERN_LIBRARY_H
<file_sep>#ifndef __MYSTRING_H
#include <cstring>
#include <iostream>
using namespace std;
class String;
ostream& operator << (ostream& os, const String& str);
// 此类的特殊之处是成员含有指针
class String
{
public:
    // 含有指针的类必须自己定义拷贝构造，拷贝赋值，和析构函数 Big Three
    String(const char* cstr = 0);
    String(const String& str);
    String& operator=(const String& str);
    ~String();
    char* get_c_str() const { return m_data; }
private:
    char* m_data;
};

#define __MYSTRING_H
#endif
<file_sep>file(REMOVE_RECURSE
  "CMakeFiles/function_like_class.dir/main.cpp.o"
  "CMakeFiles/function_like_class.dir/plus.cpp.o"
  "function_like_class.pdb"
  "function_like_class"
)

# Per-language clean rules from dependency scanning.
foreach(lang CXX)
  include(CMakeFiles/function_like_class.dir/cmake_clean_${lang}.cmake OPTIONAL)
endforeach()
<file_sep>#include <iostream>
#include "library.h"
#include "application.h"

int main() {
    Application *a = new Application();
    // 子类可以调用父类函数
    a->run();
    delete a;
    return 0;
}<file_sep>#include "Observer1.h"
<file_sep>cmake_minimum_required(VERSION 3.14)
project(variadic_templates)

set(CMAKE_CXX_STANDARD 14)

add_executable(variadic_templates main.cpp var_template.cpp var_template.h)<file_sep>#ifndef __MY_COMPLEX_H
#define __MY_COMPLEX_H
#include <iostream>
using namespace std;
class my_complex;
ostream& operator << (ostream& os, const my_complex c);
class my_complex {
public:
    my_complex(double r = 0, double i = 0): re(r), im(i)
    { }
    double real() const{ return re; }
    double image() const{ return im; }
    my_complex& operator += (const my_complex&);
private:
    double re, im;
    friend my_complex& __doaplus(my_complex*, const my_complex&);
};
#endif<file_sep>#include "var_template.h"

void print(void)
{
    return;
}<file_sep>#include <iostream>
#include "my_complex.h"
#include "complex.h"
using namespace std;
int main() {
    my_complex c(1, 2);
    cout << c << endl;
    return 0;
}<file_sep>#include "Singleton2.h"

Singleton2 Singleton2::s;
Singleton2& Singleton2::getInstance() {
    return s;
}<file_sep>#ifndef FUNCTION_LIKE_CLASS_PLUS_H
#define FUNCTION_LIKE_CLASS_PLUS_H

template <class T>
class myplus {
public:
    T operator()(const T&x, const T&y) const{return x + y;}
};


#endif //FUNCTION_LIKE_CLASS_PLUS_H
<file_sep>#ifndef SINGLETON_SINGLETON2_H
#define SINGLETON_SINGLETON2_H

#include <iostream>
using namespace std;
class Singleton2;

class Singleton2
{
public:
    static Singleton2& getInstance();
    void function() { cout << "function has been called" << endl;}
private:
    Singleton2() { }
    static Singleton2 s;
};

#endif //SINGLETON_SINGLETON2_H

<file_sep>#ifndef POINTER_LIKE_CLASS_SHARE_PTR_H
#define POINTER_LIKE_CLASS_SHARE_PTR_H

template <class T>
class share_ptr {
public:
    share_ptr() { }
    share_ptr(T* p): ptr(p) { }
    T* operator = (T* p) {
        ptr = p;
        return ptr;
    }
    T operator*() {
        return *ptr;
    }
    T* operator->() {
        return ptr;
    }
private:
    T* ptr;
    long* value;
};


#endif
<file_sep>#include <iostream>
#include <vector>
#include "Subject.h"
#include "Observer.h"
#include "Observer1.h"
using namespace std;

int main() {
    Subject s;
    Observer1 o1;
    Observer1 o2(s, 1);
    s.addObserver(o1);
    s.setValue(5);
    return 0;
}<file_sep>#include "mystring.h"

mystring::mystring(const char *cstr) {
    if(!cstr) {
        m_data = new char[1];
        m_data[0] = '\0';
    }
    else {
        m_data = new char[strlen(cstr) + 1];
        strcpy(m_data, cstr);
    }
}

mystring::mystring(const mystring& str) {
    size_t length = strlen(str.m_data);
    m_data = new char[length + 1];
    strcpy(m_data, str.m_data);
}

mystring& mystring::operator=(const mystring& 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;
}

mystring::~mystring() {
    delete[] m_data;
}

ostream& operator << (ostream& os, const mystring& str){
    os << str.get_cstr();
    return os;
}<file_sep>#include "Singleton.h"
// static必须在.cpp成员中定义，不能在.h文件中定义
Singleton& Singleton::getInstance() {
    static Singleton s;//static关键字
    return s;
}<file_sep>//
// Created by bisheng on 19-4-23.
//

#ifndef TEMPLATE_PATTERN_APPLICATION_H
#define TEMPLATE_PATTERN_APPLICATION_H

#include "library.h"
#include <iostream>

class Application:public Library
{
public:
    Application(){}
    ~Application(){}
    virtual void Step2();
    virtual void Step4();
};

#endif //TEMPLATE_PATTERN_APPLICATION_H
<file_sep>cmake_minimum_required(VERSION 3.13)
project(String)

set(CMAKE_CXX_STANDARD 14)

add_executable(String main.cpp mystring.h mystring.cpp String.h String.cpp)<file_sep>#include <iostream>
#include "share_ptr.h"

using namespace std;
int main() {
    share_ptr<int> p;
    int* p1 = new int(6);
    p = p1;
    cout << *p << endl;
    return 0;
}<file_sep>#include "share_ptr.h"
<file_sep>#ifndef OPERATOR_NEW_OPERATOR_NEW_H
#define OPERATOR_NEW_OPERATOR_NEW_H

#include <cstdlib>
#include <iostream>

using namespace std;

class my_operator {
public:
    my_operator(double r = 0, double i = 0): re(r), im(i) { cout << "ctor" << endl;}

    ~my_operator(){ cout << "dtor" << endl; }

    void* operator new (size_t size){
        void* p = malloc(size);
        cout << "my operator new, size:" << size << endl;
        return p;
    }

    void* operator new[] (size_t size){
        void* p = malloc(size);
        cout << "my operator new, size:" << size << endl;
        return p;
    }

    void operator delete(void * p) {
        free(p);
        cout << "my operator delete" << endl;
    }

    void operator delete[](void * p) {
        free(p);
        cout << "my operator delete" << endl;
    }
private:
    double re, im;
};


#endif //OPERATOR_NEW_OPERATOR_NEW_H
<file_sep>file(REMOVE_RECURSE
  "CMakeFiles/template_pattern.dir/main.cpp.obj"
  "CMakeFiles/template_pattern.dir/library.cpp.obj"
  "CMakeFiles/template_pattern.dir/application.cpp.obj"
  "template_pattern.pdb"
  "template_pattern.exe"
  "template_pattern.exe.manifest"
  "libtemplate_pattern.dll.a"
)

# Per-language clean rules from dependency scanning.
foreach(lang CXX)
  include(CMakeFiles/template_pattern.dir/cmake_clean_${lang}.cmake OPTIONAL)
endforeach()
<file_sep>cmake_minimum_required(VERSION 3.13)
project(Singleton)

set(CMAKE_CXX_STANDARD 14)

add_executable(Singleton main.cpp Singleton.h Singleton.cpp Singleton2.h Singleton2.cpp)<file_sep>//
// Created by bisheng on 19-4-23.
//
#include "application.h"

void Application::Step2()
{
    std::cout << "Step2" << std::endl;
}

void Application::Step4()
{
    std::cout << "Step4" << std::endl;
}
<file_sep>#include <iostream>
#include "Singleton.h"
#include "Singleton2.h"

int main() {
    Singleton2::getInstance().function();
    return 0;
}<file_sep>#include "operator_new_delete.h"
<file_sep>#ifndef VARIADIC_TEMPLATES_VAR_TEMPLATE_H
#define VARIADIC_TEMPLATES_VAR_TEMPLATE_H
#include <iostream>
using namespace std;

void print(void);

template<typename T, typename... Types>
void print(const T& firstArg, const Types&... args)
{
    cout << firstArg << endl;
    if(sizeof...(args) > 0)
        print(args...);
    else return;
}


#endif //VARIADIC_TEMPLATES_VAR_TEMPLATE_H
<file_sep>cmake_minimum_required(VERSION 3.14)
project(function_like_class)

set(CMAKE_CXX_STANDARD 14)

add_executable(function_like_class main.cpp plus.cpp plus.h)<file_sep>// 防卫式声明
#ifndef __COMPLEX
#define __COMPLEX


// 前置声明
class complex;
complex operator + (const complex& x, const complex& y);

// 类声明
class complex{
public:
    // 构造函数初始化列表
    complex(double r = 0, double i = 0) : re(r), im(i)
    { }

    // 参数传递尽量使用reference,但是为了防止函数内部对传入参数的修改，利用const关键字进行修饰
    // 返回值也尽量使用reference，返回值什么时候可以使用reference，什么时候不可以
    complex& operator += (const complex&);

    // const函数
    double real() const { return re;}
    double image() const { return im;}

private:
    // 数据放在private中
    double re, im;

    // 友元函数
    friend complex& __doap1(complex* ths, const complex& c);
};

#endif
<file_sep>#ifndef OBSERVER_OBSERVER_H
#define OBSERVER_OBSERVER_H

class Observer {
public:
    virtual void update(int val) = 0;
    virtual ~Observer() {}
};

#endif
<file_sep>#ifndef __MY_STRING_H
#define __MY_STRING_H

#include <cstring>
#include <iostream>
using namespace std;

class mystring;
ostream& operator << (ostream& os, const mystring& str);

class mystring {
public:
    mystring(const char* cstr = 0);
    mystring(const mystring& str);
    mystring& operator = (const mystring& str);
    ~mystring();

    char* get_cstr() const
    { return m_data;}

private:
    char * m_data;
};

#endif<file_sep>#include <iostream>
#include "Fraction.h"
int main() {
    Fraction f(1, 2);
    double d = f + 4.;
    std::cout << d << std::endl;
    return 0;
}<file_sep>file(REMOVE_RECURSE
  "CMakeFiles/pointer_like_class.dir/main.cpp.o"
  "CMakeFiles/pointer_like_class.dir/share_ptr.cpp.o"
  "pointer_like_class.pdb"
  "pointer_like_class"
)

# Per-language clean rules from dependency scanning.
foreach(lang CXX)
  include(CMakeFiles/pointer_like_class.dir/cmake_clean_${lang}.cmake OPTIONAL)
endforeach()
<file_sep>cmake_minimum_required(VERSION 3.14)
project(pointer_like_class)

set(CMAKE_CXX_STANDARD 14)

add_executable(pointer_like_class main.cpp share_ptr.cpp share_ptr.h)