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

CPP基础

输出: cout << "你好世界 我是C++" << endl;    其中cout相当于C语言中的printf  << 是指最后所有的输入都放在cout中  endl为换行      cerr << "程序错误退出" << endl;  cerr 同样为输出

输入:cin >> a;    在这里cin和C语言中scanf作用一样  

#include <iostream>using namespace std;int main()
{int a=0;int b=0;cout << "你好世界 我是C++" << endl;cout << "你好世界 我是C++" << "你好,我是补充输出的" << endl;cin >> a;       //在这里cin和C语言中scanf作用一样cin >> b;cout << a << "," << b << endl;cout << a << "+" << b << "="<< a+b << endl;cerr << "程序错误退出" << endl;return 0;
}

创建namespace 

namespace cir {double PI=3.141592653;//获取圆形周长的函数double getlenth0fcircle(double radius){return 2*PI*radius;}//获取圆形面积的函数double getAifCircle(double radius){return PI*radius*radius;}}

在.cpp文件中 首先要引用 #include “cir.h” 然后要加上  using namespace cir  才能够使用

#include <iostream>
#include <stdio.h>
#include "cir.h"using namespace std;
using namespace cir;int main()
{double myRadius =5 ;cout << "Hello World!namespace" << endl;//printf("lenth: %lf ,are:%lf\n", cir::getlenth0fcircle(myRadius),//      cir::getAifCircle(myRadius));printf("lenth: %lf ,are:%lf\n", getlenth0fcircle(myRadius),getAifCircle(myRadius));return 0;
}

Lambda 表达式

Lambda 表达式是C++ 引入的一种匿名函数的方式,它允许你在需要函数的地方内联的定义函数,而无需单独命名函数

int main()
{int x=60;int y=20;auto add=[](int a,int b)->int{return a+b;};int ret =add(x,y);cout << ret;return 0;
}

其中auto为预测  add为命名   【】捕获列表  ()为参数  int 为类型   

int getMax(int a, int b,bool(*p1)(int a,int b))
{if(p1(a,b)){return a;}else{return b;}
}
int main()
{int x=40;int y=20;//bool(*p)(int a,int b)=compare;int max=getMax(x,y,[](int a,int b)->bool {return a>b;});cout << max <<endl;return 0;
}
int max=getMax(x,y,[](int a,int b)->bool {       return a>b;
});   

内联函数和Lambda函数

带有捕获列表的Lambda 函数

方式1

