c++-面向对象类的示例-求周长面积,判断体积相等-文件操作和一般操作
面向对象编程示例:求周长和面积
#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;
}
♪(^∇^*)♪(^∇^*)(~ ̄▽ ̄)~有没有感觉很棒呀!!!(#^.^#),(*^▽^*)O(∩_∩)O哈哈~