关于C++的学习体验

#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>到后来的种种问题,到现在

的调试成功,这种感觉只有自己才能明白(好爽)!

对成员函数以及构造函数的用法有了进一步的体会!

我相信以后会很更多的难题,但是好爽啊(我喜欢挑战)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值