⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 01.cpp

📁 通过重载运算符new和delete可以克服new和delete的不足
💻 CPP
字号:
#include<iostream.h>
#include<malloc.h>

class Rect
{
	int length,width;
public:
	Rect (int l,int w)
	{
		length=l,width=w;
	}
	void disp()
	{
		cout <<"面积"<<length*width <<endl;
	}
	void *operator new (size_t size)
	{
		return malloc(size);
	}
	void operator delete (void *p)
	{
		free (p);
	}
};

void main()
{
	Rect *p;
	int l,w;
	cout<<"Enter the length:";
	cin>>l;
	cout<<"enter the width:";
	cin>>w;
	p=new Rect (l,w);
	p->disp();
	delete p;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -