#include <iostream>
class A
{
int j;
public:
A(int x) :j(x) {}
A(A *x) { j = x->j; }
void operator!() { std::cout << "J = " << j << std::endl; }
};
template<class T>
class B
{
int i;
T * x;
public:
B(int xa, T *p) :i(xa) { x = new T(p); }
void operator!() { std::cout << "I = " << i << std::endl; !*x; }
};
int main()
{
A a(1);//a.j = 1;
B<A> b(2, &a);//b.i = 2;b.x指向一个类A的对象,且该对象的j等于1.
!b;
system("pause");
exit(0);
}
#include <iostream>
template<class T>
class Array
{
T *ar;
int size;
public:
Array(int c) { ar = new T[c]; size = c; }
T & operator[](int n) { return ar[n]; }
void print()
{
for (int i = 0; i < size; i++)
std::cout << "NO." << i + 1 << ": " << ar[i] << std::endl;
}
};
int main()
{
Array<int> array(5);
std::cout << "input the value:\n";
for (int i = 0; i < 5; i++) {
std::cout << "NO." << i + 1 << ": ";
std::cin >> array[i];
}
std::cout << "output the value:\n";
array.print();
system("pause");
exit(0);
}
#include <iostream>
#include <string>
class Student
{
int number;
static Student *ip;
Student *p;
public:
Student() { p = NULL; }
Student(int n);
static Student *get_first() { return ip; }
Student *get_next() { return this->p; }
int get_number() { return number; }
};
Student::Student(int n) :number(n)
{
p = NULL;
if (ip == NULL)
ip = this;
else {
Student *temp = ip;
if (n < ip->number) {
ip = this;
p = temp;
}
else {
while (temp)
{
if (n < temp->p->number) {
p = temp->p;
temp->p = this;
break;
}
else {
if (temp->p->p == NULL) {
temp->p->p = this;
break;
}
}
temp = temp->p;
}
}
}
}
Student * Student::ip = NULL;
template<class T>
class Class
{
int num;
T *p;
public:
Class() {}
Class(int n) :num(n) { p = NULL; }
T *insert(int n) { p = new T(n); return p; }
void list_all_memeber(T *x)
{
T *temp = x;
while (temp)
{
std::cout << temp->get_number() << " ";
temp = temp->get_next();
}
std::cout << std::endl;
}
};
int main()
{
Class<Student> cls(9707);
cls.insert(23);
cls.insert(12);
cls.insert(34);
cls.list_all_memeber(Student::get_first());
system("pause");
exit(0);
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template<class T>
class Sorted :public std::vector<T>
{
public:
void sort();
};
template<class T>
void Sorted<T>::sort()
{
T temp;
for (int i = size(); i > 0; i--)
for (int j = 1; j < i; j++) {
if (at(j - 1) > at(j)) {
temp = at(j-1);
at(j-1) = at(j);
at(j) = temp;
}
}
}
char *words[] = { "is", "running", "big", "dog", "a" };
char *words2[] = { "this", "that", "theother" };
int main()
{
Sorted<int> is;
for (int i = 15; i > 0; i--) {
is.push_back(i);
}
cout << "输出对象中的内容(int):\n";
for (int i = 0; i < is.size(); i++)
cout << is[i] << " ";
cout << endl;
is.sort();
cout << "排序之后,输出对象中的内容(int):\n";
for (int i = 0; i < is.size(); i++)
cout << is[i] << " ";
cout << endl;
Sorted<string *> ss;
for (int i = 0; i < 5; i++)
ss.push_back(new string(words[i]));
cout << "输出对象中的内容(string):\n";
for (int i = 0; i < ss.size(); i++)
cout << *ss[i] << " ";
cout << endl;
ss.sort();
cout << "排序之后,输出对象中的内容(string):\n";
for (int i = 0; i < ss.size(); i++)
cout << *ss[i] << " ";
cout << endl;
Sorted<char *> scp;
for (int i = 0; i < 3; i++)
scp.push_back(words2[i]);
cout << "输出对象中的内容(char*):\n";
for (int i = 0; i < scp.size(); i++)
cout << scp[i] << " ";
cout << endl;
scp.sort();
cout << "排序之后,输出对象中的内容(char *):\n";
for (int i = 0; i < scp.size(); i++)
cout << scp[i] << " ";
cout << endl;
system("pause");
exit(0);
}
//string*和char *需要特殊定义sort()