C++ 类模板案例 - 数组类封装

文章介绍了C++中的一个自定义模板类MyArray,包括其构造函数、拷贝构造函数、赋值运算符重载等特性。通过示例展示了如何使用MyArray存储不同类型的数据,以及进行元素的添加、删除和访问。此外,还提及了值传递和引用传入的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

黑马程序员视频链接
在这里插入图片描述
MyArray.hpp

// Created by chenh on 2023/1/14.

#ifndef TEMPLATE_MYARRAY_H
#define TEMPLATE_MYARRAY_H


#include <iostream>
#include <string>

using namespace std;

template<class T>
class MyArray {
public:
    MyArray(int capacity) {
        cout << "parameter constructor" << endl;
        this->_size = 0;
        this->_capacity = capacity;
        this->_array = new T [this->_capacity];
    }

    MyArray(const MyArray &obj) {
        cout << "copy constructor" << endl;
        this->_array = new T [obj._capacity];
        this->_size = obj._size;
        this->_capacity = obj._capacity;
        for (int i = 0; i < this->_size; ++i) {
            this->_array[i] = obj._array[i];
        }
    }

    ~MyArray() {
        cout << "destruction" << endl;
        delete[] this->_array;
    }

    MyArray &operator=(const MyArray &obj) {
        cout << "operator overloading" << endl;
        if (this->_array != NULL) {
            delete[] this->_array;
        }
        this->_array = new T[obj._capacity];
        this->_size = obj._size;
        this->_capacity = obj._capacity;
        for (int i = 0; i < this->_size; ++i) {
            this->_array[i] = obj._array[i];
        }
        return *this;
    }

    T &operator[](int index) {
        if (index <= this->_size) return this->_array[index];
    }

    void push(T data) {
        if (this->_size < this->_capacity)
            this->_array[this->_size++] = data;
    }

    void pop(T data) {
        if (this->_size > 0) {
            this->_size--;
        }
    }

    int get_size() {
        return this->_size;
    }

    int get_capacity() {
        return this->_capacity;
    }

private:
    T *_array;
    int _size;
    int _capacity;
};

#endif // TEMPLATE_MYARRAY_H

main.cpp

#include <iostream>
#include <string>

#include "MyArray.hpp"

using namespace std;

template<class T1, class T2>
class Person {
public:
    Person() {};
    Person(T1 name, T2 age) {
        this->name = name;
        this->age = age;
    }

    T1 name;
    T2 age;
};

void printArray(MyArray<int> &p) {
    for (int i = 0; i < p.get_size(); ++i) {
        cout << p[i] << " ";
    }
    cout << endl;
}

void test01() {
    MyArray<int> p1(5);
    for (int i = 0; i < p1.get_capacity(); ++i) {
        p1.push(i);
    }
    printArray(p1);
}

void printArray(MyArray<Person<string, int>> &arr) {
    for (int i = 0; i < arr.get_size(); ++i) {
        cout << "name: " << arr[i].name << " age:" << arr[i].age << endl;
    }
}

void test02() {
    Person<string, int> p1("Tang monk", 40);
    Person<string, int> p2("Monkey king", 9999);
    Person<string, int> p3("Pigsy", 5000);
    Person<string, int> p4("Sandy", 3000);
    MyArray<Person<string, int>> arr(5);

    arr.push(p1);
    arr.push(p2);
    arr.push(p3);
    arr.push(p4);
    arr.pop(p4);

    printArray(arr);

}

int main() {
    test02();
    return 0;
}
  • 值传递,会导致拷贝构造,所以要使用引用传入或返回
  • 无参构造牢记,养成习惯
  • 模板,我将其理解为,一个存放变量类型的变量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Harvey1104

感谢您的认可,无限飓风不断进步

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值