c++ static 类
来源:互联网 发布:淘宝商品品牌词库 编辑:程序博客网 时间:2024/05/17 22:46
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();}
0 0
- C#Static类和Static构造函数
- static c ,C++类static 成员变量
- static c ,C++类static 成员变量
- static C,C++类static 成员变量
- C++类static C语言static
- c++-类的static成员
- Objective-C static 类方法
- C++:类内static成员
- static类成员(C++)
- static.c
- c-static
- static C
- C static
- static c
- C static
- C#static
- C Static
- c static
- 238. Product of Array Except Self
- android自定义view-打造圆形ImageView(二)
- String类常用方法总结
- c语言:顺序栈的应用(二) 判断回文数
- Android学习之使用Comparable或Comparator比较和排序元素
- c++ static 类
- Flume中的HDFS Sink配置参数说明
- SharedPreferences
- java集合类总结
- 第一个jsp和servlet留影
- Linux中文显示乱码?如何设置centos显示中文
- vb作业
- 单机环境下的用户授权模块配置
- 第 0000 题