指针使用.cpp

来自「c++实例~ 初学基础」· C++ 代码 · 共 79 行

CPP
79
字号
// 代码片段1
#include <iostream.h>

class M
{
public:
	M() { x=y=0; }
	M(int i, int j) { x=i; y=j; }
	void copy(M *m);
	void setxy(int i, int j) { x=i; y=j; }
	void print() { cout<<x<<","<<y<<endl; }
private:
	int x, y;
};

void M::copy(M *m)
{
	x=m->x;
	y=m->y;
}

void fun(M m1, M *m2);

void fun(M m1, M *m2)
{
	m1.setxy(12, 15);
	m2->setxy(22,25);
}

void main()
{
	M p(5, 7), q;
	q.copy(&p);
	fun(p, &q);
	p.print();
	q.print();
}


// 代码片段2
#include <iostream.h>

class M
{
public:
	M() { x=y=0; }
	M(int i, int j) { x=i; y=j; }
	void copy(M &m);
	void setxy(int i, int j) { x=i; y=j; }
	void print() {cout<<x<<","<<y<<endl; }
private:
	int x, y;
};

void M::copy(M &m)
{
	x=m.x;
	x=m.y;
}

void fun(M m1, M &m2);

void main()
{
	M p(5, 7), q;
	q.copy(p);
	fun(p, q);
	p.print();
	q.print();
}
void fun(M m1, M &m2)
{
	m1.setxy(12, 15);
	m2.setxy(22, 25);
}



⌨️ 快捷键说明

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