当前位置: 首页 > news >正文

QT编程练习20250507

#include <stdio.h>  // 包含标准输入输出库
#include <stdlib.h> // 包含通用工具库(如malloc, free等)
#include <string>   // 包含C++ string类定义
#include <iostream> // 包含C++输入输出流库using namespace std; // 使用std命名空间,避免每次调用标准库函数时都需要加上前缀std::// 定义一个汽车结构体,用于表示汽车的基本信息
struct Car{ string color; // 颜色string brand; // 品牌string type;  // 车型int year;     // 年限// 函数指针,指向打印汽车信息的函数void (*printCarInfo)(string color,string brand,string type, int year); // 函数指针,指向车运行的函数void (*carRun)(string type); // 函数指针,执行车停止的函数void (*carStop)(string type); 
};// 定义一个汽车类,与上述结构体类似,但使用了类的方式,并且添加了一个成员函数realPrintCarInfo
class car{
public:string color; // 颜色string brand; // 品牌string type;  // 车型int year;     // 年限// 函数指针,指向打印汽车信息的函数void (*printCarInfo)(string color, string brand, string type, int year); // 函数指针,指向车运行的函数void (*carRun)(string type); // 函数指针,执行车停止的函数void (*carStop)(string type); // 成员函数,用于打印汽车信息void realPrintCarInfo();
};// 实现realPrintCarInfo成员函数,该函数以更直观的方式打印汽车信息
void car::realPrintCarInfo(){cout << "成员函数打印" << endl;string str = "车的品牌是:" + brand+ ",型号是: " + type+ ",颜色是:" + color+ ",上市年限是:" + std::to_string(year);cout << str << endl;
}// 定义一个打印宝马3系汽车信息的函数
void bwmThreePrintCarInfo(string color,string brand,string type, int year)
{printf("车的品牌是:%s, 型号是: %s, 颜色是:%s,上市年限是%d\n",brand.c_str(), type.c_str(), color.c_str(), year);
}// 定义一个打印奥迪A6汽车信息的函数
void A6PrintCarInfo(string color,string brand,string type, int year)
{printf("车的品牌是:%s,型号是: %s, 颜色是:%s, 上市年限是%d\n",brand.c_str(), type.c_str(), color.c_str(), year);
}int main()
{// 创建一个结构体变量BWMthree并初始化其属性值struct Car BWMthree;BWMthree.color = "白色";BWMthree.brand = "宝马";BWMthree.type = "3系";BWMthree.year = 2023;BWMthree.printCarInfo = bwmThreePrintCarInfo;// 调用BWMthree的printCarInfo函数打印信息BWMthree.printCarInfo(BWMthree.color, BWMthree.brand, BWMthree.type, BWMthree.year);// 动态创建一个car类的对象AodiA6,并初始化其属性值car *AodiA6 = new car();AodiA6->color = "黑色";AodiA6->brand = "奥迪";AodiA6->type = "A6";AodiA6->year = 2008;AodiA6->printCarInfo = A6PrintCarInfo;// 调用AodiA6的printCarInfo函数打印信息AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);// 释放动态分配的内存delete AodiA6;return 0;
}

此代码示例展示了如何通过结构体和类来描述对象(这里是以汽车为例),以及如何利用函数指针实现对不同对象类型的不同行为。同时,也演示了C++中成员函数的定义与使用方法。

http://www.xdnf.cn/news/325765.html

相关文章:

  • 【PostgreSQL数据分析实战:从数据清洗到可视化全流程】7.1 主流可视化工具对比(Tableau/Matplotlib/Python库)
  • FreeCAD傻瓜教程-涡轮蜗杆的快速绘制FCGear工作台的使用方法
  • 算法专题四:前缀和
  • 【北京迅为】iTOP-4412精英版使用手册-第八章 Android 4.4系统编译
  • neo4j多跳查询,未只获取到收尾两个节点,待继续
  • 智能运维实战|数据库卡慢处置的一次关键事件
  • 尚硅谷-硅谷甄选项目记录
  • Facebook隐私设置详解:如何保护你的个人信息
  • 【漫话机器学习系列】245.权重衰减(Weight Decay)
  • SR触发器为什么能够消抖
  • Vue 项目中长按保存图片功能实现指南
  • AI大模型基础设施:NVIDIA GPU和AMD MI300系列的区别
  • android 记录应用内存
  • Scaffold-DbContext详解
  • 如何减少锁竞争并细化锁粒度以提高 Rust 多线程程序的性能?
  • 2025FIC初赛(手机)
  • JAVA中ArrayList的解析
  • Scala语法
  • 【Axure视频教程】中继器表格——未选、半选和全选
  • 代码随想录算法训练营第五十八天| 图论4—卡码网110. 字符串接龙,105. 有向图的完全联通
  • C# WPF 颜色拾取器
  • MySQL OCP 认证限时免费活动​ 7 月 31 日 前截止!!!
  • 多规格直线运动转换至非线性直线的转换方法
  • 【C++进阶】第1课—继承
  • C#管道通讯及传输信息丢失的原因
  • android中背压问题面试题及高质量回答范例
  • 前端面试测试题目(一)
  • 《Python星球日记》 第49天:特征工程与全流程建模
  • 认识tomcat(了解)
  • Android Studio开发安卓app 设置开机自启