加入收藏 | 设为首页 | 会员中心 | 我要投稿 文章分享网_茂名站长网 (https://www.0668zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

C++对象数组的打造

发布时间:2021-11-24 13:19:13 所属栏目:教程 来源:互联网
导读:使用一维指针创建对象数组: #include iostream #include string using namespace std; int nextStudentID = 1; class StudentID { public: StudentID() { cout StudentID() endl; value = nextStudentID++; cout value: value endl; } ~StudentID() { --nex

使用一维指针创建对象数组:
 
#include <iostream>   
#include <string>   
using namespace std;  
  
int nextStudentID = 1;  
class StudentID  
{  
public:  
    StudentID()  
    {  
        cout << "StudentID()" << endl;  
        value = nextStudentID++;  
        cout << "value:" << value << endl;  
    }  
    ~StudentID()  
    {  
        --nextStudentID;  
        cout << "~StudentID()" << endl;  
    }  
protected:  
    int value;  
};  
  
class Student  
{  
public:  
    Student(string pName = "noName")  
    {  
        cout << "Student()" << endl;  
        name = pName;  
        cout << "name:" << name << endl;  
    }  
    ~Student()  
    {  
        cout << "~Student()" << endl;  
    }  
protected:  
    string name;  
    StudentID id;  
};  
  
int main(int argc, char **argv)  
{  
    int i;  
    cin >> i;  
    Student *p = new Student [i];  
    delete[] p;  
    cout << "nextStudentID:" << nextStudentID << endl;  
    return 0;  
}  
结果:
 
 
[cpp]
>>3  
StudentID()  
value:1  
Student()  
name:noName  
StudentID()  
value:2  
Student()  
name:noName  
StudentID()  
value:3  
Student()  
name:noName  
~Student()  
~StudentID()  
~Student()  
~StudentID()  
~Student()  
~StudentID()  
nextStudentID:1  
 

(编辑:文章分享网_茂名站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读