C语言到C++ 面向过程到面向对象的转变

案例1

#include <iostream>

using namespace std;

class Cube
{
private:
	int m_a;
	int m_b;
	int m_c;
	int m_v;
	int m_s;
public: 
	void setA(int a)
	{
		m_a = a;
	}
	void setB(int b)
	{
		m_b = b;
	}
	void setC(int c)
	{
		m_c = c;
	}
	void setABC(int a,int b,int c)
	{
		m_a = a;
		m_b = b;
		m_c = c;
	}

public:
	int getV()
	{
		m_v = m_a * m_b*m_c;
		return m_v;
	}
	int getS()
	{
		m_s = 2 * (m_a * m_b + m_a*m_c +m_b*m_c);
		return m_s;
	}

public:
	//代码冗余 假装用面向对象的方法,实际上也是面向过程
	int judgeCube(Cube &c1, Cube &c2)
	{
		if (c1.getS() == c2.getS() && c1.getV() == c2.getV())
			return 1;
		else
			return 0;
	}
	//成员函数重载  纯正面向对象的方法
	int judgeCube(Cube &c1)
	{
		if (this->getS() == c1.getS() && this->getV() == c1.getV())
			return 1;
		else
			return 0;
	}


};

//判断两个长方体是否相等
int judgeCube(Cube &c1, Cube &c2)
{
	if (c1.getS() == c2.getS() && c1.getV() == c2.getV())
		return 1;
	else 
		return 0;
}

int main04()
{
	Cube c1,c2;
	c1.setABC(1,2,3);
	c2.setABC(2,2,3);
	cout << "V:" << c1.getV() << endl;
	cout << "S:" << c1.getS() << endl;

	//全局函数调用法
	if (judgeCube(c1, c2))
		cout << "长方体相等" << endl;
	else
		cout << "长方体不相等" << endl;

	//c1.judgeCube(c1, c2) 代码冗余 	
	//if (c1.judgeCube(c1, c2))
	//	cout << "长方体相等" << endl;
	//else
	//	cout << "长方体不相等" << endl;
	if (c1.judgeCube(c2))
		cout << "长方体相等" << endl;
	else
		cout << "长方体不相等" << endl;
	system("pause");
	return 0;
}


案例2

#include <iostream>
using namespace std;
//判断一个点是否在圆内
class MyPoint		//点类
{
public:
	void setPoint(int _x1, int _y1)
	{
		x1 = _x1; y1 = _y1;
	}

	int getx1()
	{
		return x1;
	}

	int gety1()
	{
		return y1;
	}

private:
	int x1;
	int y1;
};

class AdvCircle		//一个圆类
{
public:
	void setCircle(int _r, int _x0, int _y0)
	{
		r = _r; x0 = _x0; y0 = _y0;
	}

public:
	int judge(MyPoint &m1)
	{
		int num;
		num = (m1.getx1() - x0)*(m1.getx1() - x0) + (m1.gety1() - y0)*(m1.gety1() - y0);
		if (num > (r*r))
			return 1;
		else
			return 0;
	}

private:
	int r;
	int x0;
	int y0;
};



int main05()
{
	AdvCircle a1;
	MyPoint m1;

	a1.setCircle(2,3,3);
	m1.setPoint(6,3);
	if (a1.judge(m1))
		cout << "坐标不在圆内" << endl;
	else
		cout << "坐标在圆内" << endl;
	system("pause");
	return 0;
}

练习1


定义一个Point类,其属性包括点的坐标,提供计算两点之间距离的方法
 

/*
定义一个Point类,其属性包括点的坐标,提供计算两点之间距离的方法
*/

#include <iostream>
#include <cmath>

using namespace std;

class Point
{
public:
	void setXY(double _x, double _y)
	{
		x = _x;
		y = _y;
	}

	double getX()
	{
		return x;
	}

	double getY()
	{
		return y;
	}
public:
	double disXY(Point & p)
	{
		double x;
		x = pow(this->x - p.getX(),2) + pow(this->y -p.getY(),2);
		return sqrt(x);
	}

private:
	double x;
	double y;

};


int main06()
{
	Point cp1, cp2;
	double x, y;
	cout << "请输入第一个坐标X,Y:";
	cin >> x;
	cin >> y;
	cp1.setXY(x,y);

	cout << "请输入第二个坐标X,Y:";
	cin >> x;
	cin >> y;
	cp2.setXY(x, y);

	cout << "两点坐标的距离为:" << cp1.disXY(cp2) << endl;

	system("pause");
	return 0;
}

练习2

创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果。

#include <iostream>
#include <cmath>
/*
创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果。
*/
using namespace std;

class Circle		//圆类
{
public:
	void setRXY(int r,int x,int y)
	{
		this->x = x;
		this->y = y;
		this->r = r;
	}

	int getX()
	{
		return x;
	}

	int getY()
	{
		return y;
	}

	int getR()
	{
		return r;
	}

public:
	int CInte(Circle & c1)	//判断两个圆的关系
	{
		double dis = pow(c1.getX() - this->x, 2) +pow(c1.getY()-this->y,2);
		
		if (dis > pow(c1.getR() + this->r,2))
		{
			return 1;	//相离
		}

		else if (dis <= pow(c1.getR() + this->r,2) && dis > pow(c1.getR() - this->r,2))
		{
			return 0;	//相交
		}
		else
			return -1;	//内含
	}

private:
	int x;
	int y;
	int r;
};

int main()
{
	Circle c1, c2;
	int r, x, y;
	cout << "请输入第一个圆的R半径 X坐标 Y坐标:";
	cin >> r >> x >> y;
	c1.setRXY(r, x, y);		//坐标设置
	
	cout << "请输入第二个圆的R半径 X坐标 Y坐标:";
	cin >> r >> x >> y;
	c2.setRXY(r, x, y);

	switch ( c1.CInte(c2) )
	{
		case -1: cout << "内含" << endl; break;
		case  0: cout << "相交" << endl; break;
		case  1: cout << "相离" << endl; break;
	}


	system("pause");
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_36795563/article/details/81429934