面向對象編程示例:求周長和麵積 Point.h Point.cpp ...
面向對象編程示例:求周長和麵積
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//圓的周長
double getCircleGirth(double r)
{
return 2 * 3.14*r;
}
//源的面積
double getCircleArea(double r)
{
return 3.14*r*r;
}
//用面向對象實現
//圓類
class Circle
{
public:
void setR(double r)
{
m_r = r;
}
double getR()
{
return m_r;
}
double getGirth()
{
return 2 * 3.14 *m_r;
}
double getArea()
{
return m_r*m_r*3.14;
}
private:
double m_r; //圓的私有成員 半徑
};
class Circle2
{
public:
void setR(double r)
{
m_r = r;
}
double getR()
{
return m_r;
}
double getArea()
{
m_area = m_r*m_r*3.14;
return m_area;
}
double getGirth()
{
m_girth = m_r * 2 * 3.14;
return m_girth;
}
private:
double m_r;
double m_girth; //周長
double m_area;//面積
};
int main(void)
{
double r = 10; //圓的半徑
double g = 0;
double a = 0;
g = getCircleGirth(r);
a = getCircleArea(r);
cout << "圓的半徑是" << r << endl;
cout << "圓的周長是" << g << endl;
cout << "圓的面積是" << a << endl;
cout << "------" << endl;
Circle c;
c.setR(10);
cout << "圓的半徑是" << c.getR() << endl;
cout << "圓的周長是" << c.getGirth() << endl;
cout << "圓的面積是" << c.getArea() << endl;
cout << "------------" << endl;
Circle2 c2;
c2.setR(10);
cout << "圓的半徑是" << c2.getR() << endl;
cout << "圓的周長是" << c2.getGirth() << endl;
cout << "圓的面積是" << c2.getArea() << endl;
return 0;
}
上面那個多文件形式(模塊化?(^▽^))
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Circle.h"
using namespace std;
int main(void)
{
Circle c;
c.setR(10);
cout << "Ãæ»ý" << c.getArea() << endl;
return 0;
}
Circle.h
#pragma once
#if 0
#ifndef __CIRCLE_H_
#define __CIRCLE_H_
#endif
#endif
class Circle
{
public:
//ÉèÖð뾶£º
void setR(double r);
//µÃµ½°ë¾¶
double getR();
double getArea();
double getGirth();
private:
double m_r;
double m_area;
double m_girth;
};
Circle.cpp
#include "Circle.h"
void Circle::setR(double r)
{
m_r = r;
}
double Circle::getR()
{
return m_r;
}
double Circle::getArea()
{
m_area = m_r *m_r *3.14;
return m_area;
}
double Circle::getGirth()
{
m_girth = m_r * 2 * 3.14;
return m_girth;
}
示例:判斷立方體相等
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//立方體類
class Cube
{
public:
void setABC(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
int getArea()
{
return (m_a*m_b) * 2 + (m_a*m_c) * 2 + (m_b*m_c) * 2;
}
int getVolume()
{
return (m_a*m_b*m_c);
}
int getA()
{
return m_a;
}
int getB()
{
return m_b;
}
int getC()
{
return m_c;
}
//同類之間無私處
bool judgeCube(Cube &another)
{
if (m_a == another.m_a &&
m_b == another.getB() &&
m_c == another.getC()) {
return true;
}
else {
return false;
}
}
private:
int m_a;
int m_b;
int m_c;
};
//全局函數
bool judgeCube(Cube &c1, Cube &c2)
{
if (c1.getA() == c2.getA() &&
c1.getB() == c2.getB() &&
c1.getC() == c2.getC()) {
return true;
}
else {
return false;
}
}
int main(void)
{
Cube c1;
c1.setABC(10, 20, 30);
Cube c2;
c2.setABC(10, 20, 30);
cout << "c1 的體積是" << c1.getVolume() << endl;
cout << "c1 的面積是" << c1.getArea() << endl;
if (judgeCube(c1, c2) == true) {
cout << "相等" << endl;
}
else {
cout << "不相等" << endl;
}
cout << " ------ " << endl;
if (c1.judgeCube(c2) == true) {
cout << "相等" << endl;
}
else {
cout << "不相等" << endl;
}
return 0;
}
示例:求點是否在圓內
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//點類
class Point
{
public:
void setXY(int x, int y)
{
m_x = x;
m_y = y;
}
int getX()
{
return m_x;
}
int getY()
{
return m_y;
}
private:
int m_x;
int m_y;
};
//圓類
class Circle
{
public:
void setXY(int x, int y)
{
x0 = x;
y0 = y;
}
void setR(int r)
{
m_r = r;
}
//提供一個判斷點是否在圓內
//true 在內部
//false 在外部
bool judgePoint(Point &p)
{
int dd;
dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0);
if (dd > m_r*m_r) {
return false;
}
else {
return true;
}
}
private:
int x0;
int y0;
int m_r;
};
int main(void)
{
Circle c;
c.setXY(2, 2);
c.setR(4);
Point p;
p.setXY(8, 8);
if (c.judgePoint(p) == true) {
cout << "圓的內部" << endl;
}
else {
cout << "圓的外部" << endl;
}
return 0;
}
上面代碼的文件形式
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Circle.h"
#include "Point.h"
using namespace std;
int main(void)
{
Circle c;
c.setR(4);
c.setXY(2, 2);
Point p;
p.setXY(8, 8);
if (c.judgePoint(p) == true) {
cout << "nei" << endl;
}
else {
cout << "wai" << endl;
}
return 0;
}
Circle.h
#pragma once
#include "Point.h"
class Circle
{
public:
void setXY(int x, int y);
void setR(int r);
//提供一個判斷點是否在圓內
//true 在內部
//false 在外部
bool judgePoint(Point &p);
private:
int x0;
int y0;
int m_r;
};
Circle.cpp
#include "Circle.h"
void Circle::setXY(int x, int y)
{
x0 = x;
y0 = y;
}
void Circle::setR(int r)
{
m_r = r;
}
//提供一個判斷點是否在圓內
//true 在內部
//false 在外部
bool Circle::judgePoint(Point &p)
{
int dd;
dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0);
if (dd > m_r*m_r) {
return false;
}
else {
return true;
}
}
Point.h
#pragma once
class Point
{
public:
void setXY(int x, int y);
int getX();
int getY();
private:
int m_x;
int m_y;
};
Point.cpp
#include "Point.h"
void Point::setXY(int x, int y)
{
m_x = x;
m_y = y;
}
int Point::getX()
{
return m_x;
}
int Point::getY()
{
return m_y;
}