当前位置:编程学习 > C/C++ >>

C++对象数组的创建

 

 使用一维指针创建对象数组:

 

//============================================================================ 

// Name        : main.cpp 

// Author      : ShiGuang 

// Version     : 

// Copyright   : sg131971@qq.com 

// Description : Hello World in C++, Ansi-style 

//============================================================================ 

 

#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; 

结果:

 

 

 

>>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 

 

使用二维指针创建动态数组:

 

 

//============================================================================ 

// Name        : main.cpp 

// Author      : ShiGuang 

// Version     : 

// Copyright   : sg131971@qq.com 

// Description : Hello World in C++, Ansi-style 

//============================================================================ 

 

#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, j; 

    string temp; 

    cin >> i; 

    Student **p = new Student *[i]; 

    for (j = 0; j < i; j++) 

    { 

        cout << "j:" << j << endl; 

        cin >> temp; 

        p[j] = new Student(temp); 

        cout << "nextStudentID:" << nextStudentID << endl; 

    } 

    for (j = i - 1; j >= 0; j--) 

        delete p[j]; 

//  delete[] p; // 这句话好像没作用 

    cout << "nextStudentID:" << nextStudentID << endl; 

    return 0; 

结果:

 

>>3 

j:0 

>>shiguang1 

StudentID() 

value:1 

Student() 

name:shiguang1 

nextStudentID:2 

j:1 

>>shiguang2 

StudentID() 

value:2 

Student() 

name:shiguang2 

nextStudentID:3 

j:2 

>>shiguang3 

StudentID() 

value:3 

Student() 

name:shiguang3 

nextStudentID:4 

<
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,