#ifndef DATA_H
#define DATA_H
#include <stdlib.h>
#include <time.h>
template <class T>
struct Node{
T key; //key word used to sort
T info; //other informations
};
template <class T>
class Data{
Node<T> *arr;
T number;
public:
Data(int);
void InsertSort(); //InsertSort();
void print()const;
~Data();
};
template<class T>
Data<T>::Data(int n){
arr=(Node<T> *)malloc(sizeof(Node<T>)*n);
number=n;
srand((unsigned)time(NULL));
for(int i=0;i<number;++i)
{
arr[i].key=(T)rand(); //rand()%(end-begin+1)+begin;
}
}
template<class T>
void Data<T>::InsertSort(){
Node<T> temp;
for(int i=1;i<number;++i)
{
temp.key=arr[i].key; //Insert arr[i].key into the sorted sequence arr[1....j-1]
int j=i-1;
while(temp.key<arr[j].key&&j>=0)
{
arr[j+1].key=arr[j].key;
j--;
}
arr[j+1].key=temp.key;
}
}
template<class T>
Data<T>::~Data(){
delete[] arr;
}
template<class T>
void Data<T>::print() const{
for(int i=0;i<number;i++)
cout<<arr[i].key<<endl;
}
#endif
看了很多的C++书以及网上关于C++的评论,可是好象都不怎么能深刻的体会什么是面向对象。
上面是是自己用2天写的一个类,从一开始的#include<iostream>到后来的种种问题,到现在
的调试成功,这种感觉只有自己才能明白(好爽)!
对成员函数以及构造函数的用法有了进一步的体会!
我相信以后会很更多的难题,但是好爽啊(我喜欢挑战)