C + + object array

Time:2022-5-8

The following essay gives the key points of C + + object array.

Definition and access of object array

  • Define an array of objects

Class name array name[Number of elements]

  • Access object array elements

Access by subscript

Array name[subscript].member name

Object array initialization

  • When each element object in the array is created, the system will call the class constructor to initialize the object.

  • Assign values by initializing the list.

examplePoint a[2]={Point(1,2),Point(3,4)};

  • If no explicit initial value is specified for the array element, the array element is initialized with the default value (calling the default constructor).

Constructor of the class to which the array element belongs

  • If the class to which the element belongs does not declare a constructor, the default constructor is used.

  • When the initial value of each element object is required to be the same value, the constructor with the default formal parameter value can be declared.

  • When the initial value of each element object requires different values, you need to declare a constructor with formal parameters.

  • When each object in the array is deleted, the system calls the destructor once.

ExampleApplication example of object array

1 //Point.h
 2 
 3 #ifndef _POINT_H
 4 
 5 #define _POINT_H
 6 
 7 class point {// class definition
 8 
 9 public: / external interface
10 
11      Point();
12 
13     Point(int x, int y);
14 
15     ~Point();
16 
17     void move(int newX,int newY);
18 
19     int getX() const { return x; }
20 
21     int getY() const { return y; }
22 
23     static void showCount(); // Static function member
24 
25 private: / private data member
26 
27     int x, y;
28 
29 };
30 
31 #endif //_POINT_H
1 //Point.cpp
 2 
 3 #include 
 4 
 5 #include "Point.h"
 6 
 7 using namespace std;
 8 
 9 Point::Point() : x(0), y(0) {
10 
11   cout << "Default Constructor called." << endl;
12 
13 }
14 
15 Point::Point(int x, int y) : x(x), y(y) {
16 
17   cout << "Constructor called." << endl;
18 
19 }
20 
21 Point::~Point() {
22 
23   cout << "Destructor called." << endl;
24 
25 }
26 
27 void Point::move(int newX,int newY) {
28 
29   cout << "Moving the point to (" << newX << ", " << newY << ")" << endl;
30 
31   x = newX;
32 
33   y = newY;
34 
35 }
1 //sample.cpp
 2 
 3 #include "Point.h"
 4 
 5 #include 
 6 
 7 using namespace std;
 8 
 9 int main() {
10 
11   cout << "Entering main..." << endl;
12 
13   Point a[2];
14 
15   for(int i = 0; i < 2; i++)
16 
17      a[i].move(i + 10, i + 20);
18 
19   cout << "Exiting main..." << endl;
20 
21   return 0;
22 
23 }
1. Operation results:
2 Entering main...
3 Default Constructor called.
4 Default Constructor called.
5 Moving the point to (10,20)
6 Moving the point to (11,21)
7 Exiting main...
8 Destructor called.
9 Destructor called.

 

Recommended Today

07-RDD of Spark

1: Design Ideas of Distributed Programs step1:read data Input In the code: To specify the location to read the data file, and then return a variable representing the input data Divide the data to be processed into N parts, each of which is placed on a different machine It is equivalent to taking a big […]