01 _static.cpp
#include<iostream>
using namespace std;
struct student{
int id;
string name;
static string school;
student()=default;
student(int i,string n/*,string s*/){
id=i;name=n;/*school=s;*/}
~student(){}
};
string student::school="ZM";
int main()
{
student s1(1001,"zhangsan");
student s2(1002,"lisi");
s1.school = "SZZM";
cout<<s1.school<<endl;
cout<<s2.school<<endl;
}
02_static_class_member.cpp
#include<iostream>
using namespace std;
struct student{
int id;
string name;
static string school;
student()=default;
student(int i,string n/*,string s*/){
id=i;name=n;/*school=s;*/}
~student(){}
};
string student::school="ZM";
int main()
{
cout<<student::school<<endl;
}
03_static_class_function.cpp
#include<iostream>
using namespace std;
struct student{
int id;
string name;
static string school;
student()=default;
student(int i,string n/*,string s*/){
id=i;name=n;/*school=s;*/}
~student(){}
static void set_school(string s)
{
school = s;
}
};
string student::school="ZM";
int main()
{
student s1(1001,"zhangsan");
student s2(1002,"lisi");
student::set_school("SZZM");
cout<<s1.school<<endl;
cout<<s2.school<<endl;
}
04_static_list.cpp
#include<iostream>
using namespace std;
struct student{
int id;string name;int age;
student()=default;
student(int i,string n,int a)
:id(i),name(n),age(a)
{
student *p =new student(*this);
if(p == nullptr){
perror("new p");
return;
}
p->next = head;
head = p;
}
student(student &s){
id=s.id;name=s.name;age=s.age;
}
~student(){
student* p=head,*q=head;
while(p)
{
if(p->id == this->id)
break;
q =p;
p=p->next;
}
if(p){
if(p == head){
head = head->next;
}else
q->next = p->next;
delete p;
return;
}else
return ;
}
student * next=nullptr;
static student * head;
static int show_stu_list()
{
student *p = head;
while(p)
{
cout<<p->id<<" "<<p->name<<" "<<p->age<<endl;
p = p->next;
}
return 0;
}
};
student* student::head=nullptr;
struct stu_list{
private:
student * head=nullptr;
public:
int create_stu_list()
{
student* p=nullptr;
char ch;
while(1)
{
p = new student();
cin>>p->id>>p->name>>p->age;
if(cin.fail())break;
p->next = head;
head = p;
// cout<<"是否继续?[Y/n]";
// cin>>ch;
// if(ch == 'n' || ch== 'N')
// break;
}
return 0;
}
int show_stu_list()
{
student *p = head;
while(p)
{
cout<<p->id<<" "<<p->name<<" "<<p->age<<endl;
p = p->next;
}
return 0;
}
//return value: 0/OK,-1/failed;
int insert(student &s)
{
student *p =new student(s);
if(p == nullptr){
perror("new p");
return -1;
}
p->next = head;
head = p;
return 0;
}
int remove(int id)
{
student* p=head,*q=head;
while(p)
{
if(p->id == id)
break;
q =p;
p=p->next;
}
if(p){
if(p == head){
head = head->next;
}else
q->next = p->next;
delete p;
return 0;
}else
return -1;
}
int reserve_stu_list()
{
student * new_head=nullptr,*p;
if(head == nullptr)return -1;
while(head)
{
p = head;
head=head->next;
p->next = new_head;
new_head = p;
}
head = new_head;
return 0;
}
int release_stu_list()
{
student *p=head,*q=head;
while(p)
{
q = p->next;
delete p;
p = q;
}
head= nullptr;
}
};
student get_stu()
{
student s(1004,"zhaoqi",20);
student::show_stu_list();
return s;
}
int main()
{
student s1(1001,"zhangsan",20);
student s2(1002,"lisi",20);
student s3(1003,"wangwu",20);
get_stu();
student::show_stu_list();
}