01 强化训练-数组的封装

Myarray.h

#pragma once
#include <iostream>
using namespace std;

class MyArray {
public:
    MyArray();
    MyArray(int capacity);
    MyArray(const MyArray & array);
    ~MyArray();

    //尾插法
    void push_Back(int val){
        this->pAddress[this->m_size] = val;
        this->m_size++;
    }
    //根据索引来获取值
    int getData(int index);

    void setData(int index, int val);
    int& operator[](int index);

    int getsize();//获取数组大小
    int getcapacity();//获取数组容量

private:
    int * pAddress;//指向真正存储数据的指针
    int m_size;
    int m_capacity;
};

Myarray.cpp

#include "MyArray.h"

//默认构造
MyArray::MyArray()
{
    m_capacity = 100;
    m_size = 0;
    pAddress = new int[this->m_capacity];
}
//有参构造 参数 数组容量
MyArray::MyArray(int capacity)
{
    cout << "有参调用" << endl;
    this->m_capacity = capacity;
    this->m_size = 0;
    this->pAddress = new int[this->m_capacity];
}



//拷贝构造
MyArray::MyArray(const MyArray & array)
{
    cout << "拷贝构造" << endl;
    this->pAddress = new int[array.m_capacity];
    this->m_size = array.m_size;
    this->m_capacity = array.m_capacity;

    for (int i = 0; i < array.m_size; i++) {
        this->pAddress[i] = array.pAddress[i];
    }
}



MyArray::~MyArray()
{
    cout << "析构调用" << endl;
    if (this->pAddress != NULL) {
        delete[] this->pAddress;
        this->pAddress = NULL;
    }
}

int MyArray::getData(int index)
{
    return this->pAddress[index];
}

void MyArray::setData(int index, int val)
{
    this->pAddress[index] = val;
}

int& MyArray::operator[](int index)
{
    return this->pAddress[index];
}

int MyArray::getsize()
{
    return this->m_size;
}

int MyArray::getcapacity()
{
    return this->m_capacity;
}

main.cpp

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

void test01() {
    //堆区创建数组
    MyArray *array = new MyArray(30);
    //delete array;
    MyArray *array2 = new MyArray(*array);
    MyArray  array3 = MyArray(*array);
    //MyArray *array3 = array;这样的话是声明了另一个指针
    delete array;
    //尾插法测试
    for (int i = 0; i < 10; i++) {
        array2->push_Back(i);
    }
    //获取数据测试

    for (int i = 0; i < 10; i++) {
        cout << array2->getData(i) << endl;
    }


    for (int i = 0; i < 10; i++) {
        array3.push_Back(i);
    }
    //设置值测试

    array2->setData(0, 1000);
    cout << array2->getData(0) << endl;

    cout << "数组大小:" << array2->getsize() << endl;
    cout << "数组容量:" << array2->getcapacity() << endl;

    cout << "array3的内容" << endl;
    cout << array3[3] << endl;
    array3[0] = 100;
    cout << array3[0];
    
}

int main() {
    test01();
    system("pause");
}

02 加号运算符的重载

#include <iostream>
#include <stdlib.h>
using namespace std;

class Person {
public:
    Person() {
        cout << "调用默认构造函数" << ++cnt << "次" << endl;
    }
    Person(int a, int b) :m_A(a), m_B(b) {

    }
    //+号运算符重载 成员函数
    Person operator+(Person & p) {
        Person temp;
        temp.m_A = this->m_A + p.m_A;
        temp.m_B = this->m_B + p.m_B;
        return temp;
    
    }
    int m_A;
    int m_B;
    static int cnt;
};

int Person::cnt = 0;

//利用全局函数 进行+号运算符的重载
Person operator+(Person &p1, Person &p2) {//二元
    Person temp;
    temp.m_A = p1.m_A + p2.m_A;
    temp.m_B = p1.m_B + p2.m_B;
    return temp;
}
void test01() {
    Person p1(10, 10);
    Person p2(10, 10);
    cout << p1.m_A << p1.m_B << endl;
    Person p3 = p1 + p2;//p1+p2是由什么转变过来的呢,p1.operator+(p2),operator+(p1,p2)
    cout << p3.m_A << p3.m_B << endl;

    Person p4 = p3 + p2;

    cout << p4.m_A << p4.m_B << endl;

}

