对象指针
this指针
当前对象!
动态创建对象
new和delete (与c语言中的malloc和free一样)
申请和释放动态数组(对象数组)
作业
#include<iostream>
using namespace std;
class Rectangle{
private:
int width, height;
static int count;
public:
Rectangle();//无参构造函数
Rectangle(int newwidth,int newheight);//有参构造函数
~Rectangle();
static void getCount(){cout<<"矩形个数:"<<count<<endl;}
//内联函数,设置属性的值,一般只有这种很短的函数才可以写在定义中
//this 表示的是当前对象的意思
void setWidth(int width)
{
this->width = width;
}
void setHeight(int height)
{
this->height = height;
}
//获得属性的值
int getWidth()
{
return width;
}
int getHeight()
{
return height;
}
int getArea(){
int area = this->width * this->height;
return area;
}
int getPerimeter(){
int Perimeter = (this->width + this->height)*2;
return Perimeter;
}
};
int Rectangle::count = 0;
Rectangle::Rectangle(){
this->width = 1;
this->height = 1;
cout<<"创建矩形"<<endl;
count++;
}
Rectangle::Rectangle(int newwidth,int newheight){
this->width = newwidth;
this->height = newheight;
cout<<"创建矩形"<<endl;
count++;
}
Rectangle::~Rectangle(){
cout<<"消除矩形"<<endl;
count--;
}
int main(){
Rectangle rect[3] = {Rectangle(3,4),Rectangle(7,6),Rectangle(5,6)};
for(int i = 0;i<3;i++){
int W1 = rect[i].getWidth();
int H1 = rect[i].getHeight();
int A1 = rect[i].getArea();
int P1 = rect[i].getPerimeter();
cout<<"矩形的长为:"<<W1<<endl<<"矩形的宽为:"<<H1<<endl<<"矩形的面积为:"<<A1<<endl<<"矩形的周长为:"<<P1<<endl;
}
}
运行结果:
创建矩形
创建矩形
创建矩形
矩形的长为:3
矩形的宽为:4
矩形的面积为:12
矩形的周长为:14
矩形的长为:7
矩形的宽为:6
矩形的面积为:42
矩形的周长为:26
矩形的长为:5
矩形的宽为:6
矩形的面积为:30
矩形的周长为:22
消除矩形
消除矩形
消除矩形
对象的生成和消亡都由系统自己决定,如果使用new动态生成,一定要delete去除;
指针数组是存放在栈里面的,main结束了那么就清除了;
而对象是存放在堆里面的,main结束了如果没有及时去delete掉,那么他们就会失去指向,变成孤儿;
#include<iostream>
using namespace std;
class Rectangle{
private:
int width, height;
static int count;
public:
Rectangle();//无参构造函数
Rectangle(int width,int height);//有参构造函数
~Rectangle();
static void getCount(){cout<<"矩形个数:"<<count<<endl;}
//内联函数,设置属性的值,一般只有这种很短的函数才可以写在定义中
//this 表示的是当前对象的意思
void setWidth(int width)
{
this->width = width;
}
void setHeight(int height)
{
this->height = height;
}
//获得属性的值
int getWidth()
{
return width;
}
int getHeight()
{
return height;
}
int getArea(){
int area = this->width * this->height;
return area;
}
int getPerimeter(){
int Perimeter = (this->width + this->height)*2;
return Perimeter;
}
};
int Rectangle::count = 0;
Rectangle::Rectangle(){
this->width = 1;
this->height = 1;
cout<<"创建矩形"<<endl;
count++;
}
Rectangle::Rectangle(int width,int height){
this->width = width;
this->height = height;
cout<<"创建矩形"<<endl;
count++;
}
Rectangle::~Rectangle(){
cout<<"消除矩形"<<endl;
count--;
}
int main(){
const int N = 3;
Rectangle *pRctangle[N];
int width,height;
for(int i = 0;i<N;i++){
cout<<"请输入矩形"<<i+1<<"的长和宽"<<endl;
cin>>width>>height;
pRctangle[i] = new Rectangle(width,height);
int W1 = pRctangle[i]->getWidth();
int H1 = pRctangle[i]->getHeight();
int A1 = pRctangle[i]->getArea();
int P1 = pRctangle[i]->getPerimeter();
cout<<"矩形的长为:"<<W1<<endl<<"矩形的宽为:"<<H1<<endl<<"矩形的面积为:"<<A1<<endl<<"矩形的周长为:"<<P1<<endl;
}
for(int i = 0;i<N;i++){
delete pRctangle[i];
}
}
运行结果:
请输入矩形1的长和宽
3 4
创建矩形
矩形的长为:3
矩形的宽为:4
矩形的面积为:12
矩形的周长为:14
请输入矩形2的长和宽
5 6
创建矩形
矩形的长为:5
矩形的宽为:6
矩形的面积为:30
矩形的周长为:22
请输入矩形3的长和宽
7 8
创建矩形
矩形的长为:7
矩形的宽为:8
矩形的面积为:56
矩形的周长为:30
消除矩形
消除矩形
消除矩形
思考:为什么不能用delete [] Rectangle;?
Rectangle是静态创建的,不是动态创建的,这种方式只能删除静态创建的
用了几次new,就用几次delete。