【C++重载new】重载new以及namespace使用

6 篇文章 0 订阅

下面是重载Class内部以及全局的new代码。

class fileClass{
public:
	int a;
	int b;
	fileClass(){
		a = 0;
		b = 0;
	}
	fileClass(int aa,int bb):a(aa),b(bb){}
	void* operator new(size_t size)
	{
		cout << "new : " << size << endl;
		return malloc(size);
	}
};

static void* operator new(size_t size)
{
	cout << "new all : " << size << endl;
	return malloc(size);
}

namespace使用

namespace TV
{
	int a = 100;
	double b = 111;
	void fun(){cout << "namespace TV." << endl;}
}

using namespace TV;
fun();

c++ 测试代码

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Point
{
public:
	int x,y;
	Point(int xx, int yy):x(xx),y(yy){}
	virtual void display() const {cout << "[" << x << "," << y << "]" <<endl;}
	
	friend ostream &operator<< (ostream &output,const Point &p)
	{
		output << "[" << p.x << "," << p.y << "]" <<endl;
		return output;
	}
	Point operator++(int)
	{
		Point temp = *this;
		(this->x)++;
		(this->y)++;
		return temp;
	}
	friend Point operator*(const Point &p1,const Point &p2)
	{
		return Point(p1.x*p2.x,p1.y*p2.y);
	}
private:
	virtual void display1() const {cout << "[" << x << "," << y << "]" <<endl;}
};

class PointV : public Point
{
public:
	int V;
	PointV(int xx,int yy,int vv = 0):Point(xx,yy)
	{
		V = vv;
	}
	virtual void display() const {cout << "[" << x << "," << y << "," << V << "]" <<endl;}
	virtual void display1() const {cout << "[" << x << "," << y << "," << V << "]" <<endl;}
};

class fileClass{
public:
	int a;
	int b;
	fileClass(){
		a = 0;
		b = 0;
	}
	fileClass(int aa,int bb):a(aa),b(bb){}
	void* operator new(size_t size)
	{
		cout << "new : " << size << endl;
		return malloc(size);
	}
};

static void* operator new(size_t size)
{
	cout << "new all : " << size << endl;
	return malloc(size);
}

namespace TV
{
	int a = 100;
	double b = 111;
	void fun(){cout << "namespace TV." << endl;}
}

void matAB(double **A, double **B,
	double **C, int *sizeAB)
{
	int m = sizeAB[0], n = sizeAB[1], p = sizeAB[2];
	for (int i = 0;i < m;i++)
	{
		for (int j = 0;j < p;j++)
		{
			for (int k = 0;k < n;k++)
			{
				C[i][j] += A[i][k]*B[k][j];
			}
		}
	}
}

int main4()
{
	Point myP(1,1), *pPoint = NULL;
	PointV myV(11,22);
	myV++;
	pPoint = &myP;
	pPoint = &myV;
	pPoint->display();
	myP = myV;
	cout << myV << endl;
	myP.display();
	myP++;
	myP = myP*myP;
	cout << myP << endl;
	ofstream outfile;
	outfile.open("xiao.data",ios::binary);
	if (!outfile)
	{
		cerr << "eeee";
	}
	outfile << "xiaogongwei123" << endl;
	outfile << 12122 << endl;
	outfile.close();
	//char linstr[10] = {0};
	string linstr;
	ifstream infile("xiao.data",ios::in|ios::_Nocreate);
	infile >> linstr;
	cout << linstr << endl;
	infile >> linstr;
	cout << linstr << endl;
	infile.close();
	using namespace TV;
	fun();
	/*char *p = "123456";
	cout << *++p << endl;*/
	fileClass *pfileC = new fileClass;
	int m = 4, n = 3, p = 2;
	double **A = (double **)malloc(m*sizeof(double *)),**B = (double **)malloc(n*sizeof(double *)),
		**C = (double **)malloc(m*sizeof(double *));
	int sizeAB[3] = {4, 3, 2};
	for (int i = 0;i < m;i++)
	{
		A[i] = (double *)malloc(n*sizeof(double));
	}
	for (int i = 0;i < n;i++)
	{
		B[i] = (double *)malloc(n*sizeof(double));
	}
	for (int i = 0;i < m;i++)
	{
		C[i] = (double *)malloc( p * sizeof(double) );
	}
	for (int i = 0;i < m;i++)
	{
		for (int j = 0;j < n;j++)
		{
			A[i][j] = 1;
		}
	}
	for (int i = 0;i < n;i++)
	{
		for (int j = 0;j < p;j++)
		{
			B[i][j] = 1;
		}
	}
	for (int i = 0;i < m;i++)
	{
		for (int j = 0;j < p;j++)
		{
			C[i][j] = 0;
		}
	}


	matAB(A, B, C, sizeAB);

	for (int i = 0;i < m;i++)
	{
		for (int j = 0;j < p;j++)
		{
			cout << C[i][j] << ",";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肖恭伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值