#include<iostream.h> #include<string.h> class student { private: int sid; char name[20]; int age; int score; const int uid; //声名:uid为常量,声名时不能初始化 static int count; //声名:count为静态成员,必须在类的内部声名,外部初始化 public: student():uid(61007) //构造函数----初始化列表中进行初始化 { cout<<"开始一个构造函数!"<<endl; this->sid=10011; strcpy(this->name,"张三"); this->age=28; this->score=100; count++; //构造、增加记数 } ~student() //析构函数 { cout<<"析构这个函数"<<endl; count--; //析构、减记数 } void intput() { cout<<"请输入一个学生的信息:"<<endl; cout<<"sid= "; cin>>this->sid; cout<<"name="; cin>>this->name; cout<<"age= "; cin>>this->age; cout<<"score= "; cin>>this->score; } void output() { cout<<"你输入的学生信息:"<<endl; cout<<"sid="<<this->sid<<endl; cout<<"name="<<this->name<<endl; cout<<"age="<<this->age<<endl; cout<<"score="<<this->score<<endl; cout<<"uid= "<<this->uid<<endl; cout<<"count= "<<this->count<<endl; } }; int student::count=0; //对静态成员变量 count 初始化 void main() { student st1; student st2; student st3; //st1.intput(); st1.output(); } #include <iostream.h> class people { private: int a; int b; public: people(int a,int b) { cout<<"调用people带参数的构造函数"<<endl; } people() { cout<<"调用people无参数的构造函数"<<endl; } ~people() { cout<<"析构people函数!"<<endl; } }; class teacher :public people { private: int c; int d; public: teacher() { cout<<"调用teacher无参数的构造函数"<<endl; } teacher(int m_a,int m_b,int c,int d) :people(m_a,m_b) { cout<<"调用teacher带参数的构造函数"<<endl; } ~teacher() { cout<<"析构teacher函数!"<<endl; } }; class teacher1:public teacher { private: int e; int f; public: teacher1(int a,int b,int c,int d,int e,int f) :teacher(a,b,c,d) { cout<<"调用teacher1带参数的构造函数"<<endl; } teacher1() { cout<<"调用teacher1无参数的构造函数"<<endl; } ~teacher1() { cout<<"析构teacher1函数!"<<endl; } }; void main() { int a,b,c,d,e,f; a=1; b=2; c=3; d=4; e=5; f=6; teacher1 t1(a,b,c,d,e,f); //cout<<t1<<endl; // cout<<endl; teacher1 t2; //cout<<t2<<endl; } #include<iostream.h> #include<string.h> class student { private: int sid; char name[20]; char sex; int age; const double english; protected: float score; public: static int cut; static void count() { cut++; } student(int id,char* pname,char psex,int page,float core):english(58.6),age(23) { sid=id; strcpy(name,pname); sex=psex; // age=page; score=core; count(); } ~student() { } void input() { cout<<"请输入一个学生信息!!!"<<endl; cout<<"sid= "; cin>>sid; cout<<"name= "; cin>>name; cout<<"sex= "; cin>>sex; cout<<"age= "; cin>>age; cout<<"score="; cin>>score; } void output() { cout<<"学生信息为:"<<endl; cout<<"english= "<<this->english<<endl; cout<<"sid= "<<this->sid<<endl; cout<<"name= "<<this->name<<endl; cout<<"sex= "<<this->sex<<endl; cout<<"age= "<<this->age<<endl; cout<<"score= "<<this->score<<endl; } }; int student::cut=0; void main() { student st(1001,"wang",'M',25,87); st.input(); st.output(); student* ps = new student(1001,"wang",'M',25,87); ps->input(); ps->output(); cout<<endl; student*psp=new student(1002,"zhang",'F',26,85); psp->output (); cout<<student::cut ; cout<<endl; } #include<iostream.h> #include<string.h> class student { private: int sid; char name[20]; int age; float score; protected: const int uid; public: static int count; void input() { cout<<"请输入一个学生的信息: "<<endl; cout<<"sid = "; cin>>sid; cout<<"name = "; cin>>name; cout<<"age = "; cin>>age; cout<<"score = "; cin>>score; } void output() { cout<<"学生信息: "<<endl; cout<<"sid = "<<sid<<endl; cout<<"name = "<<name<<endl; cout<<"age = "<<age<<endl; cout<<"score = "<<score<<endl; } student():uid(3961) { cout<<"开始创建一个student"<<endl; this->sid=0; strcpy(this->name,"张三"); this->age =20; this->score =100; count++; } ~student() { cout<<"开始析构一个对象"<<endl; count--; } }; int student ::count = 0; void main() { student st1; student st2; student st3; /*int *p = new int[3]; delete[] p; student *ps; ps =new student[3]; for(int i = 0;i<3;i++) { (ps+i)->output(); } delete[] ps;*/ cout<<student.count<<endl; }