当前位置: 首页 > 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.xdnf.cn/news/909595.html

相关文章:

  • Go 并发编程基础:通道(Channel)的使用
  • C语言的全称:(25/6/6)
  • Python应用break初解
  • $attrs 与 $listeners 透传
  • 实战:用 i.MX8MP 读取 220V 电流信息的完整方案(HLW8032 接入)
  • 华科:视觉大模型动态剪枝框架FlowCut
  • onSaveInstanceState() 和 ViewModel 在数据保存能力差异
  • nginx的安装
  • 《100天精通Python——基础篇 2025 第5天:巩固核心知识,选择题实战演练基础语法》
  • 软件测评服务如何依据标准确保品质?涵盖哪些常见内容?
  • SQLAlchemy 中的 func 函数使用指南
  • [密码学实战]C语言使用SDF库构建国密算法RESTful服务(五)
  • janus客户端源码分析
  • 【计算机网络】非阻塞IO——poll实现多路转接
  • AIGC 基础篇 Python基础 01
  • 使用阿里云百炼embeddings+langchain+Milvus实现简单RAG
  • PCB设计教程【大师篇】——STM32开发板电源设计(LDO、DCDC)
  • 深入Kubernetes源码阅读指南:从环境搭建到核心原理剖析
  • 【LeetCode】3309. 连接二进制表示可形成的最大数值(递归|回溯|位运算)
  • 在 Caliper 中执行不同合约的方法
  • Varjo如何帮助Entrol最大化其XR模拟器的性能
  • 探索GIS局部放电监测:PRPD与PRPS图谱的奥秘
  • 好子集的数目之解决方案
  • EDA断供危机下的冷思考:中国芯片设计软件的破局之道优雅草卓伊凡
  • Executors for C++- A Long Story
  • C++.OpenGL (4/64)纹理(Texture)
  • Vue3 GSAP动画库绑定滚动条视差效果 绑定滚动条 滚动条动画 时间轴
  • 破壁焕新能:DeviceNET转EtherNet/IP网关赋能烟草智能制造跃迁
  • Redis 主从 + 哨兵集群部署
  • Python爬虫伪装