南京邮电大学高级程序语言设计
时间: 2023-11-19 20:04:44 浏览: 45
#include<iostream>
#include<string>
using namespace std;
class Student {
private:
int age;
string Name;
public:
Student(int m, string n);
Student();
~Student();
static int count;
void Print() const;
};
int Student::count = 0;
Student::Student(int m, string n) {
age = m;
Name = n;
count++;
}
Student::Student() {
age = 0;
Name = "NoName";
count++;
}
Student::~Student() {
count--;
}
void Student::Print() const {
cout << count << endl;
cout << "Name=" << Name << ", age=" << age << endl;
}
int main() {
cout << "count=" << Student::count << endl;
Student s1, * p = new Student(23, "ZhangHong");
s1.Print();
p->Print();
delete p;
s1.Print();
Student Stu;
cout << "count=" << Student::count << endl;
return 0;
}