Create a C ++ object Array

Source: Internet
Author: User

 

Use a one-dimensional pointer to create an object array:

 

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

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

}

Result:

 

 

 

> 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

 

Use a two-dimensional pointer to create a dynamic array:

 

 

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

// 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; // This statement does not seem to work.

Cout <"nextStudentID:" <nextStudentID <endl;

Return 0;

}

Result:

 

> 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

~ Student ()

~ StudentID ()

~ Student ()

~ StudentID ()

~ Student ()

~ StudentID ()

NextStudentID: 1

Differences between the two:

 

One-dimensional pointers apply for and create three objects when creating pointers.

 

A two-dimensional pointer creates only one pointer array (3*4 bytes) when creating a pointer. The elements in the array are used to store the addresses of three objects, each object address is created by new. Therefore, the corresponding delete function should be delete p [j], rather than delete [] p.

From Study Notes of sg131971

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.