C++是在C语言基础上的一个延伸,c室c++的一个子集,新增了数据类型、命名空间等内容
#include <iostream>
using namespace std;
int main(){
cout<<"hello world"<<endl;
return 0;
}
C++的基础
新的数据类型

反驳以下,
#include <stdio.h>
#include <stdbool.h>
int main(){
bool x = true;
if(x){
printf("pppppp");
}
return 0;
}
新的初始化方法

随用随定义


C++输入输出方式


cout<<x<<endl;
cout<<"x+y="<<x+y<<endl;
cin >>x;
cin>>x>>y;//输入多个变量
1.不用关注占位符
2.不用关注数据类型
命名空间
namespace == 划片取名字
防止不同库中,出现相同名字的变量和函数名
例如名字空间 A和B,都有f1函数
那么调用方式为
A::f1()和B::f1()
#include <iostream>
#include <stdlib.h>
using namespace std; // std是iostream的命名空间
namespace A
{
int x = 1;
int f1()
{
cout << "A" << endl;
}
} // namespace A
namespace B
{
int x = 2;
int f1()
{
cout << "B" << endl;
}
} // namespace B
int main(void)
{
int x = 0;
cout << "请输入一个整数" << endl;
cin >> x;
cout << "该整数的8进制表示为:" << oct << "0" << x << endl;
cout << "该整数的10进制表示为:" << dec << x << endl;
cout << "该整数的16进制表示为:" << hex << "0x" << x << endl;
cout << A::x << "是NamespaceA的x变量的值" << endl;
cout << B::x << "是NamespaceB的x变量的值" << endl;
A::f1();
B::f1();
bool y = false;
cout << "请输入一个布尔(0或者1)" << endl;
cin >> y;
if (y)
{
cout << "布尔" << boolalpha << true << endl;
}
else
{
cout << "布尔" << false << endl;
}
return 0;
}
---------------------------------------------------------------------------------
请输入一个整数
56
该整数的8进制表示为:070
该整数的10进制表示为:56
该整数的16进制表示为:0x38
1是NamespaceA的x变量的值
2是NamespaceB的x变量的值
A
B
请输入一个布尔(0或者1)
1
布尔true
面向对象
封装、继承、多态
指针 vs 引用
define vs const
函数默认值
函数重载
内存管理(堆内存管理,几乎完全由程序员自己控制)
网友评论