⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p7_9.cpp

📁 相当丰富的C++源码
💻 CPP
字号:
/*****************************
* p7_9.cpp                   *
* 含有对象计数器的学生类     *
******************************/
#include<iostream> 
using namespace std;
class Student {
     private:
         char *Name;
         int No;
         static int countS;
	 public:
       static int GetCount()
	   {
		   return countS;
	   }
	   Student(char* ="", int=0);
	   Student(Student &);
	   ~Student();
};
Student::Student(char * Name, int No)
{
   this->Name=new char [strlen(Name)+1];
   strcpy(this->Name, Name);
   this->No=No;
   ++countS;
   cout<<"constructing:"<<Name<<endl;
}
Student::Student(Student & r)
{
   Name=new char [strlen(r.Name)+1];
   strcpy(Name, r.Name);
   No=r.No;
   ++countS;
   cout<<"copy constructing:"<<r.Name<<endl;
}
Student::~Student()
{
   cout<<"destructing:"<<Name<<endl;
   delete [] Name;
   --countS;  
}
int Student::countS=0;
void main()
{
	cout<<Student::GetCount()<<endl; //使用类调用静态成员函数
    Student s1("Antony");            //建立一个新对象  
	cout<<s1.GetCount()<<endl;       //通过对象调用静态成员函数
    Student s2(s1);                  //利用已有对象建立一个新对象
	cout<<s1.GetCount()<<endl;
	Student S3[2];                   //建立一个对象数组
	cout<<Student::GetCount()<<endl;
	Student *s4=new Student[3];     //建立一动态对象数组
    cout<<Student::GetCount()<<endl;
	delete [] s4;                   //删除动态对象数组
    cout<<Student::GetCount()<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -