Re: Наследование шаблона
От: vlad0922  
Дата: 19.04.13 11:09
Оценка:
Дополню.
Код в файла set.cpp

#include "set.h"

template<class T>
void Set<T>::sort()
{    
try
{
if (this->n==0) throw this->n;
else
{
    int temp;

    for(int i=0; i < this->n; i++)          
        for( int j = this->n-1; j > i; j-- )   
          if ( this->array[j-1] > this->array[j] ) {
              temp=this->array[j-1];
            this->array[j-1]=this->array[j];
            this->array[j]=temp;
            }
}
}
catch (int n)
{
    std::cout<<"Array is not initialized" << std::endl;
}
}


template<class T>
bool Set<T>::check(int element)
{
try
{
if (this->n==0) throw this->n;
else
{
    for (int i=0; i<this->n;i++)
        if (element==this->array[i])
            return true;
    
    return false;
}
}
catch(int n)
{
    std::cout<<"Array is not initialized" << std::endl;
}
}

template<class T>
void Set<T>::print()
{
    try
{
if (this->n==0) throw this->n;
else
{
    for (int i=0;i<this->n;i++)
        std::cout<<this->array[i] << ' ';

    std::cout << std::endl;
    }
}
catch(int n)
{
    std::cout<<"Array is not initialized" << std::endl;
}
}

template<class T>
void Set<T>::setElement(int number, T source)
{
    try
    {
        if ( (number>=this->n) || (number<0) )
            throw number;
        else
        {
            if (check(source))
            std::cout<<"There is equal element" << std::endl;
            else
            {
            this->array[number]=source;
            sort();
            }
        }
    }
    catch (int number)
    {
        std::cout << "Wrong element number" << std::endl;
    }
}


Код в файле vect.cpp

#include "vect.h"

template<class T>
void vect<T>::create(int count)
{
    try
    {
        if (count<=0)
            throw count;
        else
            {
                n=count;
                array=new T[n];
                for (int i=0;i<n;i++)
                {
                std::cout << i;
                array[i]=0;
                }
            }
    }
    catch(int number)
    {
        std::cout << "the number of elements in the array <= 0" << std::endl;
    }    
}

template<class T>
T vect<T>::get(int number)
{
    try
    {
        if ( (number>=n) || (number<0) )
            throw number;
        else
            return array[number];
    }
    catch (int number)
    {
        std::cout << "Wrong element number" << std::endl;
    }
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.