动态数组类
如果我们的类中需要使用动态申请的内存空间,并且该空间是依附于对象的,我们一般在该类的构造函
#include<iostream>
#include<cassert>
using namespace std;
class Point{
public:
Point():x(0),y(0){
cout<<"Default Constructor called."<<endl;
}
Point(int x,int y):x(x),y(y){
cout<<"Constructor called."<<endl;
}
~Point(){cout<<"Destructor called."<<endl;}
int getX() const{return x;}
int getY() const{return y;}
void move(int newX,int newY){
x=newX;
y=newY;
}
private:
int x,y;
};
//动态数组类
class ArrayOfPoints{
public:
ArrayOfPoints(int size):size(size){
points=new Point[size];
}
~ArrayOfPoints(){
cout<<"Deleting..."<<endl;
delete[]points;
}
//获得下标为index的数组元素
Point &element(int index){
assert(index>=0 && index<size);//如果数组下标不会越界,程序中止
return points[index];//*(points+index)
}
private:
Point *points;
int size;
};
int main(){
int count;
cout<<"Please enter the count of points:";
cin>>count;
ArrayOfPoints points(count);
points.element(0).move(5,10);//通过访问数组元素的成员
points.element(1).move(15,20);//通过类访问数组元素的成员
return 0;
}
数中去申请空间(new),在该类的析构函数中释放空间(delete).
动态申请多维数组(难点)
int *p=new int [6];
可以申请一个二维数组吗?
int *p=new int [2][3];
int *p=new int[2];
//int *p=new int [2][3];
在于他们返回的指针类型是不同的
int(*p)[3]=new int[2][3];
p=new int[size][3];
只有 第一维的容量可以使用变量,其他维度必须使用常量.
#include<iostream>
using namespace std;
int main(){
//int *p=new int[2];
//int *p=new int[2][3];
int (*p)[3];
int size=5;
p=new int[size][3];
//*(*(p+i)+j)
//p[i][j]
delete[]p;
}
#include<iostream>
using namespace std;
int main(){
float(*cp)[9][8]=new float[8][9][8];
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
for(int k=0;k<8;k++)
//以指针形式数组元素
*(*(*(cp+i)+j)+k)=static cast<float>(i*100+j*10+k);
for(int i=0;i<8;i++){
for(int j=0;j<9;j++){
for(int k=0;k<8;k++)
//将指针cp作为数组名使用,通过数组名和下标访问数组元素
cout<<cp[i][j][k]<<" ";
cout<<endl;
}
cout<<endl;
}
delete[] cp;
return 0;
}
vector简介
C++标准库中封装好的一个动态数组类
大致的用法:
#include<iostream>
#include<vector>
using namespace std;
int main(){
int b[]={3,4,5};
vector<int>a(b,b+3);
cout<<a.size()<<endl;
a.push_back(6);
cout<<a[0]<<endl;
cout<<a.size()<<endl;
}
#include<iostream>
#include<vector>
using namespace std;
//计算数组arr中元素的平均值
double average(const vector<double>&arr){
double sum=0;
for(unsigned i=0;i<arr.size();i++)
sum+=arr[i];
return sum/arr.size();
}
int main(){
unsigned n;
cout<<"n=";
cin>>n;
vector<double> arr(n);//创建数组对象
cout<<"Please input"<<n<<"real numbers:"<<endl;
for(unsigned i=0;i<n;i++)
cin>>arr[i];//运算符重载
cout<<"Average="<<average(arr)<<endl;
return 0;
}
深复制与浅复制(重点)
当类的成员中含有指针数据成员的时候,通过复制构造来创建新的对象可能会遇到问题.为什么?使用默认的复制构造函数,完成的是无修改的复制,会导致不同对象的指针成员指向同一块内存空间(往往和实际需求不符),这种复制,我们称其为浅复制.
如何解决这个问题?
使用深复制,自定义复制构造函数,控制复制构造的过程.
#include<iostream>
#include<cassert>
using namespace std;
class Point{
public:
Point():x(0),y(0){
cout<<"Point"<<endl;
}
Point(int x,int y):x(x),y(y){
cout<<"Point"<<endl;
}
~Point(){cout<<"~Point"<<endl;}
int getX()const{return x;}
int getY()const{return y;}
void move(int newX,int newY){
x=newX;
y=newY;
}
private:
int x,y;
};
//动态数组类
class ArrayOfPoints{
public:
ArrayOfPoints(int size):size(size){
cout<<"ArrayOfPoints"<<endl;
points=new Points[size];
}
ArrayOfPoints(ArrayOfPoints &aps){
cout<<"ArrayOfPoints Copy"<<endl;
size=aps.size;
points=new Point[size];
for(int i=0;i<size;i++){
points[i]=aps.points[i];
}
}
~ArrayOfPoints(){
cout<<"Deleting..."<<endl;
delete[]points;
cout<<"~ArrayOfPoints"<<endl;
}
//获得下标为index的数组元素
Point &element(int index){
assert(index>=0 && index<isze);
return points[index];//*(points+index)
}
private:
Point *points;//指向动态数组首地址
int size;
};
int main(){
int count=2;
//cout<<"Please enter the count of points:";
//cin>>count;
ArrayOfPoints points1(count);//创建对象数组
points1.element(0).move(5,10);//通过访问数组元素的成员
points1.element(1).move(15,20);//通过类访问数组元素的成员
ArrayOfPoints points2(points2);
return 0;
}
#include<iostream>
#include<cassert>
using namespace std;
class Point{
public:
Point():x(0),y(0){
cout<<"Default Constructor called."<<endl;
}
POint(int x,int y):x(x),y(y){
cout<<"Constructor called."<<endl;
}
~Point(){cout<<"Destructor called."<<endl;}
int getX()const{return x;}
int getY()const{return y;}
void move(int newX,newY){
x=newX;
y=newY;
}
private:
int x,y;
};
//动态数组类
class ArrayOfPoints{
public:
ArrayOfPoints(int size):size(size){
points=new Point[size];
}
~ArrayOfPoints(){
cout<<"Deleting..."<<endl;
delete[]points;
}
//获得下标为index的数组元素
Point &element(int index){
assert(index>=0 && index<size);//若果数组下标不会越界,程序中止
return points[index];
}
private:
Point *points;//指向动态数组首地址
int size;//数组大小
};
int main(){
int count;
cout>>"Please enter the count of points:";
cin>>count;
ArrayOfPoints pointsArray1(count);//创建对象数组
pointsArray1.element(0).move(5,10);
pointsArray1.element(1).move(15,20);
ArrayOfPoints pointsArray2=pointsArray1;//创建对象数组副本
cout<<"Copy of pointsArray1:"<<endl;
cout<<"Points_0 of array2:"<<pointsArray2.element(0).getX()<<","<<pointsArray2.element(0).getY()<<endl;
cout<<"Points_1 of array2:"<<pointsArray2.element(0).getX()<<","<<pointsArray2.element(1).getY()<<endl;
pointsArray1.element(0).move(25,30);
pointsArray1.element(1).move(35,40);
cout<<"After the moving of pointsArray1:"<<endl;
cout<<"Points_0 of array2:"<<pointsArray2.element(0).getX()<<","<<pointsArray2.element(0).getY()<<endl;
cout<<"Points_1 of array2:"<<pointsArray2.element(0).getX()<<","<<pointsArray2.element(1).getY()<<endl
return 0;
}
动态数组类:简单版
#include<iostream>
#include<cassert>
using namespace std;
class Point{
public:
Point():x(0),y(0){
cout<<"Default Constructor called."<<endl;
}
Point(int x,int y):x(x),y(y){
cout<<"Constructor called."<<endl;
}
~Point(){cout<<"Destructor called."<<endl;}
int getX()const{return x;}
int getY()const{return y;}
voidmove(int newX,int newY){
x=newX;
y=newY;
}
private:
int x,y;
};
//动态数组类
class ArrayOfPoints{
public:
ArrayOfPoints(int size):size(size){
poinnts=new Point[size];
}
~ArrayOfPoints(){
cout<<"Deleting..."<<endl;
delete[]points;
}
//获得下标为index的数组元素
Point &element(int index){
assert(index>=0 && index<size);//如果数组下标不会越界,程序中止
return points[index];
}
private:
Point *points;//指向动态数组首地址
int size;//数组大小
};
int main(){
int count;
cout<<"Please enter the count of points:";
cin>>count;
ArrayOfPoints points(count);//创建对象数组
points.element(0).move(5,10);//通过访问数组元素的元素
points.element(1).move(15,20);//通过类访问数组元素的元素
return 0;
}