1. 程式人生 > >C語言到C++ 面向過程到面向物件的轉變

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;
}