Introduction to class and object
cugwhp opened this issue · comments
Introduction to class and object
1. 从面向过程到面向对象
以计算三角形的边长和面积为例,用代码的实现,演进面向过程与面向对象的区别。
- 按照题目要求,顺序写出程序如下所示。
// main.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a, b, c;
cin >> a >> b >> c;
if (a+b>c && a+c>b && b+c>a && a>0 && b>0 && c>0)
{
float fPerimeter = a+b+c;
cout << "Perimeter is " << fPerimeter << endl;
fPerimeter /= 2.;
float fArea = sqrt(fPerimeter*(fPerimeter-a)*(fPerimeter-b)*(fPerimeter-c));
cout << "Area is " << fArea << endl;
}
else
{
cerr << "Invalidate Triangle." << endl;
}
}
- 如果程序改为有若干个三角形,要统计边长和数组,上述的代码的复用就显得比较麻烦。此时,我们考虑到函数的封装性,将若干条语句封装到一起,构成一个具有一定独立功能的函数,便是函数。
分析上述的代码,我们可以归纳出3个函数:
- 判断边长a,b,c是否能构成三角形,bool isTriangle(float, float, float);
- 计算周长, float getPerimeter(float, float, float);
- 计算面积, float getArea(float, float, float);
按照此方式,代码重构为: - 函数定义
// Triangle.h
#pragma once
bool isTriangle(float, float, float); //判断边长a,b,c是否能构成三角形
float getPerimeter(float, float, float); //计算周长
float getArea(float, float, float); //计算面积
- 函数实现
//Triangle.cpp
#include "Triangle.h"
#include <cmath>
//判断边长a,b,c是否能构成三角形
bool isTriangle(float a, float b, float c)
{
return (a+b>c && a+c>b && b+c>a && a>0 && b>0 && c>0);
}
//计算周长
float getPerimeter(float a, float b, float c)
{
if (isTriangle(a, b, c)
{
return (a+b+c);
}
return 0.0f;
}
//计算面积
float getArea(float a, float b, float c)
{
if (isTriangle(a,b,c)
{
float s = getPerimeter(a,b,c).;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
return 0.0f;
}
- 主函数
// main.cpp
#include <iostream>
#include "Triangle.h"
using namespace std;
int main()
{
float a, b, c;
cin >> a >> b >> c;
if (isTrangle(a,b,c)
{
cout << "Perimeter is " << getPerimeter(a, b, c) << endl;
cout << "Area is " << getArea(a, b, c) << endl;
}
else
{
cerr << "Invalidate Triangle." << endl;
}
}
2. 对象封装
- 从对象的封装入手,我们先将函数打包在一起变为一个对象,那么代码就变为:
- 对象定义
// Triangle.h
#pragma once
class Triangle
{
public: //only public function can be called
bool isTriangle(float, float, float); //判断边长a,b,c是否能构成三角形
float getPerimeter(float, float, float); //计算周长
float getArea(float, float, float); //计算面积
}
- 函数实现
//Triangle.cpp
#include "Triangle.h"
#include <cmath>
//判断边长a,b,c是否能构成三角形
bool Triangle::isTriangle(float a, float b, float c)
{
return (a+b>c && a+c>b && b+c>a && a>0 && b>0 && c>0);
}
//计算周长
float Triangle::getPerimeter(float a, float b, float c)
{
if (isTriangle(a, b, c)
{
return (a+b+c);
}
return 0.0f;
}
//计算面积
float Triangle::getArea(float a, float b, float c)
{
if (isTriangle(a,b,c)
{
float s = getPerimeter(a,b,c).;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
return 0.0f;
}
- 主函数
// main.cpp
#include <iostream>
#include "Triangle.h"
using namespace std;
int main()
{
float a, b, c;
cin >> a >> b >> c;
//因为函数封装到class Triangle,因此函数调用的方式要通过对象
//同时,函数能被调用,必须要函数的访问权限是public
Triangle t;
if (t.isTrangle(a,b,c)
{
cout << "Perimeter is " << t.getPerimeter(a, b, c) << endl;
cout << "Area is " << t.getArea(a, b, c) << endl;
}
else
{
cerr << "Invalidate Triangle." << endl;
}
}
因为a,b,c也是对象的一部分,我们也可以将a,b,c封装到对象内部,就构成一个有函数+变量组合而成的对象。
// Triangle.h
#pragma once
class Triangle
{
public: //only public function can be called
bool isTriangle(float, float, float); //判断边长a,b,c是否能构成三角形
bool isTriangle();
float getPerimeter(float, float, float); //计算周长
float getPerimeter();
float getArea(float, float, float); //计算面积
float getArea();
bool setTriangle(float, float, float); //设置三角形的边长
float a, b, c; //三角形边长a,b,c
}
Triangle.cpp的实现代码可以在上述基础上做修改即可。
这样的好处是,变量a,b,c在class内部是共享的,任何的成员函数都可以访问a,b,c。这样:
float getPerimeter(float, float, float); //可以改为
float getPerimeter();