版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoxiao133/article/details/83416990
下面是重载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;
}