mercredi 27 janvier 2021

templated class List

I have a header file ListTraits.h, with the following content:

#pragma once

//------------ Declarations for List traits used in Test1 in main.cpp

template <typename T>
class ListTraits
{
public:
    virtual unsigned int size() = 0;
    virtual ListTraits& insert(const T& item) = 0;
    virtual void print() = 0;
};

//------------ Declarations for List traits used in Test2 in main.cpp
template <typename T>
class ListTraitsExtended
{
public:
    virtual const T* getCurrentElement() const = 0;
    virtual void advance() = 0;
    virtual void rewind() = 0;
};

I am trying to implement a templated class List<T>, that models a singly linked list and implements its functionality, and I could use all the help you could give me.

The templated class List<T> will inherit the methods from the ListTraits.h


Then I must include "ListTraits.h in a main.cpp file and run this code,

that makes calls to the implementation of list in List<T>

Here is the main.cpp::

#include <iostream>
#include <cstdio>

#include "include/List.h"
#include <random>

void Test1()
{
    std::cout << "---------------------------- TEST 1 -------------------------------\n";
    std::cout << "Create a simple ordered list of specific items.\n";
    List<int> list;
    list.insert(1);
    list.insert(4);
    list.insert(2);
    list.insert(6);
    list.insert(3);
    list.insert(0);
    list.insert(2);
    std::cout << "List size should be 7: " << (list.size() == 7 ? "PASS" : "FAIL") << ".\n";
    std::cout << "Test should print: 0 1 2 2 3 4 6\n";
    std::cout << "Output:            ";
    list.print();

}

int main(int argc, char ** argv)
{
    if (argc < 2)
    {
        exit(-1);
    }
    int seed = 5;
    
    Test1();
    //Test2(seed);
    //Test3(seed);

    getchar();
}

I am not even sure how to get started with this, so I would be really grateful, and thankful, for any advice.

Aucun commentaire:

Enregistrer un commentaire