C++基础语法
创始人
2024-02-29 08:58:19
0
  1. cout输出
    cin是键盘输入

    在这里插入图片描述
//i input 输入 o output输出 stream流 输入输出流头文件(类似stdio.h)
2 #include 
3
4 //std(标准) 使用标准的命名空间using namespace std;//命名空间,此标识符作为此组群的名字
5 using namespace std;
6
7 //有且只有一个主函数 可以有多个其他函数
8 int main(int argc, char *argv[])
9 {
10 //cout 输出 类似 c语言的printf
11 //endl 类似 c语言的 换行符
12 // printf("Hello 你好!\n");
13
14 //cout代表的输出设备
15 cout << "Hello 你好!" << endl;//将字符串输出到 控制台
16
17 //cin代表的是输入设备
18 int num =0;
19 cin >> num;//将建键盘输入的数据 赋值给 num
20 cout<<"num = "<

//使用标准的命名空间std,std中所有成员名 可以直接使用,cout endl cin都是命名空间std的成员

 #include using namespace std;int main(int argc, char *argv[]){std::cout << "Hello World!" << std::endl;cout << "Hello World!" << endl;return 0;}

作用域 ::
(表明数据归属性)

using namespace std;
2 int a = 10;//全局变量
3 void  main()
4 {
5 int a = 20;//局部变量
6 cout<<"局部变量a = "<

命名空间 namespace 解决命名冲突;
(快速开发,方便移植)

1 //定义一个名字为A的命名空间(变量、函数)
2 namespace A {
3 int a = 100;
4 }
5 namespace B {
6 int a = 200;
7 }
8 void main()
9 {
10 //A::a a是属于A中
11 cout<<"A中a = "<

命名空间可嵌套命名空间

1 namespace A {
2 int a = 1000;
3 namespace B {
4 int a = 2000;
5 }
6 }
7 void main()
8 {
9    cout<<"A中的a = "<

命名空间是开放的,即可以随时把新的成员加入已有的命名空间中(常用)

namespace A{int a = 100;}
namespace A{int c = 200;
}
int main()
{cout<<"a="<

命名空间除了可以 存放变量 还可以存放函数

1 namespace A {
2 int a=100;//变量
3
4 void func()//函数
5 {
6 cout<<"func遍历a = "<
11 //变量的使用
12 cout<<"A中的a = "<

在外定义命名空间中的函数

namespace A {
2 int a=100;//变量
3
4 void func();
5 }
6
7 void A::func()//成员函数 在外部定义的时候 记得加作用域
8 {
9 //访问命名空间的数据不用加作用域
10 cout<<"func遍历a = "<
15 cout<<"funb遍历a = "<
19 A::func();
20 funb();
21 }

无名命名空间,意味着命名空间中的标识符只能在本文件内访问,相当于给这个标识
符加上了static,使得其可以作为内部连接

1 namespace  {
2 int a=100;//变量
3
4 void func()//函数
5 {
6 cout<<"holle world"<
11 //变量的使用
12 cout<<"A中的a = "<

给命名空间 取个别名

namespace veryLongName{
2
3 int a = 10;
4 void func(){ cout << "hello namespace" << endl; }
5
6 }
7
8 int main()
{
9 namespace shortName = veryLongName;
10 cout << "veryLongName::a : " << shortName::a << endl;
11 veryLongName::func();
12 shortName::func();
13 }

using 使用命名空间

1 namespace veryLongName {
2 int a=100;
3 void func(){cout<<"hello namespace"<
8 //使用veryLongName命名空间
9 using namespace veryLongName;
10
11 //出现的变量 从veryLongName命名空间中找 找不到 从其他地方中
12 cout<<"a = "<

当局部变量和命名空间重名的时候优先选择,只会选择局部变量

1 namespace veryLongName {
2 int a=100;
3 void func(){cout<<"hello namespace"<
7 int a=200;
8 //使用veryLongName命名空间
9 using namespace veryLongName;
10
11 //出现的变量 从veryLongName命名空间中找 找不到 从其他地方中
12 cout<<"a = "<

C++,中的bool类型,和51单片机中的,bit是一样的意思

1 void test02()
2 {
3 bool mybool;
4 cout<<"sizeof(bool) = "<

相关内容

热门资讯

消费者退换货遭拒 江南布衣一门... 日前,消费者周女士向泰山财经记者反映称,她在位于山东省青岛市黄岛区漓江西路海上嘉年华江南布衣门店购买...
乌兰浩特市:法援“童”行守“未... 乌兰浩特市 法援“童”行 守“未”成长 近日,兴安盟乌兰浩特市法律援助中心联合乌兰哈达司法所,在某小...
原创 越... 日前,日本媒体报道了福原爱接受采访的消息,在采访中福原爱挺着孕肚公开了自己已经结婚,且怀上对方孩子的...
李营营律师团队分享:这种情况下... 山东高院:败诉方在诉讼中所作陈述,是否构成商业诋毁? 当事人通过民事诉讼行使合法权益,不属于“传播”...
马上评|用更完善的制度为独居者... 没有配偶、没有子女,父母早逝,一人独居,46岁上海女子蒋女士的身后事引发广泛关注。12月21日,上海...
鹏鼎控股:已组织法务团队评估专... 证券之星消息,鹏鼎控股(002938)12月22日在投资者关系平台上答复投资者关心的问题。 投资者提...
【列国鉴·年终观察】法兰西第五... 法兰西第五共和国陷入制度困境 唐霁 2025年9月,法国总统马克龙任命其执政以来第七位总理勒科尔尼,...
法官马春刚调解时突发心脏病去世... 西吉县人民法院综合审判庭副庭长马春刚,在调解纠纷时突发心脏病倒在办公桌前,12月12日,经抢救无效不...
平佳健:一场文物事件如何推动制... 【文/观察者网专栏作者 平佳健】 近日,庞元济后人庞叔令质疑其父庞增和及家人于1959年捐赠南京博...
最高检邀请部分全国人大代表走访... 12月15日至19日,应最高人民检察院邀请,来自安徽、江苏、上海及河南的18名全国人大代表赴河南走访...