student(const char * s,int _age,unsigned long long int _s_no,const char * _class_no):people(s,_age){
s_no=_s_no;
class_no = new char [strlen(_class_no)+1];
strcpy(class_no,_class_no);
cout<<"student constructor student number is:"<<s_no<<",class number is:"<<class_no<<endl;
}
char * s如果不加const限定,我输入”zhangsan“就会报错不运行,
另外strlen 适用于char数组和char指针数组,不能用在string。
继承对象的初始化可以放在初始化列表中进行,比如student中对people初始化。
对于初始化时参数一一对应,可以选择用下划线来建立对应关系,比如_class_no(形式参数)与class_no(私有数据),但是注意strcpy不要copy反了.
对于大数相乘,可能发生整形溢出,建议改成long long int ;
注意逻辑不要发生失误。bool类型不要有某种if …if…else搭配没有返回值.
UInt& UInt::operator ++()
{
*this += 1;
return *this;
}
(没有int)前缀++返回++后的结果,而且返回一个自身的引用,没有临时对象的代价,性能较高; 可执行++a+=10;
const UInt UInt::operator ++(int)
{
UInt oldVal = *this;
++(*this);
return oldVal;
}
后缀++返回一个临时对象,增加了一个对象构造和析构的代价,性能较低。无论是自身的引用还是临时对象,可以对结果进行再计算操作。
对过程细分,比如年份,每年加上某月的多少天要判断是否是闰年&&月是否是2月,不同年有不同的月的情况可以把它做成一个函数
=运算符重载防止自反要取if this==&d return *this;
if(head==NULL||head->Data>x)head= new NODE(x,head);
//在一个链表的开头插入一个元素;(该链表head收集有数据)
void push(int x){
++len;
if(head==NULL||head->Data>x)head= new NODE(x,head);
else{
NODE * now=head;
while(now->Next !=NULL&&now->Next->Data<x) now=now->Next;
now->Next= new NODE(x,now->Next);
}
}
如果是到终点(now->NEXT==NULL), now->Next = new NODE(x,NULL);其实就是尾部多加了一个元素,相当于原本now->next的null被替换为new node(new node 的 next指向null).
理解now->Next实际意义是一个指针
int getKth(int k){
if(k>=len) return -1;
NODE * now=head;
for(int i=0;i<k;i++){
now=now->Next;
}
return now->Data;
}
head储存数据,从0开始,i++的时候now就会进一位,i与now同步,直到i到k,now停止移动。
void merge(LinkList * rhs){
len=len+rhs->len;
rhs->len=0;
if(head==NULL){
head=rhs->head;
rhs->head=NULL;
return;
}
if(rhs->head==NULL){return;}
NODE * now1=head;
NODE * now2=rhs->head;
if(now1->Data<now2->Data){
head=now1;
now1=now1->Next;
}
else{
head=now2;
now2=now2->Next;
}
NODE * tail=head;
while(now1!=NULL||now2!=NULL){
if(now1==NULL){
tail->Next=now2;
now2=now2->Next;
}
else if(now2==NULL){
tail->Next=now1;
now1=now1->Next;
}
else if (now1->Data < now2->Data){
tail->Next=now1;
now1=now1->Next;
}
else{
tail->Next=now2;
now2=now2->Next;
}
tail=tail->Next;
}
rhs->head=NULL;
}
这算链表很难的了。