int main() {
    test01();
    system("pause");
}

03 左移运算符的重载

class Person {
public:
    Person();
    Person(int a, int b) {
        this->x = a;
        this->y = b;
    }
    int  getX() {
        return this->x;
    }

    int getY() {
        return this->y;
    }
private:
    int x;
    int y;
};

//ostream & operator << (ostream & cout, Person & p1) {
//    cout << "p1的x :" << p1.getX() << "p1的y:" << p1.getY();
//    return cout;
//}

ostream & operator << (ostream & cout, Person & p1) {
    cout << "p1.x:" << p1.getX() << "p1的y:" << p1.getY();
    return cout;
}

void test01() {
    Person p1(10, 10);
    cout << p1 << endl;
}


int main() {
    test01();
    system("pause");
}

04 ++ -- 重载

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

class Person {
public:
    //friend ostream & operator<<(ostream &cout, Person & p);
        
    Person() {
        myAge = 0;
    }


    Person(Person &p) {
        this->myAge = p.myAge;

    }
    //前置
    Person& operator++() {
        this->myAge++;
        return *this;
    }

    //后置++
    Person& operator++(int) {
        Person temp = *this;
        this->myAge++;
        return temp;
    }

    int myAge;
};


ostream& operator<<(ostream &cout,Person &p) {
    cout << "年龄为:" << p.myAge;
    return cout;
}


void test01() {
    Person p;
    cout << ++(++p) << endl;
    cout << p << endl;
    cout << p++ << endl;
    cout << p;
    cout << p++ << endl;
    cout << p;
    
    

}

int main() {
    test01();
    system("pause");
}

05 指针的重载

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


class Person {
public:
    Person() {

    }

    Person(int a) {
        age = a;
    }

    ~Person() {
        cout << "Person的构造函数" << endl;
    }

    void showAge() {
        cout << "age:" << age << endl;
    }

    int age;
};





class SmartPointer {
public:
    SmartPointer(Person * p) {
        this->person = p;
    }


    Person * operator->() {
        return this->person;
    }

    Person operator*() {
        return *this->person;
    }

    ~SmartPointer() {
        cout << "智能指针对其进行析构" << endl;
        if (this->person != NULL) {
            delete this->person;
            this->person = NULL;
        }
    }

    Person * person;
};


void test01() {
    //Person p(10);
    //p.showAge();
    SmartPointer sp(new Person(10));
    sp->showAge(); //编译器对其进行了优化  实际上是 sp->->这样的写法
    (*sp).showAge();
    

}

int main() {
    test01();
    system("pause");
}

06 =号的重载

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

//一个类默认提供了 构造函数 拷贝构造函数 析构函数  =运算符
class Person {
public:
    Person(const char * name) {
        this->pName = new char[strlen(name) + 1];
        strcpy(this->pName, name);
    }
    
    
    ~Person() {
        if (this->pName != NULL) {
            delete[] this->pName;
            this->pName = NULL;
        }
    }

    Person& operator=(Person &p) {
        if (this->pName != NULL) {
            delete[] this->pName;
            this->pName = NULL;
        }

        this->pName = new char[strlen(p.pName) + 1];
        strcpy(this->pName, p.pName);
        return *this;

    }
    char *pName;
};


void test01() {
    Person p1("狗蛋aaa");
    Person p2("狗剩");
    p2 = p1;
    cout << p2.pName << " " <<p1.pName;
    /*Person p3("");
    p3 = p1 = p2;
    cout <<p3.pName <<" "<<  p2.pName << " " << p1.pName;*/



}


int main() {
    test01();
    system("pause");
}

07 关系运算符的重载

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


class Person{
public:

    Person(int h, int w) {
        this->height = h;
        this->weight = w;
    }
    bool operator==(const Person& p) {
        if (this->height == p.height && this->weight == p.weight) {
            return true;
        }
        return false;
    }

    bool operator!=(const Person& p) {
        if (this->height == p.height && this->weight == p.weight) {
            return false;
        }
        return true;
    }


private:
    int height, weight;

};


