Как из этого класса сделать шаблон и потом реализовать в главной:
класс:
#include
#include
#include
using namespace std;
//АТД
class Atd
{
class Spisok
{
public:
char simv; //Данные
Spisok *next; //Указатель на следующий узел
Spisok (char x = NULL) //Конструктор
{
simv = x; next = NULL;
}
};
Spisok *begin, *end; //Указатели на начало, конец списка
int n; //количество элементов в списке
public:
Atd () //Конструктор по умолчанию
{
begin = end = NULL;
n = 0;
}
Atd (char *str); //Конструктор с параметрами
~Atd(); //Деструктор
bool operator !=(const Atd&); //Перегрузка операции != - проверка на неравенство
Atd& operator =(const Atd&); //Перегрузка операции присваивания
Atd& operator +(Atd&); //Перегрузка операции + -объединить два списка (Atd+Atd)
const char& operator [](int i) const; //Перегрузка операции [] -доступ к элементу в заданной
//позиции только для чтения (например: int i; char c;list L;c=L[i])
char& operator [](int i); //Перегрузка операции [] -доступ к элементу в заданной позиции
// void Input(); //Ввод с клавиатуры
void Print(); //Функция вывода на экран
};
//Конструктор с параметрами
Atd :: Atd (char* str)
{
n = 0;
Spisok *pv = new Spisok;
pv->simv = str[0];
begin = end = pv;
n++;
for (int i = 1; i < (int)strlen(str); i++)
{
Spisok *pv = new Spisok;
pv->simv = str[i];
end->next = pv;
end = pv;
n++;
}
end ->next = 0;
cout << endl << "vyzvan konstruktor c parametrami.\n";
}
//Деструктор
Atd :: ~Atd ()
{
if (begin != NULL )
{
Spisok *pv;
while (begin)
{
pv = begin ->next;
delete begin;
begin = pv;
}
}
cout << endl << "vyzvan destructor.\n";
}
//Перегрузка операции [] -доступ к элементу в заданной позиции только для чтения
const char& Atd :: operator [](int i) const
{
if (i < 0 || i >= n)
{
cout << i << " is a bad index" << endl;
}
else
{
Spisok *pv = begin;
for (int j = 0; j < i; j++)
pv = pv->next;
return pv ->simv;
}
}
//....
// Перегрузка операции [] -доступ к элементу в заданной позиции
char& Atd :: operator[](int i)
{
if (i < 0 || i >= n)
{
cout << i << " is a bad index" << endl;
}
else
{
Spisok *pv = begin;
for (int j = 0; j < i; j++)
pv = pv->next;
return pv ->simv;
}
}
//....*/
//Перегрузка операции присваивания
Atd& Atd :: operator =(const Atd& x)
{
cout << "vyzvana peregruzka operatora '='.\n";
this->~Atd();
Spisok *pv = new Spisok;
pv->simv = x[0];
begin = end = pv;
for (int i = 1; i < x.n; i++)
{
Spisok *pv = new Spisok;
pv->simv = x[i];
end ->next = pv;
end = pv;
}
end ->next = 0;
n = x.n;
return (*this);
}
//Перегрузка операции + -объединить два списка (Atd+Atd)
Atd& Atd :: operator +(Atd& x)
{
cout << "vyzvana peregruzka operatora '+'.\n";
end ->next = x.begin;
n = n + x.n;
x.begin = 0;
x.end = 0;
return (*this);
}
//-----------------------------------------------------------------------
//Проверка АТД на неравенство
bool Atd :: operator !=(const Atd& x)
{
cout << "vyzvana peregruzka operatora '!='.\n";
Spisok *pv = begin;
Spisok *dpv = x.begin;
while ((pv)&&(dpv))
{
if (pv->simv == dpv->simv) return false;
pv = pv->next;
dpv = dpv->next;
}
return true;
}
//статическая компонента-функция просмотра всего списка
void Atd :: Print()
{
Spisok *pv = begin;
cout << endl << "Atd: \n";
while (pv)
{
cout << pv->simv << ' ';
pv = pv->next;
}
cout << endl;
}
Главная:
#include
#include
using namespace std;
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main()
{
cout << "Press ENTER to create objects...\n";
getch ();
Atd first("IAM");
cout << "object is created.\n"
<< "Press ENTER to look at it...\n";
getch ();
first.Print();
cout << "Press ENTER to create one more object...\n";
getch();
Atd second("ACOUY");
cout << "list is object.\n"
<< "Press ENTER to look at it...\n";
getch ();
second.Print();
cout << endl << "compare them.\n";
cout << "Press ENTER to look at result.\n";
getch ();
if (first != second) cout << "They are didn't =.\n";
else cout << "They are =.\n";
first.Print();
second.Print();
cout << endl << "the first object compare to the second object. And following compare them anew.\n";
cout << "Press ENTER to look at result.\n";
getch ();
first = second;
if (first != second) cout << "They are didn't =.\n";
else cout << "They are =.\n";
first.Print();
second.Print();
cout << endl << "add first list to second.\n";
cout << "Press ENTER to look at result.\n";
getch ();
first + second;
first.Print();
getch();
cout << endl << "[] simvol from first object.\n";
first.Print();
int i;
cout << "Input index (0-9): ";
cin >> i;
cout << first[i] << endl;
cout << endl << "[] simvol from first object.\n";
first.Print();
cout << "Input index (0-9): ";
cin >> i;
first[i] = '%';
first.Print();
getch ();
return 0;
}