C++改写为C

stm使用中,经常能见到CPP的示例,这些是给arduino,esp32用的,stm32 也支持cpp但是你就想用c怎么办呢,比如我在新手的时候:: 这个双冒号就难住了英雄好汉

比如这是个cpp的 如果类不多的情况下 改写为c

cpp 复制代码
#include <iostream>

using namespace std;

class Box
{
public:
    double length;   // 长度
    double breadth;  // 宽度
    double height;   // 高度
    // 成员函数声明
    double get(void){
        return length * breadth * height;
    };
    void set( double len, double bre, double hei ){
        length = len;
        breadth = bre;
        height = hei;
    };
};
// 成员函数定义
//double Box::get(void)
//{
//    return length * breadth * height;
//}

//void Box::set( double len, double bre, double hei)
//{
//    length = len;
//    breadth = bre;
//    height = hei;
//}
int main( )
{
    Box Box1;        // 声明 Box1,类型为 Box
    Box Box2;        // 声明 Box2,类型为 Box
    Box Box3;        // 声明 Box3,类型为 Box
    double volume = 0.0;     // 用于存储体积

    // box 1 详述
    Box1.height = 5.0;
    Box1.length = 6.0;
    Box1.breadth = 7.0;

    // box 2 详述
    Box2.height = 10.0;
    Box2.length = 12.0;
    Box2.breadth = 13.0;

    // box 1 的体积
    volume = Box1.height * Box1.length * Box1.breadth;
    cout << "Box1 的体积:" << volume <<endl;

    // box 2 的体积
    volume = Box2.height * Box2.length * Box2.breadth;
    cout << "Box2 的体积:" << volume <<endl;


    // box 3 详述
    Box3.set(16.0, 8.0, 12.0);
    volume = Box3.get();
    cout << "Box3 的体积:" << volume <<endl;
    return 0;
}

改写为C:

cpp 复制代码
//#include <iostream>
#include <stdio.h>

//using namespace std;

//class Box
//        {
//                public:
double Box1length;   // 长度
double Box1breadth;  // 宽度
double Box1height;   // 高度
// 成员函数声明
double get(void);
//void set( double len, double bre, double hei );
//        };
// 成员函数定义
double Box1get(void)
{
    return Box1length * Box1breadth * Box1height;
}//暂时没用

void Box1set( double len, double bre, double hei)
{
    Box1length = len;
    Box1breadth = bre;
    Box1height = hei;
}//暂时没用
int main( )
{
//    Box Box1;        // 声明 Box1,类型为 Box
//    Box Box2;        // 声明 Box2,类型为 Box
//    Box Box3;        // 声明 Box3,类型为 Box
    double volume = 0.0;     // 用于存储体积

    // box 1 详述
    Box1height = 5.0;
    Box1length = 6.0;
    Box1breadth = 7.0;

    // box 2 详述
//    Box2.height = 10.0;
//    Box2.length = 12.0;
//    Box2.breadth = 13.0;

    // box 1 的体积
    printf("Box1height:%f",Box1height);
    volume = Box1height * Box1length * Box1breadth;
    printf("%f",volume);
//    cout << "Box1 的体积:" << volume <<endl;

    // box 2 的体积
//    volume = Box2.height * Box2.length * Box2.breadth;
//    cout << "Box2 的体积:" << volume <<endl;
//
//
//    // box 3 详述
//    Box3.set(16.0, 8.0, 12.0);
//    volume = Box3.get();
//    cout << "Box3 的体积:" << volume <<endl;
    return 0;
}

就是把类的东西完全拆成普通的,这样你可能需要多打很多代码,

相关推荐
谱写秋天6 分钟前
Qt 5.5 的安装与配置(使用 VSCode编辑)
开发语言·vscode·qt
项目申报小狂人7 分钟前
算法应用上新!自适应更新策略差分进化算法求解球形多飞行器路径规划问题,附完整MATLAB代码
开发语言·算法·matlab
阿珊和她的猫4 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
fouryears_234177 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~7 小时前
C#---StopWatch类
开发语言·c#
lifallen8 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研8 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
快乐的划水a9 小时前
组合模式及优化
c++·设计模式·组合模式
cui__OaO10 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习