声明:
类名 数组名[元素个数];
访问方法: 通过下标访问
数组名[下标].成员名
对象数组初始化:
数组中每一个元素对象被创建时,系统都会调用类构造函数初始化该对象。
通过初始化列表赋值。
例: Point A[2]={Point(1,2),Point(3,4)};
如果没有为数组元素指定显式初始值,数组元素便使用默认值初始化(调用默认构造函数)。
数组元素所属类的构造函数:
不声明构造函数,则采用默认构造函数。 各元素对象的初值要求为相同的值时,可以声明具有默认形参值的构造函数。 各元素对象的初值要求为不同的值时,需要声明带形参的构造函数。 当数组中每一个对象被删除时,系统都要调用一次析构函数。
对象数组应用举例:
//Point.h
#ifndef _POINT_H
#define _POINT_H
class Point { //类的定义
public: //外部接口
Point();
Point(int x, int y);
~Point();
void move(int newX,int newY);
int getX() const { return x; }
int getY() const { return y; }
static void showCount(); //静态函数成员
private: //私有数据成员
int x, y;
};
#endif //_POINT_H
//Point.cpp
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point() {
x = y = 0;
cout << "Default Constructor called." << endl;
}
Point::Point(int x, int y) : x(x), y(y) {
cout << "Constructor called." << endl;
}
Point::~Point() {
cout << "Destructor called." << endl;
}
void Point::move(int newX,int newY) {
cout << "Moving the point to (" << newX << ", " << newY << ")" << endl;
x = newX;
y = newY;
}
//6-3.cpp
#include "Point.h"
#include <iostream>
using namespace std;
int main() {
cout << "Entering main..." << endl;
Point a[2];
for(int i = 0; i < 2; i++)
a[i].move(i + 10, i + 20);
cout << "Exiting main..." << endl;
return 0;
}
本文介绍对象数组的概念及使用方法,包括声明、初始化、访问等,并通过一个具体的Point类实例演示了对象数组如何工作,展示了构造函数与析构函数的作用。
1168

被折叠的 条评论
为什么被折叠?