void test01() {
    Person p1(10, 30);
    Person p2(10, 20);
    if (p1 == p2) {
        cout << "相等" << endl;
    }

    if (p1 != p2) {
        cout << "不相等" << endl;
    }


}



int main() {
    test01();
    system("pause");
}

08 ()运算符的重载

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

class Myprint {
public:
    void operator() (string text) {
        cout << "hello," << text << endl;
    }
};



class Myadd {
public:
    int operator() (int x, int y) {
        return x + y;
    }
};

void test01() {
    Myprint myprint;
    myprint("superzhaoyang");
}

void test02() {
    Myadd myadd;
    cout << myadd(10, 20) << endl;
}


void test03() {

    cout << Myadd()(10,50) << endl; //匿名对象 
}

int main() {
    //test01();
    test03();

    system("pause");
}

09 强化训练-字符串类的封装

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


class Mystring {
public:
    friend ostream& operator<< (ostream& cout, Mystring& mystring);
    friend istream& operator>> (istream& cout, Mystring& mystring);
    Mystring(const char* str) {
        cout << "带参数的构造函数" << endl;
        this->str = new char[strlen(str) + 1];
        strcpy(this->str, str);
        this->msize = strlen(str);

    }

    Mystring(const Mystring & mystring) {

        this->str = new char[strlen(mystring.str) + 1];
        strcpy(this->str, mystring.str);
        this->msize = strlen(mystring.str);
    }

    ~Mystring() {

        cout << "析构函数的调用" << endl;
        if (this->str != NULL) {
            delete[] this->str;
            this->str = NULL;
        }
        this->msize = 0;


    }


    Mystring& operator=(const char * string) {
        if (this->str != NULL) {
            delete[] this->str;
            this->str = new char[strlen(string) + 1];

        }

        strcpy(this->str, string);

        return *this;
    }

    Mystring& operator=( Mystring& string) {
        if (this->str != NULL) {
            delete[] this->str;
            this->str = new char[strlen(string.str) + 1];
        }

        strcpy(this->str, string.str);
        this->msize = string.msize;
        return string;
    }

    Mystring & operator+ (Mystring& string) {
        int newsize = strlen(this->str) + strlen(string.str) + 1;
        char * tmp = new char[newsize];
        //cout << "tmp :" << tmp;
        memset(tmp, 0, newsize);
        strcat(tmp, this->str);
        strcat(tmp, string.str);
        Mystring str(tmp);
        delete[] tmp;
        tmp = NULL;
        return str;


        

    }

    Mystring & operator+ (const char* str) {
        strcat(this->str, str);
        return *this;
    }


    char& operator[](int index) {
        return this->str[index];

    }

    bool operator==(Mystring& mystring) {
        if (strcmp(this->str,mystring.str) == 0 && this->msize == mystring.msize)
            return true;

        return false;
    }

    bool operator==(const char * str) {
        if (this->str == str)
            return true;

        return false;
    }



private:
    char * str;
    int msize;
};

ostream& operator<< (ostream& cout, Mystring& mystring) {
    cout << mystring.str;
    return cout;
}


istream& operator>> (istream& cin, Mystring& mystring) {
    if (mystring.str != NULL) {
        delete[] mystring.str;
        mystring.str = NULL;
    }
    char buf[1024];
    cin >> buf;
    mystring.str = new char[strlen(buf) + 1];
    strcpy(mystring.str, buf);
    return cin;
}



void test01() {
    Mystring mystring("hello,world");

    cout << mystring << endl;

    //cin >> mystring;
    //cout << mystring << endl;
    Mystring mystring2 = "";
    mystring2 = mystring;
    cout << mystring2 << endl;
    cout << mystring2[0] << endl;
    //mystring2[0] = '2';
    cout << mystring2[0] << endl;
    //Mystring str4 = mystring + mystring2;
    //cout << (mystring + mystring2) << endl; //这里为什么不行
    //cout << mystring + "niub" << endl;

    if (mystring == mystring2) {
        cout << "equal" << endl;
    }

}
int main() {
    test01();
    system("pause");
}
最后修改:2021 年 03 月 11 日 05 : 41 PM
如果觉得我的文章对你有用,请随意赞赏