#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> using namespace std; class Student { public: Student() { this->id = 0; this->name = NULL; } Student(int id, char *name) { this->id = id; //this->name = name; int len = strlen(name); this->name = new char[len + 1]; strcpy(this->name, name); } Student(const Student &another) { this->id = another.id; //深拷貝 int len = strlen(another.name); this->name = new char[len + 1]; strcpy(this->name, another.name); } Student & operator=(const Student &another) { //1 防止自身賦值 if (this == &another) { return *this; } //2 先將自身的額外開闢的空間回收掉 if (this->name != NULL) { delete[] this->name; this->name = NULL; this->id = 0; } //3 執行深拷貝 this->id = another.id; int len = strlen(another.name); this->name = new char[len + 1]; strcpy(this->name, another.name); //4 返回自己 return *this; } void printS() { cout << name << endl; } ~Student() { if (this->name != NULL) { delete[] this->name; this->name = NULL; this->id = 0; } } private: int id; char *name; }; int main(void) { Student s1(1, "zhang3"); Student s2(s1); s2 = s1; Student s3(2, "li4"); //s2 = s3 = s1;//s2 = 賦值操做符 s1.printS(); s2.printS(); s3.printS(); return 0; }
main.cppios
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include "MyArray.h" using namespace std; int main(void) { MyArray array1(10);//開闢10元素的數組 //賦值操做 for (int i = 0; i < 10; i++) { //array1.setData(i, i + 10); array1[i] = i + 10;//space[1] = 1+10 } cout << "--------" << endl; cout << "array1:" << endl; for (int i = 0; i < 10; i++) { cout << array1[i] << " "; } cout << endl; MyArray array2 = array1; cout << "array2:" << endl; for (int i = 0; i < array2.getLen(); i++) { cout << array2[i] << " "; } cout << endl; cout << " ------------" << endl; MyArray array3(5); cin >> array3; cout << "array3:" << endl; cout << array3 << endl; cout << endl; if (array3 != array1) { cout << "不相等" << endl; } else { cout << "相等 " << endl; } return 0; }
Array.cpp數組
#include "MyArray.h" MyArray::MyArray() { cout << "MyArray()..." << endl; this->len = 0; this->space = NULL; } MyArray::MyArray(int len) { if (len <= 0) { this->len = 0; return; } else { this->len = len; //給space開闢空間 this->space = new int[this->len]; cout << "MyArray::MyArray(int len) ..." << endl; } } MyArray::MyArray(const MyArray &another) { if (another.len >= 0) { this->len = another.len; //深拷貝 this->space = new int[this->len]; for (int i = 0; i < this->len; i++) { this->space[i] = another.space[i]; } cout << "MyArray::MyArray(const MyArray &another) ..." << endl; } } MyArray::~MyArray() { if (this->space != NULL) { delete[]this->space; this->space = NULL; len = 0; cout << "MyArray::~MyArray() ..." << endl; } } void MyArray::setData(int index, int data) { if (this->space != NULL) { this->space[index] = data; } } int MyArray::getData(int index) { return this->space[index]; } int MyArray::getLen() const { return this->len; } MyArray& MyArray::operator=(const MyArray& another) { if (this == &another) { return *this; } if (this->space != NULL) { delete[]this->space; this->space = NULL; this->len = 0; } if (another.len >= 0) { this->len = another.len; //深拷貝 this->space = new int[this->len]; for (int i = 0; i < this->len; i++) { this->space[i] = another.space[i]; } cout << "MyArray::operator=(const MyArray& another) ..." << endl; } return *this; } int & MyArray::operator[](int index) const { return this->space[index]; } ostream &operator<<(ostream &os,const MyArray &array) { os << "遍歷整個數組 " << endl; //array.getLen(); //getLen(&array); for (int i = 0; i < array.getLen(); i++) { os << array[i] <<" ";//array.operator[]( i) } os << "調用的<<操做符重載" << endl; return os; } istream &operator>>(istream &is, MyArray &array) { cout << "請輸入" << array.getLen() << "個數" << endl; for (int i = 0; i < array.getLen(); i++) { cin >> array[i]; } return is; } bool operator==(MyArray &array1, MyArray &array2) { if (array1.len != array2.len) { return false; } for (int i = 0; i < array1.len; i++) { if (array1.space[i] != array2.space[i]) { return false; } } return true; } bool MyArray::operator!=(MyArray &another) { return !(*this == another); }
Array.h函數
#pragma once #include <iostream> using namespace std; class MyArray { public: MyArray(); MyArray(int len); MyArray(const MyArray &another); ~MyArray(); void setData(int index, int data); int getData(int index); int getLen() const ; MyArray& operator=(const MyArray& another); int & operator[](int index) const; friend ostream &operator<<(ostream &os,const MyArray &array); friend istream &operator>>(istream &is, MyArray &array); friend bool operator==(MyArray &array1, MyArray &array2); bool operator!=(MyArray &another); private: int len; int *space; };
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Sqr { public: Sqr(int a) { this->a = a; } int operator()(int value) { return value * value; } int operator()(int value1, int value2) { return value1 * value2; } private: int a; }; void func(int a) { } int main(void) { Sqr s(10); int value = s(2); //s.operator()(2); //將一個對象 當成一個普通函數來調用。 //稱這種對象是 仿函數,僞函數, 函數對象 cout << value << endl; value = s(10, 20); cout << value << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <fstream> using namespace std; class A { public: A() { cout << "A()..." << endl; } A(int a) { cout << "A(int)..." << endl; this->a = a; } //重載的new操做符 依然會觸發對象的構造函數 void * operator new(size_t size) { cout << "重載了new操做符" << endl; return malloc(size); } void *operator new[](size_t size) { cout << "重載了new[]操做符" << endl; return malloc(size); } void operator delete(void * p) { cout << "重載了delete操做符" << endl; if (p != NULL) { free(p); p = NULL; } } void operator delete[](void *p) { cout << "重載了delete[]操做符" << endl; if (p != NULL) { free(p); p = NULL; } } ~A() { cout << "~A().... " << endl; } private: int a; }; int main(void) { //char *array = malloc(sizeof(char)* 80); //int *value_p = new int; A *array_p = new A[10]; //array_p->operator new[](sizeof(A[10])); delete[] array_p; A *ap = new A(10); //ap->operator new(sizeof(A)); delete ap; return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Test { public: Test(int value) { this->value = value; } Test operator+(Test &another) { cout << "執行了+操做符重載" << endl; Test temp(this->value + another.value); return temp; } bool operator&&(Test &another) { cout << "執行了&&操做符重載" << endl; if (this->value && another.value) { return true; } else { return false; } } bool operator||(Test &another) { cout << "重載了||操做符" << endl; if (this->value || another.value) { return true; } else { return false; } } ~Test(){ cout << "~Test()..." << endl; } private: int value; }; int main(void) { int a = 1; int b = 20; Test t1(0); Test t2(20); //重載&&操做符,並不會發生短路現象。 if (t1 && (t1+t2) ) { //t1.operator&&( t1.operator+(t2) ) cout << "爲真" << endl; } else { cout << "爲假" << endl; } cout << "------" << endl; if (t1 || (t1 + t2)) {//t1.operator||( t1.operator+(t2) ) cout << "爲真" << endl; } else { cout << "爲假" << endl; } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <memory> using namespace std; class A { public: A(int a) { cout << "A()..." << endl; this->a = a; } void func() { cout << "a = " << this->a << endl; } ~A() { cout << "~A()..." << endl; } private: int a; }; class MyAutoPtr { public: MyAutoPtr(A * ptr) { this->ptr = ptr;//ptr = new A(10) } ~MyAutoPtr() { if (this->ptr != NULL) { cout << "delte ptr" << endl; delete ptr; this->ptr = NULL; } } A* operator->() { return this->ptr; } A& operator*() { return *ptr; } private: A *ptr; }; void test1() { #if 0 A* ap = new A(10); ap->func(); (*ap).func(); delete ap; auto_ptr<int> ptr(new int); #endif auto_ptr<A> ptr(new A(10)); ptr->func(); (*ptr).func(); } void test2() { MyAutoPtr my_p(new A(10)); my_p->func(); //my_p.ptr -> func() (*my_p).func(); // *ptr.func() } int main(void) { //test1(); test2(); return 0; }
main.cpp優化
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include "MyString.h" using namespace std; int main(void) { string s1; MyString s1("abc"); MyString s2("123"); //cout << s1 + s2 << endl; cout << s1 << endl; cout << s2 << endl; #if 0 MyString s1("abc"); MyString s2(s1); MyString s3 = "123"; cout << s1 << endl; cout << s2 << endl; s1[1] = 'x'; cout << s1 << endl; s1 = s3; cout << s1 << endl; #endif return 0; }
String.cppthis
#include "MyString.h" MyString::MyString() { this->len = 0; this->str =NULL; } MyString::MyString(const char *str) { if (str == NULL) { this->len = 0; this->str = new char[0 + 1]; strcpy(this->str, ""); } else { int len = strlen(str); this->len = len; this->str = new char[len + 1]; strcpy(this->str, str); } } //初始化時候被調用的 MyString::MyString(const MyString &another) { this->len = another.len; this->str = new char[this->len + 1]; strcpy(this->str, another.str); } MyString::~MyString() { if (this->str != NULL) { cout << this->str << "執行了析構函數" << endl; delete this->str; this->str = NULL; this->len = 0; } } char & MyString::operator[](int index) { return this->str[index]; } MyString & MyString::operator=(const MyString &another) { if (this == &another) { return *this; } if (this->str != NULL) { delete[] this->str; this->str = NULL; this->len = 0; } this->len = another.len; this->str = new char[this->len + 1]; strcpy(this->str, another.str); return *this; } ostream & operator<<(ostream &os, MyString&s) { os << s.str; return os; } istream & operator>>(istream &is, MyString &s) { //1 將s以前的字符串釋放掉 if (s.str != NULL) { delete[] s.str; s.str = NULL; s.len = 0; } //2 經過cin添加新的字符串 char temp_str[4096] = { 0 }; cin >> temp_str; int len = strlen(temp_str); s.str = new char[len + 1]; strcpy(s.str, temp_str); s.len = len; return is; } MyString MyString::operator+(MyString &another) { MyString temp; int len = this->len + another.len; temp.len = len; temp.str = new char[len + 1]; memset(temp.str, 0, len + 1); strcat(temp.str, this->str); strcat(temp.str, another.str); return temp; }
String.hspa
#pragma once #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class MyString { public: MyString(); //MyString(int len); //建立一個長度是len的string對象 MyString(const char *str); MyString(const MyString &another); ~MyString(); //重載操做符[] char &operator[](int index); //重載操做符>> friend istream & operator>>(istream &is, MyString &s); //重載=操做符 MyString & operator=(const MyString &another); //重載==操做符 //重載!=操做符 //重載+操做符 MyString operator+(MyString &another); //重載操做符<< friend ostream & operator<<(ostream &os, MyString&s); private: int len; char *str; };