欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c++-面向对象类的示例-求周长面积,判断体积相等-文件操作和一般操作

程序员文章站 2023-09-28 22:40:32
面向对象编程示例:求周长和面积 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;
}