 auto add =[x,y]()->int{//用这种方式捕获,是不能修改变量值的,只能用,可读。return x+y;};int ret =add();cout << ret <<  endl;

方式2

 auto mul =[=]()->int{//用这种方式可以捕获所有的变量,不需要去在列表中写明,是不能修改变量值的,只能用,可读。return x*y;};ret =mul();cout << ret <<  endl;

= 可以捕获所有的变量

方式三

auto modifyAndMul =[&]()->int{//用这种引用的方式来捕获,引用类似指针,进行地址访问。 可修改数值x=15;return x*y*z;};ret =modifyAndMul();cout << ret <<  endl;

& 这种方式可以修改捕获的数值

内联函数

类:

当把在c语言中的结构体搬到c++中 会出现一系列的问题 

以下为c语言的结构体

#include <stdio.h>
#include <stdlib.h>
struct Car{       //汽车“类”char *color ;  //颜色char *brand;   //品牌char *type;    //车型int year;      //年限void (*printCarInfo)(char *color,char *brand,char*type,int year);  //函数指针,指向车介绍函数void (*carRun)(char *type);       //函数指针,指向车运行的函数void (*carStop)(char * type);     //函数指针,执行车停止的函数
};void bwmThreePrintCarInfo(char *color,char *brand,char *type,int year)
{printf("车的品牌是 :%s,型号是 :%s,颜色是: %s,上市年限是: %d\r\n",brand,type,color,year);}
void AodiA6PrintCarInfo(char *color,char *brand,char *type,int year)
{printf("车的品牌是 :%s,型号是 :%s,颜色是: %s,上市年限是: %d\r\n",brand,type,color,year);}int main()
{struct Car BWMthree;BWMthree.color ="白色";BWMthree.brand ="宝马";BWMthree.type ="3系";BWMthree.year =2025;BWMthree.printCarInfo =bwmThreePrintCarInfo;BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);struct Car *AodiA6;AodiA6 =(struct Car*)malloc(sizeof(struct Car));AodiA6->color="黑色";AodiA6->brand ="奥迪";AodiA6->year =2025;AodiA6->type ="A6";AodiA6->printCarInfo=AodiA6PrintCarInfo;AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type ,AodiA6->year);return 0;
}

放到C++下

问题①  char * 要换成 string 

问题② 要将结构体struct 换成类 class  且将私有换成public 

问题③ 要将printf 换成结构体的形式cout输出

问题④ 在C++中,通过std::to string ()函数,将整形数转化为字符串

问题⑤ 如果采用指针的形式 那么不能用malloc  要使用 Car *AodiA6 =new Car(); 

如下:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace  std;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 bwmThreePrintCarInfo(string color,string brand,string type,int year)
{//printf("车的品牌是 :%s,型号是 :%s,颜色是: %s,上市年限是: %d\r\n",//brand,type,color,year);string str = "车的品牌是:" + brand+ ",型号是:" + type+ ",颜色是:"  + color+",上市年限是:" + to_string(year);cout << str << endl;}
void AodiA6PrintCarInfo(string color,string brand,string type,int year)
{//printf("车的品牌是 :%s,型号是 :%s,颜色是: %s,上市年限是: %d\r\n",//brand,type,color,year);string str = "车的品牌是:" + brand+ ",型号是:" + type+ ",颜色是:"  + color+",上市年限是:" + to_string(year);cout << str << endl;}int main()
{Car BWMthree;BWMthree.color ="白色";BWMthree.brand ="宝马";BWMthree.type ="3系";BWMthree.year =2025;BWMthree.printCarInfo =bwmThreePrintCarInfo;BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);Car *AodiA6=new Car();//AodiA6 =(struct Car*)malloc(sizeof(struct Car));AodiA6->color="黑色";AodiA6->brand ="奥迪";AodiA6->year =2025;AodiA6->type ="A6";AodiA6->printCarInfo=AodiA6PrintCarInfo;AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type ,AodiA6->year);return 0;
}

真正的成员变量   在class中 不需要像结构体一样 进行传参 

一般在类的外部进行成员函数的实现 在class中只需要进行函数的引用

外部时

void Car::realPrintCarInfo()

{

}

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>using namespace  std;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();    //声明成员函数};
void Car::realPrintCarInfo()  //在类的外部进行成员函数的实现
{string str = "车的品牌是:" + brand+ ",型号是:" + type+ ",颜色是:"  + color+",上市年限是:" + to_string(year);cout << str << endl;
}void bwmThreePrintCarInfo(string color,string brand,string type,int year)
{//printf("车的品牌是 :%s,型号是 :%s,颜色是: %s,上市年限是: %d\r\n",//brand,type,color,year);string str = "车的品牌是:" + brand+ ",型号是:" + type+ ",颜色是:"  + color+",上市年限是:" + to_string(year);cout << str << endl;}
void AodiA6PrintCarInfo(string color,string brand,string type,int year)
{//printf("车的品牌是 :%s,型号是 :%s,颜色是: %s,上市年限是: %d\r\n",//brand,type,color,year);string str = "车的品牌是:" + brand+ ",型号是:" + type+ ",颜色是:"  + color+",上市年限是:" + to_string(year);cout << str << endl;}int main()
{Car BWMthree;BWMthree.color ="白色";BWMthree.brand ="宝马";BWMthree.type ="3系";BWMthree.year =2025;//BWMthree.printCarInfo =bwmThreePrintCarInfo;//BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);BWMthree.realPrintCarInfo();Car *AodiA6=new Car();//AodiA6 =(struct Car*)malloc(sizeof(struct Car));AodiA6->color="黑色";AodiA6->brand ="奥迪";AodiA6->year =2025;AodiA6->type ="A6";//AodiA6->printCarInfo=AodiA6PrintCarInfo;//AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type ,AodiA6->year);AodiA6->realPrintCarInfo();return 0;
}

在C++中,一个类包含另一个类的对象成为组合。这种关系通常白哦是一种拥有的关系

class Car{       //汽车“类”
public://成员数据string color ;  //颜色string brand;   //品牌string type;    //车型int year;      //年限Wheel wl;Wheel *pwl;//其实也是成员数据,指针变量,指向函数的变量,而并非真正的成员函数void (*printCarInfo)(string color,string brand,string type,int year);  //函数指针,指向车介绍函数void (*carRun)(string type);       //函数指针,指向车运行的函数void (*carStop)(string type);     //函数指针,执行车停止的函数void realPrintCarInfo();    //声明成员函数};

class中包含class

class Wheel
{
public:string brand;string year;void wheelPrintInfo();};void Wheel::wheelPrintInfo()
{cout << "我的轮胎品牌是 " << brand << endl;cout << "我的轮胎日期是"  << year << endl;}
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>using namespace  std;
//在C++中,一个类包含另一个类的对象成为组合
class Wheel
{
public:string brand;string year;void wheelPrintInfo();};void Wheel::wheelPrintInfo()
{cout << "我的轮胎品牌是 " << brand << endl;cout << "我的轮胎日期是"  << year << endl;}class Car{       //汽车“类”
public://成员数据string color ;  //颜色string brand;   //品牌string type;    //车型int year;      //年限Wheel wl;Wheel *pwl;//其实也是成员数据,指针变量,指向函数的变量,而并非真正的成员函数void (*printCarInfo)(string color,string brand,string type,int year);  //函数指针,指向车介绍函数void (*carRun)(string type);       //函数指针,指向车运行的函数void (*carStop)(string type);     //函数指针,执行车停止的函数void realPrintCarInfo();    //声明成员函数};
void Car::realPrintCarInfo()  //在类的外部进行成员函数的实现
{string str = "车的品牌是:" + brand+ ",型号是:" + type+ ",颜色是:"  + color+",上市年限是:" + to_string(year);cout << str << endl;
}void bwmThreePrintCarInfo(string color,string brand,string type,int year)
{//printf("车的品牌是 :%s,型号是 :%s,颜色是: %s,上市年限是: %d\r\n",//brand,type,color,year);string str = "车的品牌是:" + brand+ ",型号是:" + type+ ",颜色是:"  + color+",上市年限是:" + to_string(year);cout << str << endl;}
void AodiA6PrintCarInfo(string color,string brand,string type,int year)
{//printf("车的品牌是 :%s,型号是 :%s,颜色是: %s,上市年限是: %d\r\n",//brand,type,color,year);string str = "车的品牌是:" + brand+ ",型号是:" + type+ ",颜色是:"  + color+",上市年限是:" + to_string(year);cout << str << endl;}int main()
{Car BWMthree;BWMthree.color ="白色";BWMthree.brand ="宝马";BWMthree.type ="3系";BWMthree.year =2025;BWMthree.pwl = new Wheel();BWMthree.pwl->brand="米其林";BWMthree.pwl->year="2025";BWMthree.realPrintCarInfo();BWMthree.pwl->wheelPrintInfo();// BWMthree.wl.brand="米其林";//BWMthree.wl.year ="2025";//BWMthree.wl.wheelPrintInfo();//BWMthree.printCarInfo =bwmThreePrintCarInfo;//BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);Car *AodiA6=new Car();//AodiA6 =(struct Car*)malloc(sizeof(struct Car));AodiA6->color="黑色";AodiA6->brand ="奥迪";AodiA6->year =2025;AodiA6->type ="A6";AodiA6->pwl = new Wheel();AodiA6->pwl->brand="马牌";AodiA6->pwl->year="2025";AodiA6->realPrintCarInfo();AodiA6->pwl->wheelPrintInfo();//AodiA6->wl.brand = "马牌";//AodiA6->wl.year =2025;//AodiA6->printCarInfo=AodiA6PrintCarInfo;//AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type ,AodiA6->year);//AodiA6->wl.wheelPrintInfo();return 0;
}

注意指针 时要设立新的new

http://www.lqws.cn/news/190963.html

相关文章:

  • 内嵌式mqtt server
  • RNN和CNN使用场景区别
  • 【Auto.js例程】华为备忘录导出到其他手机
  • Levenberg-Marquardt算法详解和C++代码示例
  • 山东大学算法设计与分析复习笔记
  • VTK 显示文字、图片及2D/3D图
  • 基于STM32语音识别柔光台灯
  • logstash拉取redisStream的流数据,并存储ES
  • Python读取阿里法拍网的html+解决登录cookie
  • 宁乡地-气-碳-水相互作用综合观测数据集
  • 概念理解篇:线程同步之【互斥】
  • 《0/1背包》题集
  • 飞马LiDAR500雷达数据预处理
  • AOSP (Android11) 集成Google GMS三件套
  • 大模型时代的“思考“与“行动“:人工智能的认知革命
  • STM32标准库-TIM输出比较
  • iview Switch Tabs TabPane 使用提示Maximum call stack size exceeded堆栈溢出
  • 《深度体验 Egg.js:打造企业级 Node.js 应用的全景指南》
  • ardupilot 开发环境eclipse 中import 缺少C++
  • Splash动态渲染技术全解析:从基础到企业级应用(2025最新版)
  • 麒麟v10系统的docker重大问题解决-不支持容器名称解析
  • 【机械视觉】Halcon—【八、形态学调整和生成棋盘格】
  • MDP的 Commands模块
  • MS31912TEA 多通道半桥驱动器 氛围灯 照明灯 示宽灯 转向灯驱动 后视镜方向调节 可替代DRV8912
  • 在 Caliper 中执行不同合约的方法
  • 应用分享 | 精准生成和时序控制!AWG在确定性三量子比特纠缠光子源中的应用
  • Java学习——正则表达式
  • 09.三数之和
  • vm虚拟机添加虚拟机无反应,获取所有权
  • 在WPF项目中集成Python:Python.NET深度实战指南