C++学习之




对于Array类,含义即建立某个数组,数组类型为T,T可以是任何类,比如之前的Point类(某点的x,y坐标)。






通透的瞬间:Arrayofpoints类的本质:建立一个数组,数组类型为某点的一对坐标(x,y)

*points是对象的数据成员,指向Point类的指针;

构造函数:为points赋予地址,地址的大小为size;

析构函数:删除points的地址

复制构造函数:某对象v,其含有points指针指向的地址和值,以及size大小。首先获取对象v的数据成员size,然后为数据成员points用new分配地址建立数组,数组类型为Point(Point含有数据成员x和y,即某点的的坐标),接着使其地址的值与v相等。相当于通过指针指向的地址为数组赋值。




#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <string>
#include <cassert>
using namespace std;

class Point{
private:
	int x, y;
public:
	static int count;
	Point() :x(0), y(0){
		count++;
		cout << "default Point constructed" << endl;
	}
	Point(int x, int y) :x(x), y(y){
		count++;
		cout << "Point constructed" << endl;
	}
	~Point(){
		count--;
		cout << "Point deconstructed" << endl;
	}
	int getx() const{
		return x;
	}
	int gety() const{
		return y;
	}
	void move(int newx, int newy){
		x = newx;
		y = newy;
	}
};

class Arrayofpoints{
private:
	Point *points;
	int size;
public:
	Arrayofpoints(int size) :size(size){
		points = new Point[size];      //two* "default Point constructed"
		cout << "Arrayofpoints constructed" << endl;
	}
	~Arrayofpoints(){
		cout << "deleting..." << endl;
		delete[] points;
	}
	Arrayofpoints(const Arrayofpoints &v){
		size = v.size;
		points = new Point[size];     //two* "default Point constructed"
		for (int i = 0; i < size; i++){
			points[i] = v.points[i];
		}
		cout << "Arrayofpoints copier called" << endl;
	}
	Point &element(int index){
		assert(index >= 0 && index < size);
		return points[index];
	}
};


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值