CPP 核心编程8-模板案例

本文通过实例演示了如何使用模板化技术实现一个通用的数组类,并展示了其在不同数据类型上的应用,包括基本类型和自定义类型。文章还介绍了数组类的构造、拷贝、赋值操作以及一些基本的方法。

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

 

#include <iostream>

using namespace std;

#include "MyArray.hpp"

template<class T>
void printInfo(MyArray<T> &arr) {
    for (int i = 0; i < arr.getSize(); i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

/*class Person;

template<>
void printInfo(MyArray<Person> &arr) {
    for (int i = 0; i < arr.getSize(); i++) {
        cout << "name=" << arr[i].m_Name << " age=" << arr[i].m_Age << endl;
    }
}*/

void test() {
    //有参构造
    MyArray<int> ma(5);
    //拷贝构造
    MyArray<int> ma2(ma);
    //赋值
    MyArray<int> ma3(100);
    ma3 = ma;
}

void test2() {
    //有参构造
    MyArray<int> ma(5);

    for (int i = 0; i < 5; i++) {
        ma.pushBack(i);
    }
    cout << "ma的打印输出为:" << endl;
    printInfo(ma);
    cout << "ma容量:" << ma.getCapacity() << endl;
    cout << "ma大小:" << ma.getSize() << endl;

    MyArray<int> ma2(ma);
    cout << "ma2 的打印输出为:" << endl;
    printInfo(ma2);
    cout << "ma2容量:" << ma.getCapacity() << endl;
    cout << "ma2大小:" << ma.getSize() << endl;
    ma2.popBack();
    cout << "ma2 尾删后的打印输出为:" << endl;
    printInfo(ma2);
    cout << "ma2 尾删后容量:" << ma2.getCapacity() << endl;
    cout << "ma2 尾删后大小:" << ma2.getSize() << endl;

    cout << "ma2[0]的值为:" << ma2[0] << endl;
    ma2[0] = 100;
    cout << "ma2[0]=100 后 ma2[0]的值为:" << ma2[0] << endl;
}

//测试自定义类型
class Person {
public:
    Person() {}

    Person(string name, int age) {
        m_Name = name;
        m_Age = age;
    }

    string m_Name;
    int m_Age;
};

template<>
void printInfo(MyArray<Person> &arr) {
    for (int i = 0; i < arr.getSize(); i++) {
        cout << "name=" << arr[i].m_Name << " age=" << arr[i].m_Age << endl;
    }
}

void test3() {
    MyArray<Person> ma(5);
    Person p1("孙悟空", 100);
    Person p2("猪八戒", 99);
    Person p3("沙僧", 98);
    Person p4("白龙马", 97);
    Person p5("唐僧", 1000);
    ma.pushBack(p1);
    ma.pushBack(p2);
    ma.pushBack(p3);
    ma.pushBack(p4);
    ma.pushBack(p5);
    printInfo(ma);
    cout << "容量" << ma.getCapacity() << endl;
    cout << "大小" << ma.getSize() << endl;
}


int main() {
    test();
    test2();
    test3();
    return 0;
}
有参构造
拷贝构造
有参构造
=调用
析构
析构
析构
有参构造
ma的打印输出为:
0 1 2 3 4 
ma容量:5
ma大小:5
拷贝构造
ma2 的打印输出为:
0 1 2 3 4 
ma2容量:5
ma2大小:5
ma2 尾删后的打印输出为:
0 1 2 3 
ma2 尾删后容量:5
ma2 尾删后大小:4
ma2[0]的值为:0
ma2[0]=100 后 ma2[0]的值为:100
析构
析构
有参构造
name=孙悟空 age=100
name=猪八戒 age=99
name=沙僧 age=98
name=白龙马 age=97
name=唐僧 age=1000
容量5
大小5
析构

上面有一点主意

如果把76行的

template<>
void printInfo(MyArray<Person> &arr) {
    for (int i = 0; i < arr.getSize(); i++) {
        cout << "name=" << arr[i].m_Name << " age=" << arr[i].m_Age << endl;
    }
}

给删掉

将15行的

/*class Person;

template<>
void printInfo(MyArray<Person> &arr) {
    for (int i = 0; i < arr.getSize(); i++) {
        cout << "name=" << arr[i].m_Name << " age=" << arr[i].m_Age << endl;
    }
}*/

注释打开,编译会报错

invalid use of incomplete type 'class Person'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值