案例:输出Hello World
(1)源代码
#include <iostream>
using namespace std;
int main(){
cout << "Hello World" <<endl;
}
(2)编译及运行
g++ helloworld.cpp
./a.out
(3)基本区别
从helloworld.cpp,看C和C++的基本区别:
1、单行注释//
2、文件后缀名.cpp
3、头文件#include
4、命名空间 using namespace std;
5、标准输出流cout、输出运算符<<、换行控制器endl
6、编译工具g++
1、源文件后缀
(1)C/C++头文件后缀名的区别
C | C++ |
---|---|
*.h | *.h *.hpp |
(2)C/C++源文件后缀名的区别
C | C++ |
---|---|
*.c | *.cpp *.cc *.cxx |
(3)不同编译器C++源文件后缀名区别
平台 | 可用后缀名 |
---|---|
Unix | .c,.cc,.cxx,.C |
GNU C++ | .C ,.cc,.cxx,.cpp,*.c++ |
Borland C++ | *.cpp |
Microsoft Visual C++ | .cpp,.cxx,*.cc |
2、引用头文件
C++头文件使用C标准库,在C标准库文件名前面加上字母c,并且省略后缀名.h,例如:
C | C++ |
---|---|
#include <stdio.h> | #include <iostream/#include <cstdio |
#include <stdlib.h> | #include <cstdlib |
#include <string.h> | #include <cstring |
#include <math.h> | #include <cmath |
有些C++编译器同时支持以上两种头文件,但是有些不支持。所以请使用C++标准方式
3、函数重载
实验:以下C与C++的编译执行结果
(1)printf.c
#include <stdio.h>
void printf(){
printf("Hello world");
}
int main(){
printf();
}
printf.c:2:6: error: conflicting types for ‘printf’
void printf(){
^~~~~~
In file included from printf.c:1:
/usr/include/stdio.h:332:12: note: previous declaration of ‘printf’ was here
extern int printf (const char *__restrict __format, ...);
(2)printf.cpp
#include <cstdio>
void printf(){
printf("Hello world");
}
int main(){
printf();
}
编译通过
函数重载:
函数名相同只有参数(个数或者类型)不同
C | C++ |
---|---|
不支持重载 | 支持重载 |
4、命名空间
4.1 实验:以下C的编译结果
//scope.c
#include <stdio.h>
void test(){
printf("this is test\n");
}
void test(){
printf("this is another test\n");
}
int main(){
test();
}
scope.c:7:6: error: redefinition of ‘test’
void test(){
^~~~
scope.c:3:6: note: previous definition of ‘test’ was here
void test(){
^~~~
4.2 命名空间
C | C++ |
---|---|
不支持命名空间 | 支持命名空间 |
4.3 命名空间的作用
避免全局变量、函数、类的命名冲突(因为名字相同而编译失败)
4.4 定义命名空间
namespace 空间名 {
// 定义类/函数
}
4.5 引用命名空间
using指令
using namespace 空间名;
例如使用标准命名空间std:
using namespace std;
using 声明(using declaration)
using 空间名::标识符;
例如使用标准命名空间std的cout
using std::cout;
4.6 C++命名空间的处理方式
#include <cstdio>
namespace scope1{
void test(){
printf("this is test\n");
}
}
namespace scope2{
void test(){
printf("this is another test\n");
}
}
int main(){
scope1::test();
scope2::test();
}
4.7 全局命名空间
(1)默认的命名空间,所有名字都在全局命名空间中。
(2)使用方式:直接忽略或者只写::
例如:定义全局函数void test(),默认就是在全局命名空间中,调用方式test()或者::test()
在C++中,不带.h后缀的头文件所包含和定义的标识符在std空间中;
带.h后缀的头文件所包含和定义的标识符在全局命名空间中,不需要声明使用std空间
5、类型
5.1 新增基本类型bool --true/false
在C99中的stdio.h中增加了三个宏定义bool、true和false。在C++中是内置类型和常量
5.2 关于验证boo是不是宏定义
(1)password.c
#include <stdio.h>
int main(){
printf("input user name:");
char name[BUFSIZ];
scanf("%s",name);
printf("input 3 number password:");
int password1;
scanf("%s",&password1);
printf("input 3 number password again:");
int password2;
scanf("%s",&password2);
printf("password check:%d\n",password1==password2);
}
(2)password.cpp
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
cout << "input username:";
char name[BUFSIZ];
cin >> name;
cout << "input 3 number password:";
int password1;
cin >> password1;
cout << "input 3 number password again:";
int password2;
cin >> password2;
cout << "password check:" << (password1==password2) <<endl;
}
6、思想
C | C++ |
---|---|
面向过程 | 面向对象/基于对象 |
何为面向过程?面向对象?
(1)面向过程:强调如何处理(如何解决)
(2)面向对象:强调执行处理的对象(找谁解决)
面向过程与面向对象:厨师与老板
思维区别
将问题按照过程方式来解决?
将问题抽象为一个对象来解决?
7、动态内存
7.1 基本类型的动态内存
(1)C
#include <stdio.h>
#include <stdlib.h>
int main(){
int *num = malloc(sizeof(int));
*num = 100;
printf("%d\n",*num);
free(num);
}
(2)C++
#include <iostream>
using namespace std;
int main(){
int *num = new int;
*num = 100;
cout << *num << endl;
delete num;
}
(3)动态内存的区别
C | C++ |
---|---|
malloc()/free() | new/delete |
8、初始化
C++特殊初始化方法
int n=10;
int m(10);