• 【一起来用C++】————(1)类的练习案例(多文件编写)


    【一起来用C++】————(1)类的练习案例(多文件编写)

    练习案例1


    练习案例1:设计立方体类
    设计立方体类(Cube)
    求出立方体的面积和体积
    分别用全局函数和成员函数判断两个立方体是否相等。
    在这里插入图片描述


    #include 
    using namespace std;
    
    
    class cube {
    public:
    	//设置长,宽,高求表面积
    	void set_L(int L)
    	{
    		m_L = L;
    	}
    	void set_W(int W)
    	{
    		m_W = W;
    	}
    	void set_H( int H)
    	{
    		m_H = H;
    	}
    	int get_L()
    	{
    		return m_L;
    	}
    	int get_W()
    	{
    		return m_W;
    	}
    	int get_H()
    	{
    		return m_H;
    	}
    	int get_s()
    	{
    		return 2 * m_L * m_W + 2 * m_L * m_H + 2 * m_W * m_H;
    	}
    
    	int get_v()
    	{
    		return m_L * m_W * m_H;
    	}
    	//成员函数判断两个立方体是否相等
        void m_Is(cube &c)
        {
    	    if (m_L == c.get_L() &&
    			m_W == c.get_W() &&
    			m_H == c.get_H()) {
    			cout << "两立方体相等" << endl;
    	    }
    	    else {
    			cout << "两立方体不相等" << endl;
          	}
    	}
    
    private:
    	int m_L;//长
    	int m_W;//宽
    	int m_H;//高
    };
    //全局函数判断两个立方体是否相等
    void Is(cube& cube1, cube& cube2)
    {
    	if (cube1.get_L() == cube2.get_L()&&
    		cube1.get_W() == cube2.get_W()&&
    		cube1.get_H() == cube2.get_H()) {
    		cout << "两立方体相等" << endl;
    	}
    	else {
    		cout << "两立方体不相等" << endl;
    	}
    }
    
    int main()
    {
    	cube cu1;
    	cu1.set_L(10);
    	cu1.set_W(10);
    	cu1.set_H(10);
    	cu1.get_L();
    	cu1.get_W();
    	cu1.get_H();
    	cout << "表面积为:" <<cu1.get_s() << endl;
    	cout << "体积为:" << cu1.get_v() << endl;
    
    	cube cu2;
    	cu2.set_L(10);
    	cu2.set_W(20);
    	cu2.set_H(30);
    	cu2.get_L();
    	cu2.get_W();
    	cu2.get_H();
    	cout << "表面积为:" << cu2.get_s() << endl;
    	cout << "体积为:" << cu2.get_v() << endl;
    
    	Is(cu1, cu2);
    	cu2.m_Is(cu1);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    在这里插入图片描述


    多文件方式:
    在这里插入图片描述

    //list.cpp
    #include 
    #include "cube.h"//包含头文件
    using namespace std;
    
    void Is(cube& cube1, cube& cube2)
    {
    	if (cube1.get_L() == cube2.get_L()&&
    		cube1.get_W() == cube2.get_W()&&
    		cube1.get_H() == cube2.get_H()) {
    		cout << "两立方体相等" << endl;
    	}
    	else {
    		cout << "两立方体不相等" << endl;
    	}
    }
    
    int main()
    {
    	cube cu1;
    	cu1.set_L(10);
    	cu1.set_W(10);
    	cu1.set_H(10);
    	cu1.get_L();
    	cu1.get_W();
    	cu1.get_H();
    	cout << "表面积为:" <<cu1.get_s() << endl;
    	cout << "体积为:" << cu1.get_v() << endl;
    
    	cube cu2;
    	cu2.set_L(10);
    	cu2.set_W(20);
    	cu2.set_H(30);
    	cu2.get_L();
    	cu2.get_W();
    	cu2.get_H();
    	cout << "表面积为:" << cu2.get_s() << endl;
    	cout << "体积为:" << cu2.get_v() << endl;
    
    	Is(cu1, cu2);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    //cube.h
    #pragma once//
    #include //
    using namespace std;//包含头文件
    
    class cube {
    public:
    	//设置长,宽,高求表面积
    	void set_L(int L);
    	                            //类内只进行函数声明
    	void set_W(int W);
    	
    	void set_H(int H);
    	
    	int get_L();
    	
    	int get_W();
    	
    	int get_H();
    	
    	int get_s();
    
    	int get_v();
    	
    	void m_Is(cube& c);
    	
    private:
    	int m_L;//长
    	int m_W;//宽
    	int m_H;//高
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    //cube.cpp
    #include "cube.h"//包含函数声明
    //设置长,宽,高求表面积
    	void cube::set_L(int L)//添加类的解析符cube::
    	{
    		m_L = L;
    	}
    	void cube::set_W(int W)
    	{
    		m_W = W;
    	}
    	void cube::set_H(int H)
    	{
    		m_H = H;
    	}
    	int cube::get_L()
    	{
    		return m_L;
    	}
    	int cube::get_W()
    	{
    		return m_W;
    	}
    	int cube::get_H()
    	{
    		return m_H;
    	}
    	int cube::get_s()
    	{
    		return 2 * m_L * m_W + 2 * m_L * m_H + 2 * m_W * m_H;
    	}
    
    	int cube::get_v()
    	{
    		return m_L * m_W * m_H;
    	}
    	
    	void cube::m_Is(cube& c)
    	{
    		if (m_L == c.get_L() &&
    			m_W == c.get_W() &&
    			m_H == c.get_H()) {
    			cout << "两立方体相等" << endl;
    		}
    		else {
    			cout << "两立方体不相等" << endl;
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    练习案例2


    练习案例2:点和圆的关系
    设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系。
    在这里插入图片描述


    //设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系。
    #include 
    using namespace std;
    
    class Point {
    public:
    	void set_x(int x)
    	{
    		m_X = x;
    	}
    	int get_x()
    	{
    		return m_X;
    	}
    	void set_y(int y)
    	{
    		m_Y = y;
    	}
    	int get_y()
    	{
    		return m_Y;
    	}
    private:
    	int m_X;
    	int m_Y;
    };
    
    class Circle {
    public:
    	void set(int r)
    	{
    		m_R = r;
    	}
    	int get()
    	{
    		return m_R;
    	}
    	void set_o(Point center)
    	{
    		m_O = center;
    	}
    	Point get_o()
    	{
    		return m_O;
    	}
    private:
    	int m_R;
    	Point m_O;
    };
    
    void Is(Circle& c, Point& p)
    {
    	//计算两点之间距离平方
    	int C = (c.get_o().get_x() - p.get_x()) * (c.get_o().get_x() - p.get_x())
    		+ (c.get_o().get_y() - p.get_y()) * (c.get_o().get_y() - p.get_y());
    	//计算半径平方
    	int R = c.get() * c.get();
    	//判断距离
    	if (C < R) {
    		cout << "点在圆内" << endl;
    	}
    	else if (C == R) {
    		cout << "点在圆上" << endl;
    	}
    	else {
    		cout << "点在圆外" << endl;
    	}
    }
    
    int main()
    {
    	Circle c;//设置半径
    	c.set(10);
    	Point o;//设置圆心
    	o.set_x(10);
    	o.set_y(0);
    	c.set_o(o);
    	Point p;//设置外点
    	p.set_x(10);
    	p.set_y(10);
    
    	Is(c, p);//判断
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    在这里插入图片描述


    多文件方法:
    新建文件
    在这里插入图片描述

    //Circle.h
    #pragma once
    #include 
    using namespace std;
    
    class Point;
    class Circle {
    public:
    	void set(int r);
    	
    	int get();
    		
    	void set_o(Point center);
    		
    	Point get_o();
    private:
    		int m_R;
    		Point m_O;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    //Circle.cpp
    #include "Circle.h"
    #include "Point.h"
    
    void Circle::set(int r)
    {
    	m_R = r;
    }
    int Circle::get()
    {
    	return m_R;
    }
    void Circle::set_o(Point center)
    {
    	m_O = center;
    }
    Point Circle::get_o()
    {
    	return m_O;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    //Point.h
    #pragma once
    #include 
    using namespace std;
    
    class Point {
    public:
    	void set_x(int x);
    	
    	int get_x();
    	
    	void set_y(int y);
    	
    	int get_y();
    	
    private:
    	int m_X;
    	int m_Y;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    //Point.cpp
    #include "Point.h"
    
    void Point::set_x(int x)
    {
    	m_X = x;
    }
    int Point::get_x()
    {
    	return m_X;
    }
    void Point::set_y(int y)
    {
    	m_Y = y;
    }
    int Point::get_y()
    {
    	return m_Y;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    //c.cpp
    //设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系。
    #include 
    #include "Point.h"
    #include "Circle.h"
    
    using namespace std;
    
    void Is(Circle& c, Point& p)
    {
    	//计算两点之间距离平方
    	int C = (c.get_o().get_x() - p.get_x()) * (c.get_o().get_x() - p.get_x())
    		+ (c.get_o().get_y() - p.get_y()) * (c.get_o().get_y() - p.get_y());
    	//计算半径平方
    	int R = c.get() * c.get();
    	//判断距离
    	if (C < R) {
    		cout << "点在圆内" << endl;
    	}
    	else if (C == R) {
    		cout << "点在圆上" << endl;
    	}
    	else {
    		cout << "点在圆外" << endl;
    	}
    }
    
    int main()
    {
    	Circle c;//设置半径
    	c.set(10);
    	Point o;//设置圆心
    	o.set_x(10);
    	o.set_y(0);
    	c.set_o(o);
    	Point p;//设置外点
    	p.set_x(10);
    	p.set_y(10);
    
    	Is(c, p);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    参考文献

    https://www.bilibili.com/video/BV1et411b73Z?p=104&t=41.2

  • 相关阅读:
    Java中[Ljava.lang.Object;是什么?
    阿里云服务器部署wordpress站点
    2022 Java零基础必备 简单易学 Eclipse免费下载安装+JDK环境搭建一站式捆绑服务到底的教程 足够全、足够详细、足够劲爆
    【最新面试问题记录持续更新,java,kotlin,android,flutter】
    【obs】x264编码:“buffer_size“
    现货供应3~4.2V转5V 1A(FH6901)完美兼容FP6291
    【UE 粒子练习】07——创建动画拖尾类型粒子
    element-ui日期选择器el-date-picker, 案例:填写有效期和选择开始时间后, 自动生成结束时间, datetime时间转换
    【PowerQuery】第三方方案刷新PowerBI数据
    操作系统原理实验四:管道通信、消息通信程序
  • 原文地址:https://blog.csdn.net/qq_38364548/article/details/126008244