面向对象——案例练习(1)求圆的周长和面积

面向对象与面向过程:

面向过程:.吃(饭)
面向对象:(人,饭)

方法一:面向过程

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

int main(void)
{
    double r=10;
    double girth=0;
    double area=0;
    
    girth = getCircleGirth(r);
    area = getCircleArea(r);
    
    cout << "圆的半径:" << r << endl;
    cout << "圆的周长:" << girth << endl;
    cout << "圆的面积:" << area << endl;

    return 0;
}

方法二:面向对象

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Circle
{
public:
   //圆的周长
double getCircleGirth() { return 2 * 3.14*m_r; }    //圆的面积 double getCircleArea() { return 3.14*m_r*m_r; } void set(double r) { m_r = r; } double get() { return m_r; } private: int m_r; }; int main(void) { Circle c; double r; double girth; double area; c.set(10); girth = c.getCircleGirth(); area = c.getCircleArea(); cout << "圆的半径:" << c.get() << endl; cout << "圆的周长:" << girth << endl; cout << "圆的面积:" << area << endl; return 0; }

 

posted @ 2018-10-14 21:29  悦悦的小屋  阅读(1253)  评论(0编辑  收藏  举报