C++入门(2)
2024-08-25 02:54:00
第一个程序,输入输出:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;//endl 换行
int a;
cin>>a;
return ;
}
注释:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
数据类型
类型 | 关键字 |
---|---|
布尔型 | bool |
字符型 | char |
整型 | int |
浮点型 | float |
双浮点型 | double |
无类型 | void |
宽字符型 | wchar_t |
要使用string类型,需要引入#include <string>
Extern 变量的使用
文件A.cpp
#include <iostream> extern int count; void write_extern(void) //(void)表示无输入参数 也可以不填写
{
std::cout << "Count is " << count << std::endl;
} 文件main.cpp
int count ;
extern void write_extern();
void main()
{
count =1;
write_extern();
}
定义函数
return_type function_name( parameter list )
{
body of the function
}
数据结构
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
}book;
类定义
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
类使用
#pragma region 类的使用
Student *pStu = new Student; //在堆中 需要手动回收
pStu -> name = "小明";
pStu -> age = 15;
pStu -> score = 92.5f;
pStu -> say();
delete pStu; //删除对象 Student s; //在栈中 自动回收
s.name="1111";
s.age=23;
s.score=22;
s.say();
Student stu;
stu.name="2";
stu.age=2;
stu.score=2;
stu.say();
cout<<&s<<";"<<&stu<<endl;
#pragma endregion
引用类 类名+ 定义名(参数),无参数时直接 类名+定义名
->就是用指针调用函数或者获取数据。
a->b等价于(*a).b 函数不需要加分号
类或者结构体后面才需要分号
最新文章
- [No00007A]没有文件扩展";.js";的脚本引擎 解决办法
- Python下载进度条
- List集合基于某个字段排序
- Python :open文件操作,配合read()使用!
- [1008]harder_prime
- java中遇到过的String的一些特性
- 在ASP.NET 5中显示错误信息
- [BIM]STEP标准和EXPRESS语言
- herf窗口点击跳转
- HTTP长连接200万尝试及调优
- 聚类算法:K均值、凝聚层次聚类和DBSCAN
- 海量数据挖掘--DB优化篇
- 详解android的号码匹配
- VUE请求本地数据的配置json-server
- ffmypeg 视频处理类库使用方法
- 【转载】ASP.NET Core Web 支付功能接入 支付宝-电脑网页支付篇
- An Overview of End-to-End Exactly-Once Processing in Apache Flink (with Apache Kafka, too!)
- 判断点在不在多边形范围内c#
- 实战--Keepalived和LVS实现负载高可用
- java正则积累